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
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');
System.debug('##encodedString:-'+encodedString);

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.

Thanks
-Tejas(+91 9663266726)
ezdhanhussainezdhanhussain
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,
-Tejas
ezdhanhussainezdhanhussain
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.

Thanks
Tejas
ezdhanhussainezdhanhussain
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';
            newAttachments.add(emailAtt);
            
            if (newAttachments.size() > 0) {
                try{
                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..
ItruItru

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).

SRKSRK
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
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_output_pdf_render_in_apex.htm
// 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 = 
            (PageReference)this.reportPagesIndex.get(this.selectedReport);
        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.setContentType('application/pdf');
        attachment.setFileName('AccountSummary-' + account.Name + '.pdf');
        attachment.setInline(false);
        attachment.setBody(reportPdf);
        message.setFileAttachments(new Messaging.EmailFileAttachment[]{ attachment });
        Messaging.sendEmail(new Messaging.SingleEmailMessage[]{ message });