I am trying to convert an HTML email that is sent to an Email Service into a PDF file. Some of the options I have explored:

attachment.fileName = fileName + '.pdf';
attachment.mimeTypeSubType = 'application/pdf';
attachment.body = Blob.toPdf(attachmentBody);

This works great with text and some css, but when I send a rich HTML email with images and lots of styling it fails and generates an error - System.InvalidParameterValueException: An error occurred while parsing the input string.

The second option, I tried to pass in the HTML from the email to a Visualforce page where it was rendered as a PDF and used as an attachment, but there are limitations in the Email Service that you can't use visualforce pages and the HTML markup from the email raised validation exceptions where HTML tags were not closed properly.

I know there are lots of paid HTMLtoPDF services out there on the web with APIs that you can call, but I'd like to explore any alternate Salesforce options before resorting to this or building my own HTMLtoPDF web app.


  • getContentAsPDF() in Apex or RenderAs="PDF" in Visualforce is what you're looking for - but as you note, you can't use these in asynchronous methods.
    – joshbirk
    Commented Jan 23, 2013 at 15:37
  • Have you tried validating the HTML you are attempting to render as a PDF? If it has some invalid HTML markup or syntax, it is possible the conversion would fail to parse it. Commented Jan 23, 2013 at 17:02
  • Here is the complete code for PDF generation, in case you want to use it - iwritecrappycode.wordpress.com/2013/04/02/dynamic-pdf-generator. Thanks to Kenji for sharing this on his blog. Commented Jun 2, 2013 at 17:20

2 Answers 2


I would parse the blob into a string and strip certain kinds of html tags for the following reasons :

  1. PDF rendering doesn’t support images encoded in the data: URI scheme format
  2. Don't use components that rely on JavaScript
  3. Don't use components that depend on Salesforce stylesheets
  4. tags that rely on JS or an external URI are going to confuse the PDF generator
  5. a tag that refers to an external stylesheet that's not a static resource are also going to be problematic.

I suggest you look at Best Practices for Rendering PDF's in the APEX documentation and much of my reasoning will become readily apparent. Creating PDF's from known input data can be tricky enough as it is. Rendering them from other sources which you have no control over would be totally hit or miss without first stripping extraneous tags or links to embedded external content.


You could try sanitizing the html by loading it up to the DOM object but probably it'll fail all the same on misplaced tags...

Can you try cheating around this limitation with REST callout to same org? ;) Similar to like I did the fallback plan in Scheduled reports as attachment

Boolean restRequestNeeded = System.isFuture() || System.isScheduled(); // System.isBatch() too?
// ...
    HttpRequest req = new HttpRequest();
    req.setHeader('Cookie','sid=' + UserInfo.getSessionId());
    output = new Http().send(req).getBody();

Your url would be a VF page that displays the content or maybe directly "view HTML" address...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .