+ Start a Discussion
Tejas DJTejas DJ 

Blob.toPDF() dosen't support Chinese characters

Hello Everyone,

I have observed a wired behavior with "Blob.toPDF(<string>)" Salesforce Apex API's , this method strips off Chinese characters and there is no option to set character encoding, please find below the sample code.

String targetString = 'Before - 測試中文字 - After';

Attachment attachmentPDF = new Attachment();
attachmentPDF.parentId = '<parent record id>';
attachmentPDF.Name = 'Test' + '.pdf';
attachmentPDF.ContentType = 'application/pdf; charset=UTF-8';
attachmentPDF.body = Blob.toPdf(targetString); //This creates the PDF content
insert attachmentPDF;

I have also tried encoding Chinese character content to UTF-8 but this displays all the encoded characters in PDF,

String targetString = 'Before - 測試中文字 - After';
String encodedString = EncodingUtil.urlEncode(targetString,'UTF-8');

Attachment attachmentPDF = new Attachment();
attachmentPDF.parentId = '<parent record id>';
attachmentPDF.Name = 'Test' + '.pdf';
attachmentPDF.ContentType = 'application/pdf; charset=UTF-8';
attachmentPDF.body = Blob.toPdf(encodedString); //This creates the PDF content
insert attachmentPDF;

Please help.

-Tejas(+91 9663266726)
use Blob.valueof(targetString) instead of Blob.toPdf();

If solved mark it as solution.. So that it can help others
Tejas DJTejas DJ
Hello ezdhanhussain,

Thanks for the reply, I tried Blob.valueof(targetString) this dosen't work.

Also I am specifically using Blob.toPdf(targetString) method because I want to create a well formatted PDF, I.e If you wish to create a PDF with all the tables and images we need to use Blob.toPDF() method which can also accept HTML content as input parameter and create a well formatted PDF.

Thanks & Regards,
Hi tejas I had a same requirement working with chinese characters. Blob.valueof solved my issue. It also accepted html content, where as blob.topdf showed me styles code in the pdf document
Tejas DJTejas DJ
Hello Ezdhanhussain,

Can you please try creating a PDF document with the above code and can you please suggest any corrections required?

This issue has become very critical for me.

I have tried Blob.valueof but it is creating a corrupted pdf document for me.

HI Tejas Here is the code i had Worked with
list<attachment> newAttachments = new list<attachment>();
            attachment emailAtt = new Attachment();
            emailAtt.ParentId = em.id;
            emailAtt.Name = 'Emailpdf';
            emailAtt.Body = blob.valueof(mergedTemplate);
            emailAtt.contenttype = 'pdf';
            if (newAttachments.size() > 0) {
                insert newAttachments;
                system.debug('attachments insert in email successfull::::::::::::::');
And here is the preview how it looks in attachments after uploadingUser-added image

Hope it may resolve your issue..

Hi @Tejas DJ - 6 years after and I'm facing this issue also.

Did you find a solution for it? (Blob.valueOf didnt work for me also).

I am not very sure of issues here you can create a VF page and then get that VF page as a PDF in apex class

take a look at this URL it may help
// Create email
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setToAddresses(new String[]{ this.recipientEmail });
        message.setSubject('Account summary for ' + account.Name);
        message.setHtmlBody('Here\'s a summary for the ' + account.Name + ' account.');
        // Create PDF
        PageReference reportPage = 
        reportPage.getParameters().put('id', this.selectedAccount);
        Blob reportPdf;
        try {
            reportPdf = reportPage.getContentAsPDF();
        catch (Exception e) {
            reportPdf = Blob.valueOf(e.getMessage());
        // Attach PDF to email and send
        Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
        attachment.setFileName('AccountSummary-' + account.Name + '.pdf');
        message.setFileAttachments(new Messaging.EmailFileAttachment[]{ attachment });
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ message });