Giter Site home page Giter Site logo

rust-pdf-to-png-service's Introduction

PDF to PNG Service

midjourney feature image

This is the result of a long, painful and embarrassing journey to resolve a very simple task: get pngs and webp files from a PDF. Here are a few alternatives we tried:

  • pdf-to-png-converter - great, simple solution, but limited pdf render functions
  • gm - tried many different variations but we couldn't get this to work
  • sharp - built on libvips it's the fastest image conversion library, but doesn't come with PDF support out of the box. pdfium is required and it's a nightmare to try to build a somewhat up-to-date Dockerfile with libvips and libpdfium.

Cloud Functions / Lambda have ImageMagick pre-installed which should be capbable, but:

  • on Google Cloud Functions, ImageMagick is only installed on the Node 12 and 14 runtime. Not on Node 16 ๐Ÿคฆ
  • In order to read PDF, ImageMagick requires a change in a policy.xml which we don't have access to in the Functions environment

Setup

The Dockerfile has everything you need. You can build it locally or in the cloud and use the Cloud Run environment to start the container. There are two environment variables needed:

    SOURCE_BUCKET=source-bucket
    DEST_BUCKET=destination-bucket

This service is currently designed for Google Cloud EventArc triggers. You should create a trigger on the source bucket when new files are uploaded/finalized. The trigger will then call the Cloud Run instance and execute this service. There'll be various versions:

  • original png: full size, 90% quality
  • preview png: 1920px width, 75% quality, interlace
  • thumbnail png: 1024px width, 70% quality, interlace
  • lossless webp: full size, 100% quality
  • preview webp: 1920px width, 75% quality

All versions will be stored on the destination bucket with the same filename as the source (including folders), and a prefix for the version.

Local Development

To build and run locally, you need to create a service account and place the keyfile in the same folder. You'll need to add:

GOOGLE_APPLICATION_CREDENTIALS=keyfile.json

As an environment variable. vips is linked statically:

RUSTFLAGS="-C target-feature=-crt-static $(pkg-config vips --libs)" cargo build && ./target/debug/pdf-service

Feel free to fork to adjust this to your own needs.

Happy converting.

Acknowledgements & Thanks

License

Licensed under MIT.

Built with ๐Ÿ’Œ in Cyprus ๐Ÿ‡จ๐Ÿ‡พ

rust-pdf-to-png-service's People

Contributors

patrickheneise avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

harssh

rust-pdf-to-png-service's Issues

Lossless PNG?

Is there a reason why lossless PNG isn't an option? =)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.