In the course of working on another project (migrating a website to the free “Chocolate” hosting plan at Freehostia), I stumbled across something pretty awesome that I wanted to share.
This came about as part of the 500 KB file size limitation with the Freehostia Chocolate plan. The 500 KB limit is not a knock on Freehostia at all - I believe they are the best free hosting provider out there and would recommend them wholeheartedly.
The awesome trick I mentioned is this: The ability to use a single Action in Microsoft Flow to either:
- Compress a PDF (reduce its file size)
- Convert files of various formats to and from PDF
This is made possible with ConvertAPI and the HTTP action in Flow. I will outline my findings in this post.
Setup ConvertAPI (1,500 free “seconds”)
The heavy lifting in this scenario is done with ConvertAPI. You can use this service free of charge (sort of). Upon sign up, you receive 1,500 “conversion seconds” - this is a one-time allotment. Once you consume all 1,500 seconds, your only option is upgrading to a paid plan.
In my case, conversions (compressing PDF files between 1 MB and 2 MB) consume roughly 1 second per transaction. In other words, I would be able to compress ~1,500 PDFs without paying for the service. For my use case, this is fine and will keep me going for years (or more) - your mileage may vary.
The ConvertAPI setup is easy:
1) Sign up for a ConvertAPI account
2) Once signed up and signed in, get your Secret from Account Settings - you will use this in your Flow
Setup the HTTP Action in Flow
The HTTP Action setup in Flow was tricky at first, but I got it working as desired after a little tinkering. Flow is keen on passing around raw file content between Actions and the bulk of the ConvertAPI documentation was aimed at converting or compressing uploaded files (not necessarily their content). The following HTTP Action configuration does the job.
1) Capture the file content of the file you want to convert or compress. This can be accomplished with a variety of Triggers and Actions like “Get file content using path”, “Attachments Content” from the “Attachments” array of a “When a new email arrives trigger”, etc.
Example: Passing raw PDF content from an email attachment into ConvertAPI
Example: Passing raw PDF content from an SFTP action into ConvertAPI
2) Create an HTTP Action as follows. The Method should be POST, the URI is the path to the ConvertAPI endpoint for your use case (I used convert/pdf/to/compress), the 3 Headers are required, Queries are used to specify conversion options (and your Secret), and the Body is the file content you need to convert.
The required Headers are:
- Content-Type: application/octet-stream
- Accept: application/octet-stream
- Content-Disposition: attachment; filename=“myfile.pdf”
The required Queries are (for API authentication):
- Secret: (your ConvertAPI secret)
Other Queries or Query String Parameters can be appended as well - you can use the ConvertAPI documentation to see what parameters are supported (I prefer the curl view). In my examples, I tweaked the PDF compression options.
3) The Body returned by the HTTP Action is the converted (or compressed PDF) - once the HTTP Action runs, you must write out (save) its Body into a destination file.
Other HTTP action examples for ConvertAPI
Convert a JPG image to a PNG image - ConvertAPI reference
There are certainly other ways to create/save/convert to PDF with Microsoft Flow but using ConvertAPI is by far the easiest method I have come across. It is only a single action! Depending on your transaction volume, ConvertAPI may or may not be a good option for your conversions or compressions. I have not run the costs for the paid plans, but at a glance, the cost appears to be less than $0.01 per “conversion second” which sounds reasonable.