diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..aef7e2a --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,114 @@ +name: Release + +on: + push: + tags: + - "v*" + +jobs: + build: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: true + matrix: + os: [ ubuntu-latest, macos-latest ] + arch: [ x86_64, arm64 ] + exclude: + - os: macos-latest + arch: x86_64 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up permissions to execute build script for Linux/macOS + if: ${{ matrix.os != 'windows-latest' }} + run: chmod +x build-mupdf.sh + + - name: Install cross-compiler for arm64 on Ubuntu + if: ${{ matrix.os == 'ubuntu-latest' }} + run: | + sudo apt-get update + sudo apt-get install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu + + - name: Install XQuartz + if: ${{ matrix.os == 'macos-latest' }} + run: | + echo "export HOMEBREW_NO_AUTO_UPDATE=1" >> $GITHUB_ENV + brew install --cask xquartz + + - name: Build mutool for ${{ matrix.os }} ${{ matrix.arch }} + shell: bash + run: | + target_os=${{ matrix.os == 'ubuntu-latest' && 'linux' || 'darwin' }} + target_arch=${{ matrix.arch }} + + OS=$target_os ARCH=$target_arch ./build-mupdf.sh + + - name: Upload binary artifact + uses: actions/upload-artifact@v4 + with: + name: mutool_${{ matrix.os == 'ubuntu-latest' && 'linux' || 'darwin' }}_${{ matrix.arch }} + # path: bin/mutool_${{ matrix.os }}* + path: bin/mutool* + + release: + needs: build + runs-on: ubuntu-latest + steps: + - name: Download mutool binaries + uses: actions/download-artifact@v4 + with: + path: bin/ + pattern: mutool_* + + - name: Create release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{ github.ref }} + release_name: Release ${{ github.ref }} + draft: true + prerelease: false + + - name: Upload Linux binary (x86_64) to Release + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: bin/mutool_linux_x86_64 + asset_name: mutool_linux_x86_64 + asset_content_type: application/octet-stream + + - name: Upload Linux binary (arm64) to Release + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: bin/mutool_linux_arm64 + asset_name: mutool_linux_arm64 + asset_content_type: application/octet-stream + + - name: Upload macOS binary (x86_64) to Release + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: bin/mutool_darwin_x86_64 + asset_name: mutool_darwin_x86_64 + asset_content_type: application/octet-stream + + - name: Upload macOS binary (arm64) to Release + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ steps.create_release.outputs.upload_url }} + asset_path: bin/mutool_darwin_arm64 + asset_name: mutool_darwin_arm64 + asset_content_type: application/octet-stream diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c9cfa0b..1f22b66 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,6 +1,10 @@ name: Tests -on: [ 'push', 'pull_request' ] +# on: [ 'push', 'pull_request' ] +on: + push: + branches: + - "master" jobs: ci: @@ -15,12 +19,11 @@ jobs: name: Tests P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }} steps: - - name: Checkout - uses: actions/checkout@v2 + uses: actions/checkout@v4 - name: Cache dependencies - uses: actions/cache@v1 + uses: actions/cache@v4 with: path: ~/.composer/cache/files key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} @@ -35,9 +38,13 @@ jobs: - name: Install Composer dependencies run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist - - name: Replace mutool binary for macos - if: ${{ matrix.os == 'macos-latest' }} - run: mv ./bin/mutool-macos ./bin/mutool + - name: Set up permissions to execute build script for Linux/macOS + if: ${{ matrix.os != 'windows-latest' }} + run: chmod +x build-mupdf.sh + + - name: Build mutool + shell: bash + run: ./build-mupdf.sh - - name: Unit Tests + - name: Unit tests run: php ./vendor/bin/pest diff --git a/.gitignore b/.gitignore index 7ae6add..ac2cecf 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ vendor phpcs.xml phpunit.xml .phpunit.result.cache +.idea +.vscode +.phpactor.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d9f2b7..f68b36e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,22 +1,35 @@ # Changelog - All notable changes to `php-mupdf` will be documented in this file. -Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) principles. - -## NEXT - YYYY-MM-DD +## [Unreleased] +## [3.0.0] - 2024-11-08 +> v3.0.0 is a major release with breaking changes. ### Added -- Nothing - -### Deprecated -- Nothing +- Workflow for building `mutool` binary for different platforms and packaging it as a release asset +### Removed +- `mutool` binary is now available as a release asset instead of being commited to repository -### Fixed -- Nothing +## [2.0.1] - 2024-05-22 +### Added +- Support for PHP 8.2 +## [2.0.0] - 2022-08-11 ### Removed -- Nothing +- Support for PHP 7.4 + +## [1.1.0] - 2022-07-22 +### Added +- Support for png format -### Security -- Nothing +## [1.0.0] - 2022-07-21 +> Initial release +### Added +- Convert PDF to jpg + +[unreleased]: https://github.com/dotgksh/php-mupdf/compare/v1.0.0...HEAD +[1.0.0]: https://github.com/dotgksh/php-mupdf/releases/tag/v1.0.0 +[1.1.0]: https://github.com/dotgksh/php-mupdf/releases/tag/v1.1.0 +[2.0.0]: https://github.com/dotgksh/php-mupdf/releases/tag/v2.0.0 +[2.0.1]: https://github.com/dotgksh/php-mupdf/releases/tag/v2.0.1 +[3.0.0]: https://github.com/dotgksh/php-mupdf/releases/tag/v3.0.0 diff --git a/README.md b/README.md index c457e9e..000233a 100644 --- a/README.md +++ b/README.md @@ -11,27 +11,21 @@ Minimal PDF to image converter using [MuPDF](https://mupdf.com/docs/mutool.html) [![Total Downloads](https://img.shields.io/packagist/dt/karkowg/php-mupdf.svg)](https://packagist.org/packages/karkowg/php-mupdf) ## Install - -Via Composer - ``` bash $ composer require karkowg/php-mupdf ``` ## Usage - ### Convert all the pages to jpg - ``` php -$pdf = new Karkow\MuPdf\Pdf('path/to/pdf/file'); +$pdf = new Gksh\MuPdf\Pdf('path/to/pdf/file'); $pdf->saveAllPagesAsImages('./images/', 'page-'); ``` ### Convert a single page to png - ``` php -$pdf = new Karkow\MuPdf\Pdf('path/to/pdf/file'); +$pdf = new Gksh\MuPdf\Pdf('path/to/pdf/file'); $pdf ->setPage(2) @@ -42,50 +36,30 @@ $pdf Please refer to `tests/PdfTest.php` for other use cases. ## [mutool](https://mupdf.com/releases/index.html) - -A compiled binary (v1.20.0) is available at `bin/mutool`. If for any reason you want/need to use your own installation, you can do so by passing its path as a 2nd argument to the constructor. +A build script will compile `mutool` and make it available under `bin/mutool`. If for any reason you want/need to use your own installation, you can do so by passing its path as a 2nd argument to the constructor. ``` php -$pdf = new Karkow\MuPdf\Pdf('path/to/pdf/file', 'path/to/mutool'); +$pdf = new Gksh\MuPdf\Pdf('path/to/pdf/file', 'path/to/mutool'); ``` -## Change log - -Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. - ## Testing - ``` bash +$ ./build-mupdf.sh $ composer test ``` -## Contributing +## Changelog +Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. +## Contributing Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE_OF_CONDUCT](CODE_OF_CONDUCT.md) for details. ## Security - If you discover any security related issues, please email karkowg@gmail.com instead of using the issue tracker. ## Credits - -- [Gustavo Karkow][link-author] -- [All Contributors][link-contributors] +- [Gustavo Karkow](https://github.com/karkowg) +- [All Contributors](../../contributors) ## License - The MIT License (MIT). Please see [License File](LICENSE.md) for more information. - -[ico-version]: https://img.shields.io/packagist/v/karkowg/php-mupdf.svg?style=flat-square -[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square -[ico-travis]: https://img.shields.io/travis/karkowg/php-mupdf/master.svg?style=flat-square -[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/karkowg/php-mupdf.svg?style=flat-square -[ico-code-quality]: https://img.shields.io/scrutinizer/g/karkowg/php-mupdf.svg?style=flat-square -[ico-downloads]: https://img.shields.io/packagist/dt/karkowg/php-mupdf.svg?style=flat-square - -[link-packagist]: https://packagist.org/packages/karkowg/php-mupdf -[link-travis]: https://travis-ci.org/karkowg/php-mupdf -[link-scrutinizer]: https://scrutinizer-ci.com/g/karkowg/php-mupdf/code-structure -[link-code-quality]: https://scrutinizer-ci.com/g/karkowg/php-mupdf -[link-downloads]: https://packagist.org/packages/karkowg/php-mupdf -[link-author]: https://github.com/karkowg diff --git a/bin/.gitignore b/bin/.gitignore index 7d70235..72e8ffc 100644 --- a/bin/.gitignore +++ b/bin/.gitignore @@ -1,3 +1 @@ * -!mutool -!mutool-macos diff --git a/bin/mutool b/bin/mutool deleted file mode 100755 index 412339f..0000000 Binary files a/bin/mutool and /dev/null differ diff --git a/bin/mutool-macos b/bin/mutool-macos deleted file mode 100755 index fabc85e..0000000 Binary files a/bin/mutool-macos and /dev/null differ diff --git a/build-mupdf.sh b/build-mupdf.sh index 2b4c41d..d3ffa5f 100755 --- a/build-mupdf.sh +++ b/build-mupdf.sh @@ -1,12 +1,46 @@ #!/usr/bin/env bash -rm -f ./bin/mutool +VERSION=1.24.10 + rm -rf ./build -mkdir build -cd build -curl "https://mupdf.com/downloads/archive/mupdf-1.20.0-source.tar.gz" -o "mupdf.tar.gz" +mkdir -p build +mkdir -p bin + +cd build || exit +curl "https://mupdf.com/downloads/archive/mupdf-$VERSION-source.tar.gz" -o "mupdf.tar.gz" tar -xf mupdf.tar.gz -cd mupdf-1.20.0-source -make HAVE_X11=no HAVE_GLUT=no prefix=../. install +cd mupdf-$VERSION-source || exit +if [[ "$OS" == "linux" ]]; then + if [[ "$ARCH" == "x86_64" ]]; then + export CC="gcc" + export XCFLAGS="-m64" + elif [[ "$ARCH" == "arm64" ]]; then + export CC="aarch64-linux-gnu-gcc" + export CXX="aarch64-linux-gnu-g++" + export XCFLAGS="-march=armv8-a" + fi + make HAVE_X11=no HAVE_GLUT=no prefix=../. install +else + if [[ "$ARCH" == "x86_64" ]]; then + export CC="clang" + export CXX="clang++" + export XCFLAGS="-arch x86_64" + export XLDFLAGS="-L/opt/X11/lib -lX11 -framework OpenGL" + export XCXXFLAGS="-v" + elif [[ "$ARCH" == "arm64" ]]; then + export CC="gcc-12" + export CXX="g++-12" + export XCFLAGS="-arch arm64 -I/opt/X11/include" + export XLDFLAGS="-L/opt/X11/lib -lX11 -framework OpenGL" + export XCXXFLAGS="-v" + fi + which xterm + ps aux | grep XQuartz + make prefix=../. install +fi + +cd .. +ls -la +cp ./bin/mutool ../bin/mutool cd .. -cp bin/mutool ../bin/mutool +ls -la bin diff --git a/composer.json b/composer.json index abfe196..03097ad 100644 --- a/composer.json +++ b/composer.json @@ -1,25 +1,22 @@ { - "name": "karkowg/php-mupdf", + "name": "gksh/php-mupdf", "type": "library", "description": "Minimal PDF to image converter using MuPDF", "keywords": [ - "karkowg", + "php", "mutool", "mupdf", - "php-mupdf", - "pdf-to-image", "pdf", + "pdf-to-image", "convert", "image" ], - "homepage": "https://github.com/karkowg/php-mupdf", + "homepage": "https://github.com/dotgksh/php-mupdf", "license": "MIT", "authors": [ { "name": "Gustavo Karkow", - "email": "karkowg@gmail.com", - "homepage": "https://github.com/karkowg", - "role": "Developer" + "email": "karkowg@gmail.com" } ], "require": { @@ -32,12 +29,12 @@ }, "autoload": { "psr-4": { - "Karkow\\MuPdf\\": "src" + "Gksh\\MuPdf\\": "src" } }, "autoload-dev": { "psr-4": { - "Karkow\\MuPdf\\Tests\\": "tests" + "Gksh\\MuPdf\\Tests\\": "tests" } }, "bin": ["bin/mutool"], diff --git a/scripts/install.php b/scripts/install.php new file mode 100644 index 0000000..8df32ff --- /dev/null +++ b/scripts/install.php @@ -0,0 +1,80 @@ + "x86_64", + "amd64" => "x86_64", + "aarch64" => "arm64", + "arm64" => "arm64", + ]; + + return $archMap[$arch] ?? null; +} + +function getBinaryUrl($os, $architecture) +{ + $binaryUrls = [ + "linux" => [ + "x86_64" => "https://github.com/dotgksh/php-mupdf/releases/latest/download/mutool_linux_x86_64", + "arm64" => "https://github.com/dotgksh/php-mupdf/releases/latest/download/mutool_linux_arm64", + ], + "darwin" => [ + "x86_64" => "https://github.com/dotgksh/php-mupdf/releases/latest/download/mutool_darwin_x86_64", + "arm64" => "https://github.com/dotgksh/php-mupdf/releases/latest/download/mutool_darwin_arm64", + ], + "windows" => [], + ]; + + return $binaryUrls[$os][$architecture] ?? null; +} + +function downloadBinary($url, $target) +{ + $content = file_get_contents($url); + + if ($content === false) { + throw new Exception("Failed to download from $url"); + } + + file_put_contents($target, $content); +} + +$os = strtolower(PHP_OS_FAMILY); +$architecture = getArchitecture(); + +if (! $architecture) { + echo "Unsupported architecture: " . php_uname("m") . "\n"; + exit(1); +} + +$url = getBinaryUrl($os, $architecture); + +if (! $url) { + echo "Unsupported OS: $os or architecture: $architecture\n"; + exit(1); +} + +if (! file_exists($binDir)) { + mkdir($binDir, 0755, true); +} + +$target = "$binDir/mutool" . ($os === "windows" ? ".exe" : ""); + +echo "Downloading mutool binary for $os ($architecture)...\n"; + +try { + downloadBinary($url, $target); + + if ($os !== "windows") { + chmod($target, 0755); + } + + echo "Installed mutool binary in $target\n"; +} catch (Exception $e) { + echo "Error: " . $e->getMessage() . "\n"; + exit(1); +} diff --git a/src/Exceptions/InvalidFormat.php b/src/Exceptions/InvalidFormat.php index 3061ca4..f08095b 100644 --- a/src/Exceptions/InvalidFormat.php +++ b/src/Exceptions/InvalidFormat.php @@ -1,6 +1,6 @@ testFile = __DIR__ . '/files/test.pdf';