+ Start a Discussion
cedgcedg 

generation PDF Blob is not a valid UTF-8 string error

Hi,

 

I want to generate a PDF and attach it to an email, based on the Opportunity object. So far, it shouldn't be difficult...

 

So i've a VF page with the content , rendered as pdf and its controller. If i launch the page in a browser, it's ok.

 

I've another class (containing the method launched by an action button on the Opportunity layout) to get the content of the PDF and attach it to the email.

Before attaching it, i want to check if there is no error in the PDF, so I call the body.toString() method to do that  (body is the blob). 

Then i get the error "System.StringException: BLOB is not a valid UTF-8 string", while  I used this method many times in the past without problem for other PDF generation.

 

        PageReference pdf;
        pdf = Page.QuoteTemplate;
        pdf.getParameters().put('id',opp.Id);
Blob body; try { body = pdf.getContent(); } catch (VisualforceException e) { body = Blob.valueOf('Some Text ... ERROR_COMPANYADDRESSNOTFOUND ... ERROR_COMPANYNOTFOUND'); } if (body.toString().contains('ERROR_')){ return 'warning'; }

 

I searched for information, i find that examples of this error when trying to read the content of a document which is not in UTF8, but in this case i'm starting from a VF page and when i try directly to look at the page, it's ok.

 

One solution often mentionned is to use the  EncodingUtil.base64Encode(body);

but then I cannot compare the content to find the "ERROR_..." (i think)

 

 

Anybody has an idea ?

 

Many thanks

Ced

 

Vinita_SFDCVinita_SFDC

Hi,

 

Try converting Blob to string linke this and then apply contains method:

 

Blob b;
String b64;
String strUrlUTF8;
String strOriginal;

strOriginal = 'Joe’s Café & Bar';
System.debug('String strOriginal: [' + strOriginal + ']');

strUrlUTF8 = EncodingUtil.urlEncode(strOriginal, 'UTF-8');
System.debug('String strUrlUTF8: [' + strUrlUTF8 + ']');

b = Blob.valueOf(strUrlUTF8);
System.debug('Blob b: [' + b.toString() + ']');

b64 = EncodingUtil.base64Encode(b);
System.debug('String b64: [' + b64 + ']');

b = EncodingUtil.base64Decode(b64);
System.debug('Blob b: [' + b.toString() + ']');

strUrlUTF8 = b.toString();
System.debug('String strUrlUTF8: [' + strUrlUTF8 + ']');

strOriginal = EncodingUtil.urlDecode(strUrlUTF8, 'UTF-8');
System.debug('String strOriginal: [' + strOriginal + ']');

 

 

cedgcedg

Hi,

I had seen this code, and i tested it again but it's not working.

The "b.toString()" makes an error.

Ajay_SFDCAjay_SFDC

Hi there ,

 

I have used following code in one of my project :

 

PageReference pdf = Page.GPS_Quote;

pdf.getParameters().put('id',quoteId);
Attachment attach = new Attachment();
QuoteDocument quoteAttach = new QuoteDocument();

Blob body;
try {
body = pdf.getContentAsPDF();
}
catch (VisualforceException e) {
body = Blob.valueOf('No content in PDF');
}
quoteAttach.Document = body;
quoteAttach.QuoteId = quoteId;
try{
insert quoteAttach;
}
catch(Exception exp){
system.debug('Exception: '+exp);
}

 

Let me know if it helps you !!