[SalesForce] Batch printing Salesforce reports and Visualforce pages

I have three Salesforce reports and two Visualforce pages that render as PDF pages. I'd like to print all of them with one button press. In case it matters, the reports and Visualforce pages are parameterized. The content of the report is based on which site (custom object) it is being run for.

These are sufficiently important that we'd consider creating custom VF pages of the three standard reports if there was a way to print just VF/PDF pages in a batch.

Best Answer

A really interesting question!

I guess the answer depends on how similar the pages are. Do they use same standardController (or maybe only a custom controller?), will same parameter(s) be passed to them? Or would you have to modify the params because one needs id pointing to Account and another to Opportunity?

Assuming all pages need same param(s): /apex/pageName?id={!Opportunity.Id} and all use Oppty std. controller - you could merge them all in one master page with <apex:include>:

<apex:page standardController="Opportunity" readonly="true" renderAs="pdf">
    <apex:include pageName="page1" />
    <apex:include pageName="page2" />
    <apex:include pageName="{!$Page.page3}" /><!-- compile-time references are a good thing, they protect you when you try to delete the referenced page by mistake -->
</apex:page>

This tag doesn't allow passing parameters so I think you'd have to pass everything you need in the URL of the main page. You could also try without the same stdController but I think pages will start to complain about fields fetched without being queried for so you'd have to modify your constructors.

If you'll have parameter clashes (or you'll have more time on your hands) you could rename them or consider reworking these 3 pages into VF components. Components are even better than "include" because you can pass attributes to them (imagine extending of existing library of VF tags with your own). You could still have the original pages left, serving as thin wrappers over the newly created components.


As for grabbing the reports - I encourage you to try out the Analytics API, there also are some questions posted over here.

Long ago I've created an abomination of a component that's pretty much screenscraping the report page: Scheduled reports as attachment. Maybe you'll be able to reuse something from it (it doesn't allow param passing, only the report's Id; would be easy to extend though or remove the check).

But if you have time - at least try to do it the proper way and consider sharing with us ;) It's now my most upvoted answer... not sure if I should be proud but clearly there was huge demand for something like this!

P.S. VF page size is capped at 15 MB if I recall correctly...