diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml deleted file mode 100644 index 7b28208..0000000 --- a/.github/workflows/go.yml +++ /dev/null @@ -1,33 +0,0 @@ -name: Go - -on: - push: - branches: - - main - - development - pull_request: - branches: - - main - - development - -jobs: - test: - strategy: - fail-fast: false - matrix: - os: [ ubuntu-latest, macos-latest, windows-latest ] - go: [ "1.21" ] - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v3 - - - name: Set up Go - uses: actions/setup-go@v3 - with: - go-version: ${{ matrix.go }} - - name: Run tests - run: | - go test ./... -v - - name: Test build - run: | - go build main.go diff --git a/.github/workflows/libturbojpeg.pc b/.github/workflows/libturbojpeg.pc new file mode 100644 index 0000000..02e1938 --- /dev/null +++ b/.github/workflows/libturbojpeg.pc @@ -0,0 +1,9 @@ +prefix=/opt/libjpegturbo/libjpeg-turbo-3.0.0 +libdir=/opt/libjpegturbo/libjpeg-turbo-3.0.0/build +includedir=/opt/libjpegturbo/libjpeg-turbo-3.0.0 + +Name: libturbojpeg +Description: A SIMD-accelerated JPEG codec that provides the TurboJPEG API +Version: 3.0.0 +Libs: -L${libdir} -lturbojpeg +Cflags: -I${includedir} \ No newline at end of file diff --git a/.github/workflows/pdfium-windows.pc b/.github/workflows/pdfium-windows.pc new file mode 100644 index 0000000..60e9d51 --- /dev/null +++ b/.github/workflows/pdfium-windows.pc @@ -0,0 +1,11 @@ +prefix=D:/opt/pdfium +libdir=D:/opt/pdfium/bin +includedir=D:/opt/pdfium/include + +Name: PDFium +Description: PDFium +Version: 6029 +Requires: + +Libs: -L${libdir} -lpdfium +Cflags: -I${includedir} diff --git a/.github/workflows/pdfium.pc b/.github/workflows/pdfium.pc new file mode 100644 index 0000000..cca98c1 --- /dev/null +++ b/.github/workflows/pdfium.pc @@ -0,0 +1,11 @@ +prefix=/opt/pdfium +libdir=/opt/pdfium/lib +includedir=/opt/pdfium/include + +Name: PDFium +Description: PDFium +Version: 6029 +Requires: + +Libs: -L${libdir} -lpdfium +Cflags: -I${includedir} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 15f05b5..8b4a63b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -4,37 +4,199 @@ on: release: types: [created] +env: + GO_VERSION: "1.21" + PDFIUM_VERSION: "6029" + jobs: - build: + release-webassembly: strategy: fail-fast: false matrix: - os: [ ubuntu-20.04, macos-latest, windows-latest ] - go: [ "1.21" ] + os: [ ubuntu-latest, macos-latest, windows-latest ] runs-on: ${{ matrix.os }} + env: + CGO_ENABLED: 0 steps: - uses: actions/checkout@v3 - name: Set up Go uses: actions/setup-go@v3 with: - go-version: ${{ matrix.go }} + go-version: ${{ env.GO_VERSION }} - name: Build Linux - if: matrix.os == 'ubuntu-20.04' + if: matrix.os == 'ubuntu-latest' run: | - go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-linux-x64 main.go + GOARCH=amd64 go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-webassembly-linux-amd64 main.go + GOARCH=arm64 go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-webassembly-linux-arm64 main.go - name: Build MacOS if: matrix.os == 'macos-latest' run: | - go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-mac-x64 main.go + GOARCH=amd64 go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-webassembly-mac-amd64 main.go + GOARCH=arm64 go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-webassembly-mac-arm64 main.go - name: Build Windows if: matrix.os == 'windows-latest' run: | - go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-windows-x64 main.go + GOARCH=amd64 go build -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-webassembly-windows-amd64 main.go + - uses: actions/upload-artifact@v3 + with: + name: builds + path: artifacts + - uses: AButler/upload-release-assets@v2.0 + with: + files: 'artifacts/*' + repo-token: ${{ secrets.GITHUB_TOKEN }} + + release-cgo-linux: + env: + CGO_ENABLED: 1 + LD_LIBRARY_PATH: "/opt/pdfium/lib" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library libjpeg-turbo + run: | + sudo curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-linux-x64.tgz -o pdfium-linux-x64.tgz && sudo mkdir /opt/pdfium && sudo tar -C /opt/pdfium -xvf pdfium-linux-x64.tgz + sudo rm pdfium-linux-x64.tgz + sudo cp ./.github/workflows/pdfium.pc /usr/lib/pkgconfig/pdfium.pc + sudo apt-get update -y + sudo apt-get install -y libturbojpeg libturbojpeg-dev + + - name: Build + run: | + go build -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-native-linux-amd64 main.go + + - uses: actions/upload-artifact@v3 + with: + name: builds + path: artifacts + + - uses: AButler/upload-release-assets@v2.0 + with: + files: 'artifacts/*' + repo-token: ${{ secrets.GITHUB_TOKEN }} + + release-cgo-linux-musl: + env: + CGO_ENABLED: 1 + CC: "musl-gcc" + LD_LIBRARY_PATH: "/opt/pdfium/lib:/opt/libjpegturbo/libjpeg-turbo-3.0.0/build" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library, musl and libjpeg-turbo + run: | + sudo curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-linux-musl-x64.tgz -o pdfium-linux-musl-x64.tgz && sudo mkdir /opt/pdfium && sudo tar -C /opt/pdfium -xvf pdfium-linux-musl-x64.tgz + sudo rm pdfium-linux-musl-x64.tgz + sudo cp ./.github/workflows/pdfium.pc /usr/lib/pkgconfig/pdfium.pc + sudo cp ./.github/workflows/libturbojpeg.pc /usr/lib/pkgconfig/libturbojpeg.pc + sudo apt-get update -y + sudo apt-get install -y musl-dev musl-tools + sudo curl -L https://master.dl.sourceforge.net/project/libjpeg-turbo/3.0.0/libjpeg-turbo-3.0.0.tar.gz -o libjpeg-turbo-3.0.0.tar.gz && sudo mkdir /opt/libjpegturbo && sudo tar -C /opt/libjpegturbo -xvf libjpeg-turbo-3.0.0.tar.gz + cd /opt/libjpegturbo/libjpeg-turbo-3.0.0 && sudo mkdir build && cd build + sudo cmake -G"Unix Makefiles" -DCMAKE_C_COMPILER=musl-gcc -DWITH_JPEG8=1 -DCMAKE_BUILD_TYPE=None ../ + sudo make turbojpeg + + - name: Build + run: | + go build --ldflags '-linkmode external -extldflags "-fno-PIC -L/opt/libjpegturbo/libjpeg-turbo-3.0.0/build"' -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-native-linux-musl-amd64 main.go + + - uses: actions/upload-artifact@v3 + with: + name: builds + path: artifacts + + - uses: AButler/upload-release-assets@v2.0 + with: + files: 'artifacts/*' + repo-token: ${{ secrets.GITHUB_TOKEN }} + + + release-cgo-macos: + env: + CGO_ENABLED: 1 + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library and libjpeg-turbo + run: | + sudo curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-mac-x64.tgz -o pdfium-mac-x64.tgz && sudo mkdir /opt/pdfium && sudo tar -C /opt/pdfium -xvf pdfium-mac-x64.tgz + sudo rm pdfium-mac-x64.tgz + sudo cp ./.github/workflows/pdfium.pc /usr/local/lib/pkgconfig/pdfium.pc + brew install jpeg-turbo + + - name: Build + run: | + export LD_LIBRARY_PATH="/opt/pdfium/lib" + go build -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-native-mac-amd64 main.go + - uses: actions/upload-artifact@v3 with: name: builds path: artifacts + + - uses: AButler/upload-release-assets@v2.0 + with: + files: 'artifacts/*' + repo-token: ${{ secrets.GITHUB_TOKEN }} + + release-cgo-windows: + env: + CGO_ENABLED: 1 + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library and libjpeg-turbo + run: | + curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-win-x64.tgz -o pdfium-win-x64.tgz + mkdir -p D:\opt\pdfium + tar -C D:\opt\pdfium -xvf pdfium-win-x64.tgz + rm pdfium-win-x64.tgz + mkdir D:\opt\pkgconfig + cp ./.github/workflows/pdfium-windows.pc D:\opt\pkgconfig\pdfium.pc + curl -L https://master.dl.sourceforge.net/project/libjpeg-turbo/3.0.0/libjpeg-turbo-3.0.0-gcc64.exe -o libjpeg-turbo-3.0.0-gcc64.exe + ./libjpeg-turbo-3.0.0-gcc64.exe /S + $Folder = 'C:\libjpeg-turbo-gcc64\lib\pkgconfig' + while (!(Test-Path -Path $Folder)) { + "libjpeg-turbo does not exist yet!" + Start-Sleep -s 5 + } + + - name: Build + run: | + $env:PKG_CONFIG_PATH = 'D:\opt\pkgconfig;C:\libjpeg-turbo-gcc64\lib\pkgconfig' + $env:Path += ";D:\opt\pdfium\bin;C:\libjpeg-turbo-gcc64\bin" + go build -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -ldflags "-X 'github.com/klippa-app/pdfium-cli/version.VERSION=${{ github.event.release.name }}'" -o artifacts/pdfium-native-windows-amd64 main.go + + - uses: actions/upload-artifact@v3 + with: + name: builds + path: artifacts + - uses: AButler/upload-release-assets@v2.0 with: files: 'artifacts/*' diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..411d99a --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,171 @@ +name: Go tests + +on: + push: + branches: + - main + - development + pull_request: + branches: + - main + - development + +env: + GO_VERSION: "1.21" + PDFIUM_VERSION: "6029" + +jobs: + test-webassembly: + strategy: + fail-fast: false + matrix: + os: [ ubuntu-latest, macos-latest, windows-latest ] + runs-on: ${{ matrix.os }} + env: + CGO_ENABLED: 0 + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + - name: Run tests + run: | + go test ./... -v + - name: Test build + run: | + go build main.go + + test-cgo-linux: + env: + CGO_ENABLED: 1 + LD_LIBRARY_PATH: "/opt/pdfium/lib" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library libjpeg-turbo + run: | + sudo curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-linux-x64.tgz -o pdfium-linux-x64.tgz && sudo mkdir /opt/pdfium && sudo tar -C /opt/pdfium -xvf pdfium-linux-x64.tgz + sudo rm pdfium-linux-x64.tgz + sudo cp ./.github/workflows/pdfium.pc /usr/lib/pkgconfig/pdfium.pc + sudo apt-get update -y + sudo apt-get install -y libturbojpeg libturbojpeg-dev + + - name: Test Linux + run: | + go test ./... -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -v + + - name: Build + run: | + go build -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg main.go + + test-cgo-linux-musl: + env: + CGO_ENABLED: 1 + CC: "musl-gcc" + LD_LIBRARY_PATH: "/opt/pdfium/lib:/opt/libjpegturbo/libjpeg-turbo-3.0.0/build" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library, musl and libjpeg-turbo + run: | + sudo curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-linux-musl-x64.tgz -o pdfium-linux-musl-x64.tgz && sudo mkdir /opt/pdfium && sudo tar -C /opt/pdfium -xvf pdfium-linux-musl-x64.tgz + sudo rm pdfium-linux-musl-x64.tgz + sudo cp ./.github/workflows/pdfium.pc /usr/lib/pkgconfig/pdfium.pc + sudo cp ./.github/workflows/libturbojpeg.pc /usr/lib/pkgconfig/libturbojpeg.pc + sudo apt-get update -y + sudo apt-get install -y musl-dev musl-tools + sudo curl -L https://master.dl.sourceforge.net/project/libjpeg-turbo/3.0.0/libjpeg-turbo-3.0.0.tar.gz -o libjpeg-turbo-3.0.0.tar.gz && sudo mkdir /opt/libjpegturbo && sudo tar -C /opt/libjpegturbo -xvf libjpeg-turbo-3.0.0.tar.gz + cd /opt/libjpegturbo/libjpeg-turbo-3.0.0 && sudo mkdir build && cd build + sudo cmake -G"Unix Makefiles" -DCMAKE_C_COMPILER=musl-gcc -DWITH_JPEG8=1 -DCMAKE_BUILD_TYPE=None ../ + sudo make turbojpeg + + - name: Test Linux + run: | + go test ./... --ldflags '-linkmode external -extldflags "-fno-PIC -L/opt/libjpegturbo/libjpeg-turbo-3.0.0/build"' -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -v + + - name: Build + run: | + go build --ldflags '-linkmode external -extldflags "-fno-PIC -L/opt/libjpegturbo/libjpeg-turbo-3.0.0/build"' -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg main.go + + test-cgo-macos: + env: + CGO_ENABLED: 1 + runs-on: macos-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library and libjpeg-turbo + run: | + sudo curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-mac-x64.tgz -o pdfium-mac-x64.tgz && sudo mkdir /opt/pdfium && sudo tar -C /opt/pdfium -xvf pdfium-mac-x64.tgz + sudo rm pdfium-mac-x64.tgz + sudo cp ./.github/workflows/pdfium.pc /usr/local/lib/pkgconfig/pdfium.pc + brew install jpeg-turbo + + - name: Test + run: | + export LD_LIBRARY_PATH="/opt/pdfium/lib" + go test ./... -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -exec "env DYLD_LIBRARY_PATH=/opt/pdfium/lib" -v + + - name: Build + run: | + export LD_LIBRARY_PATH="/opt/pdfium/lib" + go build -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg main.go + + test-cgo-windows: + env: + CGO_ENABLED: 1 + runs-on: windows-latest + steps: + - uses: actions/checkout@v3 + + - name: Set up Go + uses: actions/setup-go@v3 + with: + go-version: ${{ env.GO_VERSION }} + + - name: Set up PDFium library and libjpeg-turbo + run: | + curl -L https://github.com/bblanchon/pdfium-binaries/releases/download/chromium%2F${{ env.PDFIUM_VERSION }}/pdfium-win-x64.tgz -o pdfium-win-x64.tgz + mkdir -p D:\opt\pdfium + tar -C D:\opt\pdfium -xvf pdfium-win-x64.tgz + rm pdfium-win-x64.tgz + mkdir D:\opt\pkgconfig + cp ./.github/workflows/pdfium-windows.pc D:\opt\pkgconfig\pdfium.pc + curl -L https://master.dl.sourceforge.net/project/libjpeg-turbo/3.0.0/libjpeg-turbo-3.0.0-gcc64.exe -o libjpeg-turbo-3.0.0-gcc64.exe + ./libjpeg-turbo-3.0.0-gcc64.exe /S + $Folder = 'C:\libjpeg-turbo-gcc64\lib\pkgconfig' + while (!(Test-Path -Path $Folder)) { + "libjpeg-turbo does not exist yet!" + Start-Sleep -s 5 + } + + - name: Test + run: | + $env:PKG_CONFIG_PATH = 'D:\opt\pkgconfig;C:\libjpeg-turbo-gcc64\lib\pkgconfig' + $env:Path += ";D:\opt\pdfium\bin;C:\libjpeg-turbo-gcc64\bin" + go test ./... -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg -v + + - name: Build + run: | + $env:PKG_CONFIG_PATH = 'D:\opt\pkgconfig;C:\libjpeg-turbo-gcc64\lib\pkgconfig' + $env:Path += ";D:\opt\pdfium\bin;C:\libjpeg-turbo-gcc64\bin" + go build -tags pdfium_experimental,pdfium_cli_use_cgo,pdfium_use_turbojpeg main.go \ No newline at end of file diff --git a/README.md b/README.md index 68f1de7..e447166 100644 --- a/README.md +++ b/README.md @@ -38,9 +38,35 @@ Download the binary from the latest release for your platform and save it as `pd You can also use the `install` tool for this: ```bash -sudo install pdfium-linux-x64 /usr/local/bin/pdfium +sudo install pdfium-webassembly-linux-x64 /usr/local/bin/pdfium ``` +#### Release types + +The following release types are available: + +- Linux + - WebAssembly (amd64 + arm64) + - Native (amd64) + - Native + MUSL (amd64) +- MacOS + - WebAssembly (amd64 + arm64) + - Native (amd64) +- Windows + - WebAssembly (amd64) + - Native (amd64) + + +**WebAssembly**: this is a single binary that includes everything that you need to run pdfium-cli, but is a lot slower +than native due to the WebAssembly runtime. Most useful if speed is not a concern and easy distribution is more +important. + +**Native**: A native build that requires [pdfium](https://github.com/bblanchon/pdfium-binaries) and +[libjpeg-turbo](https://libjpeg-turbo.org/) to be available on your system. + +**Native + MUSL**: Same as native but built with MUSL so that it does not require a system libc which allows it to be +used in Alpine Docker containers. + ### From source Make sure you have a working Go development environment. @@ -98,6 +124,12 @@ Flags: Use "pdfium [command] --help" for more information about a command. ``` +The following build tags are available to control different build types: + + - pdfium_cli_use_cgo: whether to compile the native CGO version (faster, but requires [pdfium](https://github.com/bblanchon/pdfium-binaries) to be installed). + - pdfium_experimental: whether to enable experimental features of pdfium in the build. + - pdfium_use_turbojpeg: whether to enable [libjpeg-turbo](https://libjpeg-turbo.org/) support, which speeds up jpeg compression a lot compared to the default jpeg encoding in Go. + ## About Klippa Founded in 2015, [Klippa](https://www.klippa.com/en)'s goal is to digitize & automate administrative processes with