Convert Email to PDF Using an HTML Template and Zapier
Receive an email, place its details into an HTML template, and generate a PDF with PDF.co. This workflow includes the email’s headers and plain-text body; it does not convert attachments.
Step 1: Receive the Email
Create a Zap and select Email by Zapier → New Inbound Email.
Configure the receiving address and copy the full address provided by Zapier. Send a sample email to it, then test the trigger.
Confirm that the results contain the sender, recipient, subject, and message body.
Step 2: Populate the HTML Template
Add Code by Zapier → Run JavaScript.
Under Input Data, create these keys and map the corresponding values from the email trigger:
date: Email date.from: Sender’s name and email address.to: Recipient information.cc: CC recipients, if present.subject: Email subject.body: Plain-text email body.
Paste this code:
const escapeHtml = (value) =>
String(value ?? "").replace(/[&<>"']/g, (character) => ({
"&": "&",
"<": "<",
">": ">",
'"': """,
"'": "'"
})[character]);
const rows = [
["Date", inputData.date],
["From", inputData.from],
["To", inputData.to],
["CC", inputData.cc],
["Subject", inputData.subject]
]
.map(([label, value]) =>
`<tr><th>${label}:</th><td>${escapeHtml(value)}</td></tr>`
)
.join("");
return {
html: `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body { font-family: Arial, sans-serif; font-size: 12px; }
table { width: 100%; border-collapse: collapse; }
th { width: 70px; text-align: left; vertical-align: top; }
th, td { padding: 5px; overflow-wrap: anywhere; }
.message {
margin-top: 20px;
white-space: pre-wrap;
overflow-wrap: anywhere;
}
</style>
</head>
<body>
<table>${rows}</table>
<div class="message">${escapeHtml(inputData.body)}</div>
</body>
</html>`
};This creates the HTML template, inserts the email values, and escapes special characters so they display as text. Line breaks in the message body are preserved.
Test the step to generate its html output.
Step 3: Convert the HTML to PDF
Add PDF.co → HTML to PDF Converter.
Connect your PDF.co account using OAuth by signing in and authorizing Zapier.
Configure:
- HTML or URL Input: Map
htmlfrom the code step. - HTML Template ID: Leave blank.
- Page Orientation: Select Portrait.
- Page Size: Select A4 or Letter.
- Custom Margins: Enter
15mm. - Name: Enter
email.pdf.
The template is supplied directly as HTML, so it does not need to be saved in PDF.co’s template manager. See the HTML to PDF documentation for available settings.
Test the action and open the returned url. Check that the email details and message body appear correctly.
Step 4: Save the PDF
Add Google Drive → Upload File.
Connect your account, select a destination folder, and map the PDF.co output url to File.
Test the upload to confirm that the PDF is saved. This keeps a permanent copy before the temporary PDF.co link expires.
Step 5: Publish the Zap
Publish the Zap and send another email to the receiving address.
The workflow will populate the HTML template, create a PDF, and save it to Google Drive automatically.
Related Tutorials
