0

I have used below visualforce code to display two pdf in a single page but in the result, I am getting two different PDFs with two sections with iframe.

Is it possible to combine these two PDFs in single PDF ?

mainPage

<apex:page renderAs="pdf">
    <apex:include pageName="POC_PDF1"/>
    <apex:include pageName="POC_PDF2"/>
</apex:page>

POC_PDF1

<apex:page >
    This is PDF1
</apex:page>

POC_PDF2

<apex:page Controller="xyzController">
    <iframe src="data:application/pdf;base64,{!pdf}" height="1000px" width="100%"/>
</apex:page>

Apex code :

public class xyzController {

   public xyzController(ApexPages.StandardController controller){}

   public ContentVersion conVersion {
       get {
           conVersion = [SELECT VersionData, FileType FROM ContentVersion WHERE ContentDocument.Title = 'PDF_081108' AND IsLatest = true];
           return conVersion;
       }
       private set;
   }

   public String pdf {
       get {
           return EncodingUtil.base64encode(conVersion.VersionData);
       }
   }
}
1

Added based on comments

You need not use iframe. Instead you can return the url based on ContentVerionId and render image/pdf:

pdf2 page:

<apex:page controller="poc" renderAs="pdf" >
    <div style="background:red;color:white;">
        This is PDF page 2
    </div>
    <img style="width:600px" src="{!pdfImgUrl}" />
</apex:page>

its apex controller:

public ContentVersion conVersion {
    get {
        conVersion = [SELECT Id, VersionData, FileType FROM ContentVersion WHERE ContentDocument.Title = 'PDF_081108' AND IsLatest = true];
        return conVersion;
    }
    private set;
}

public String pdfImgUrl {
    get {
        return '/sfc/servlet.shepherd/version/renditionDownload?rendition=ORIGINAL_Jpg&versionId='+conVersion.Id;
    }
}

OLD ANSWER

iframe will not work as it creates totally separate DOMs. You should use apex:include which will:

The entire page subtree is injected into the Visualforce DOM at the point of reference and the scope of the included page is maintained.

You can try with 2 options:

OPTION 1: Join 2 pages and then render as PDF

MainPage.vfp:

<apex:page renderAs="pdf" >
    <apex:include pageName="POC_pdf1"/>
    <apex:include pageName="POC_pdf2"/>
</apex:page>

POC_pdf1.vfp:

<apex:page  >
    <div style="background:blue;color:white;">
        This is PDF page 1
    </div>
</apex:page>

POC_pdf2.vfp:

<apex:page  >
    <div style="background:red;color:white;">
        This is PDF page 2
    </div>
</apex:page>

OPTION 2: Create 2 separate PDFs and join them in main page:

MainPage.vfp:

<apex:page >
    <apex:include pageName="POC_pdf1"/>
    <apex:include pageName="POC_pdf2"/>
</apex:page>

POC_pdf1.vfp:

<apex:page renderAs="pdf" >
    <div style="background:blue;color:white;">
        This is PDF page 1
    </div>
</apex:page>

POC_pdf2.vfp:

<apex:page renderAs="pdf" >
    <div style="background:red;color:white;">
        This is PDF page 2
    </div>
</apex:page>
1
1

For what it's worth, Visualforce's PDF rendering library is super-old and you are unlikely to be able to accomplish this goal of merging one dynamically created PDF with one static PDF. The underlying library (Flying Saucer) does have a merge functionality but it is not exposed in any way to Apex/VF so we can't access it.

Conga Composer (paid, uses an external system) does have the ability to run a document merge that stitches multiple PDFs together.

1
  • Thanks for your answer but I used an alternate option like displayed two PDFs in a single page with two different iframe. :)
    – Nail David
    Aug 29 '19 at 9:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.