function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SuriSuri 

Blob.toPdf Parsing Error: InvalidParameterValueException

Hi, I am constructing a HTML in a string, and converting it to PDF using the Blob.toPdf() method.

 

I was getting the following error:

 

FATAL_ERROR|System.InvalidParameterValueException: An error occurred while parsing the input string.

 

I checked it line by line, and I realize that this particular line causes this error. I am referencing a Static Resource in an Image tag, and this is causing the error.

 

Can anybody tell me why? and how to resolve this?

 

String htmlBody = '<img src="/resource/1347290849000/myCompany_Logo" />';

Blob.toPdf(htmlBody);

Best Answer chosen by Admin (Salesforce Developers) 
feeneyheadfeeneyhead

Siri -

 

I created a workaround!

 

I found an online PDF conversion API  (http://pdfcrowd.com) and I'm use their HMTL to PDF API -- it can process IMG tags without issue.  Pass in raw HTML and return a Blob of the PDF file:

 

public blob getPDFfromRawHTML(String username, String key, String rawHTML) {

          Blob pdfFile;
          String status = '';

           username = EncodingUtil.urlEncode(username,'UTF-8');
           key = EncodingUtil.urlEncode(key,'UTF-8');
           rawHTML = EncodingUtil.urlEncode(rawHTML,'UTF-8');

           Http ohttp = new Http();
           HttpRequest orequest = new HttpRequest();
           orequest.setEndpoint('http://pdfcrowd.com/api/pdf/convert/html/');
           orequest.setMethod('POST');
           orequest.setHeader('Content-Type','application/x-www-form-urlencoded');
           orequest.setBody('username=' + username + '&key=' + key + '&pdf_scaling_factor=1&src=' + rawHTML);

           try {
                 HttpResponse oresponse = ohttp.send(orequest);
                 pdfFile = oresponse.getBodyAsBlob();
           }catch(System.CalloutException e) { 
                 pdfFile = Blob.valueOf(rawHTML);
           }
          return pdfFile;
}

 

In another class, I'm processing an inbound email attachment and creating a PDF attachment from some information contained in the email, here's the code for the callout to the getPDFfromRawHTML function:

 

( code prior to this  parses the email message and creates pdfContent which at this point is just raw HTML code)

 

                   // Use PDFCrowd Web Service API
                    httpCallout ohttpCallout = new httpCallout();     // getPDFfromRawHTML is part of httpCallout class
                   pdfFile = ohttpCallout.getPDFfromRawHTML(PDFCROWD_USERNAME,PDFCROWD_KEY, pdfContent);

                   Attachment attachmentPDF = new Attachment();
                   AttachmentPDF.parentId = oWebForm[0].Id;        // just the custom object I'm attaching it to
                   AttachmentPDF.Name = attachmentName;
                   AttachmentPDF.body = pdfFile;
                   insert AttachmentPDF;

 

(and so on ...)

 

PDFCrowd is relatively inexpensive and you trial it to see if it fits your situation.

 

Hope this helps!

 

All Answers

SuriSuri

It does not work even for an img tag with a public URL. For example:

 

<img src="http://www.google.com/images/srpr/logo3w.png" />

feeneyheadfeeneyhead

Just curious ... did you ever get this to work?  I'm pulling an HTML doc into Salesforce and converting it to PDF using the Blob.toPDF function and having the same problem  -- it converts the doc but ignores the IMG tag.

SuriSuri

Nope! 

 

I tried contacting Salesforce Support too - and the only suggestion given was to use a VF Page with renderAs="pdf" and use Page.getContent().

 

(which I am well aware of and that's not what I'm looking for :P Page.getContent() does not work in Triggers or Email Services )

 

I guess there is no solution to this if there's a need for creating a PDF from a Trigger.

feeneyheadfeeneyhead

Siri -

 

I created a workaround!

 

I found an online PDF conversion API  (http://pdfcrowd.com) and I'm use their HMTL to PDF API -- it can process IMG tags without issue.  Pass in raw HTML and return a Blob of the PDF file:

 

public blob getPDFfromRawHTML(String username, String key, String rawHTML) {

          Blob pdfFile;
          String status = '';

           username = EncodingUtil.urlEncode(username,'UTF-8');
           key = EncodingUtil.urlEncode(key,'UTF-8');
           rawHTML = EncodingUtil.urlEncode(rawHTML,'UTF-8');

           Http ohttp = new Http();
           HttpRequest orequest = new HttpRequest();
           orequest.setEndpoint('http://pdfcrowd.com/api/pdf/convert/html/');
           orequest.setMethod('POST');
           orequest.setHeader('Content-Type','application/x-www-form-urlencoded');
           orequest.setBody('username=' + username + '&key=' + key + '&pdf_scaling_factor=1&src=' + rawHTML);

           try {
                 HttpResponse oresponse = ohttp.send(orequest);
                 pdfFile = oresponse.getBodyAsBlob();
           }catch(System.CalloutException e) { 
                 pdfFile = Blob.valueOf(rawHTML);
           }
          return pdfFile;
}

 

In another class, I'm processing an inbound email attachment and creating a PDF attachment from some information contained in the email, here's the code for the callout to the getPDFfromRawHTML function:

 

( code prior to this  parses the email message and creates pdfContent which at this point is just raw HTML code)

 

                   // Use PDFCrowd Web Service API
                    httpCallout ohttpCallout = new httpCallout();     // getPDFfromRawHTML is part of httpCallout class
                   pdfFile = ohttpCallout.getPDFfromRawHTML(PDFCROWD_USERNAME,PDFCROWD_KEY, pdfContent);

                   Attachment attachmentPDF = new Attachment();
                   AttachmentPDF.parentId = oWebForm[0].Id;        // just the custom object I'm attaching it to
                   AttachmentPDF.Name = attachmentName;
                   AttachmentPDF.body = pdfFile;
                   insert AttachmentPDF;

 

(and so on ...)

 

PDFCrowd is relatively inexpensive and you trial it to see if it fits your situation.

 

Hope this helps!

 

This was selected as the best answer
SuriSuri

Sounds Great!

 

Thanks a lot! :D

 

I guess this should work - as long as the callout is done just once per invocation, and the 10 callout limit in APEX is not hit.

 

Will be trying it out soon.

 

But the main issue here would be - that I don't want "sensitive" data from my salesforce instance being sent to an external service. :)

 

 

Vijay BharghavVijay Bharghav

hi Siri

I am also facing the error, can you list down how did you resolve the issue. As i have an image in the mail body. And when converting the incoming mail to PDF using Blob.toPDF. I am getting below exception.

 

System.InvalidParameterValueException: An error occurred while parsing the input string

 

Regards

Vijay Bharghav