Microsoft Power Automate (Flow): compressing PDFs

Decorative title image of Microsoft Flow and ConvertAPI logos with PDF file icon

All product names, logos, and brands used in this post are property of their respective owners.

Due to Power Automate (Flow) licensing changes announced in 2019 and taking full effect in 2020, the HTTP connector referenced in this post is now considered "Premium" and requires a paid (Power Automate per user plan or Power Automate per flow plan) license.

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 Power Automate (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 Power Automate (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

ConvertAPI secret

Setup the HTTP Action in Power Automate (Flow)

The HTTP Action setup in Power Automate (Flow) was tricky at first, but I got it working as desired after a little tinkering. Power Automate (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.

Passing raw PDF content from an email attachment into ConvertAPI Example: Passing raw PDF content from an email attachment into ConvertAPI

Passing raw PDF content from an SFTP action into ConvertAPI Example: Passing raw PDF content from an SFTP action into ConvertAPI

  1. 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.

Call the ConvertAPI web service using the HTTP Action in Power Automate (Flow)

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.

  1. 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.

Example of creating a file via SFTP using the Response Body from the HTTP Action

Other HTTP action examples for ConvertAPI

Example HTTP action settings for converting HTML to PDF with ConvertAPI via Power Automate (Flow) Create a PDF from HTML - ConvertAPI reference. In this case, HTML is passed directly in the HTTP Action body (instead of using output from a previous Action). Here is the resulting PDF.

Example HTTP action settings for converting JPG to PNG with ConvertAPI via Power Automate (Flow) Convert a JPG image to a PNG image - ConvertAPI reference

Final thoughts

There are certainly other ways to create/save/convert to PDF with Microsoft Power Automate (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.