[SalesForce] How to generate multi page PDF using Visualforce

I would like to generate a multi-page PDF using Salesforce \ Visualforce, so that I can print multiple records at once.

Each PDF page comes from a ContentVersion record.

I know I can generate a PDF using a visualforce page with the renderAs="pdf" attribute.

And I know I can control the page size using:

<style>
    @page {
          margin: 0;
          padding: 0;
          size: 6in 4in;
    }
</style>

But the challenge is generating each individual page, as they need to print in a batch, but each record should appear on a new page.

I thought I might be able to do something like:

<apex:repeat ... >
    <apex:include pageName="page1?contentVersionId=xxxxxxxx" />
</apex:repeat>

So I could loop over the ContentVersion records passing the ContentVersion.ID into the <apex:include but it doesn't seem to be possible to pass parameters into the <apex:include

Questions

  1. How can I generate a PDF with multiple pages where each page is a ContentVersion record?

Best Answer

Do you already have it generating with everything you need, but without the page-breaks? If so, then your problem is just one of how to do page-breaks in the fairly limited CSS that Visualforce's PDF renderer supports.

There's a good reference for print formatting in PDF here: https://www.antennahouse.com/CSSInfo/CSS-Page-Tutorial-en.pdf

Using something like that, you could write:

<apex:repeat... >
  <div style="page-break-after: always;">
    <!-- whatever to output the ContentVersion -->
  </div>
</apex:repeat>

Additional

Following discussion in the comments, the root of the problem is not page-breaks in a PDFS. It is joining together separate PDFs. That is not possible directly on the platform, so the solution is to make a callout to some compute-platform and go from there. If you know Apex, then you can probably at least get by in Java, so the Java version of http://www.pdfsharp.com/PDFsharp/ is a decent start.

Once you start to think about it, merging PDFs has lots of intricacies... What if the page sizes are different? What about margins? Table-of-contents? Footnotes? etc. So even with the library to help, it can still be moderately hairy.

Nonetheless, I've managed to do this on AWS Lambda. I guess you could do it on Google or Heroku, depending on what environment you're most used to.