3

We are using report namespace to extract the Salesforce report data into csv using APEX. (There are no vf pages). Is there any way to write code in APEX to export the Salesforce report results into a pdf format.I am able to export the results only in CSV format.

In the below example, I tried to export a existing salesforce report data into csv, similar to this I am looking to export the results into a pdf format. (without using any apps or VF page render as option)

   ApexPages.PageReference report = new ApexPages.PageReference('/reportid?csv=1'); 

    Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
    attachment.setFileName('report.csv');
    attachment.setBody(report.getContent());
    attachment.setContentType('text/csv');
    System.debug('attachement==>'+ attachment);
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    message.setFileAttachments(new Messaging.EmailFileAttachment[] { attachment } );
    message.setSubject('Report');
    message.setPlainTextBody('The report is attached.');
    message.setToAddresses( new String[] { '[email protected]' } );
    Messaging.sendEmail( new Messaging.SingleEmailMessage[] { message } );
0

1 Answer 1

6

Firstly, you might want to vote for the idea Ability to Print Reports to PDF. Maybe with an additional comment that you want it export to PDF in the same way that it can to CSV.


You can use Blob.toPDF(stringToConvert) to convert an HTML string into a PDF without a corresponding Visualforce page.

Example for the documentation:

String pdfContent = 'This is a test string';
Account a = new account(name = 'test');
insert a;
Attachment attachmentPDF = new Attachment();
attachmentPdf.parentId = a.id;
attachmentPdf.name = account.name + '.pdf';
attachmentPdf.body = blob.toPDF(pdfContent);
insert attachmentPDF;

You could combine this with a conversion from the CSV to an HTML string to generate a PDF.


Here is something that seemed so promising, but didn't work out in the end.

From Can report data be accessed programatically? we know that when a report is exported in XLS format it is actually HTML that Excel will interpret as a spreadsheet.

This, combined with the Blob.toPdf() method accepting HTML would provide a quick shorthand to create the PDF for a report in a few lines of Apex. Something like:

// Note that this might not work if the org is using MyDomain
// Extra Query String paramters to get the HTML for the report.
ApexPages.PageReference report = new ApexPages.PageReference('/00O40000002Kwok?export=1&enc=UTF-8&xf=xls'); 

Account a = new account(name = 'test');
insert a;
Attachment attachmentPDF = new Attachment();
attachmentPdf.parentId = a.id;
attachmentPdf.name = account.name + '.pdf';
// Pull the report html content and immediately convert it into a PDF.
attachmentPdf.body = System.Blob.toPdf(report.getContent().toString());
insert attachmentPDF;

The good news, it ran without an error and inserted the new PDF as an Attachment!

The bad news, the generated PDF was garbled.

enter image description here


Tried it again with a much simpler report. Seems somewhat promising if you don't have many columns.

Before: enter image description here

Output: enter image description here

2
  • 1
    Rorschach PDF! Fun to look at, anyway.
    – Adrian Larson
    Aug 16, 2016 at 22:01
  • The data they need is in there somewhere I'm sure. Foiled by FlyingSaucer again. Plan B is to run the CSV through a parser and then create the HTML from that. Starting to become a Heath Robinson Solution. Aug 16, 2016 at 22:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .