Preserve Code Colors When Converting HTML to PDF with Zapier

6 Minutes Read

This tutorial shows how to convert syntax-highlighted code into a PDF while preserving its existing HTML and CSS colors.

Prerequisites

You will need:

  • A Zapier account
  • A PDF.co account
  • A publicly accessible webpage or raw HTML containing color-styled code

PDF.co preserves the colors defined in the source HTML. It does not automatically add syntax highlighting to unformatted code.

Step 1: Select a Trigger

Create a Zap and select the app that provides your HTML or webpage URL.

For a simple recurring conversion, you can use Schedule by Zapier and select the desired frequency. For an event-driven workflow, use a trigger from your form, database, CMS, or spreadsheet.

Test the trigger before continuing.

Step 2: Add the PDF.co Action

  1. Add PDF.co as the action app.
  2. Select HTML to PDF.
  3. Sign in and authorize your PDF.co account.

Step 3: Configure the Conversion

Select the appropriate Input Type:

  • Choose URL to convert a publicly accessible webpage.
  • Choose Raw HTML when the HTML code comes from an earlier Zapier step.

Then configure:

  • URL or HTML: Enter the webpage URL or map the raw HTML.
  • File Name: Enter a name such as formatted-code.pdf.
  • Page Size: Select the desired paper size.
  • Page Orientation: Use landscape if the code contains long lines.
  • Render Page Background: Enable this when the code block uses a colored background.
  • Do Not Wait Until Full Page Load: Leave disabled so external stylesheets and fonts have time to load.

The source HTML should already include its syntax-highlighting styles. If it references an external CSS file, that file must also be publicly accessible.

Step 4: Test the Conversion

Test the PDF.co action and open the returned PDF URL.

Verify that:

  • Syntax colors match the source webpage.
  • The code block background is visible.
  • Long lines are not cut off.
  • External fonts and styles have loaded.

If lines extend beyond the page, use landscape orientation or update the HTML to wrap long lines.

Step 5: Store the PDF

PDF.co output URLs are temporary. Add a storage action such as Google Drive – Upload File and map the PDF.co output URL into its file field.

Step 6: Publish the Zap

Run one complete test. Once the styled code and layout appear correctly in the stored PDF, publish the Zap.

In this tutorial, you learned how to maintain the code colors when exporting to PDF using PDF.co and Zapier.

Related Tutorials

See Related Tutorials