Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

Cheers

share|improve this question
    
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 Jan 23 '13 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. –  Michael Welburn Jan 23 '13 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. –  Prafulla Patil Jun 2 '13 at 17:20
add comment

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.

share|improve this answer
add comment

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?
// ...
if(restRequestNeeded){
    HttpRequest req = new HttpRequest();
    req.setEndpoint(requestUrl);
    req.setMethod('GET');
    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...

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.