Save body email as PDF

Describe the issue/error/question

I am trying to save to PDF the body of an email. I have thought to use the field HTML from IMAP node to convert to PDF but I haven’t found the way to do it.
Do someone some idea about how can I do it?

Any help will be welcome.
Thank you very much for your time and patient!
BR!

Please share the workflow

Information on your n8n setup

  • **n8n version:**0.182.0
  • Database you’re using (default: SQLite): N/A
  • **Running n8n with the execution process [own(default), main]:**own
  • **Running n8n via [Docker, npm, n8n.cloud, desktop app]:**desktop app

Hi @franchogarcia, this isn’t an operation built into n8n I am afraid. You could use an API for the job, for example OneSimpleAPI. n8n has a node for it, but it only supports converting HTML coming from a URL into a PDF.

If you want to use your email HTML you’d need to use the HTTP Request node. Like so:

Your credentials would need to be set up like so (Name = token, Value would be your actual token):

The above workflow would transform this:

Into a PDF like this:

Thank you very much @MutedJam!! You are the best! :blush:
Do you think that your solution will support embebed pictures into the html code? IMAP returns the embebed pictures into the body as base64.

I doubt it tbh, but it’s worth simply trying it out. If not, it could be worth checking directly with the folks offering this API. I believe @Jon mentioned they’re pretty active when it comes to the development of their tool.

There might be other APIs out there as well, though I am not super familiar with this space.

Okey, thank you very much again @MutedJam!!

Yes, there are tons of alternatives - if what you have works, that’s great. Generally HTML→PDF conversion isn’t great, but if you can be sure the content won’t be very complex or dynamic you’ll be ok (or if you don’t care about the output).

If you’re finding this isn’t performing well enough, you could consider converting the HTML to markdown then using a simple library like pdfmake to produce a PDF.

Adding one thing for anyone who lands here from search, because inline images are the part that usually breaks.

  • Email HTML points at embedded images with cid: links (for example <img src="cid:image001.png@...">). A PDF renderer cannot resolve those, so the images come out blank.
  • If you use the Email Trigger (IMAP) node, check the Resolved format first. Its HTML output may already have inline images turned into data URIs.
  • If you still see cid: links, use a Code node to swap each one for a data URI (data:image/png;base64,...) built from the matching attachment. After that the HTML is self-contained.
  • Add <meta charset="utf-8"> and a simple @page { size: A4; margin: 12mm; } before converting, otherwise accents and margins can look off.
  • Then send the HTML to your renderer. Self-hosted Gotenberg works well: HTTP Request node, multipart form data, the HTML as a file field named index.html, POST to /forms/chromium/convert/html. The response is the PDF binary, ready for Drive or an attachment.

Wide newsletter-style emails (600 to 700px tables) also print better with a landscape page or a slight zoom on the body.

If you would rather not run Gotenberg yourself, thirds.ai takes the same HTML in one API call and returns the PDF: HTML to PDF API: convert HTML to PDF from your code | thirds.ai

(Disclosure: I’m connected to thirds.ai.)