Search code examples
pdfcoldfusioncoldfusion-9

Can I include another PDF in CFDOCUMENT?


I have a PDF generated in Coldfusion. I would like to add pages from another PDF sitting in the folder. I've checked cfpdf, but it doesn't seem to be the way to go. Is there a way to do this?

<cfdocument format="PDF" fontEmbed = "yes" pageType="A4" margintop="0.2" marginbottom="0.1" marginleft="0.2" marginright="0.2">
        <cfinclude template="header.inc">
        ... content ....
        pages 2nd PDF should be here
</cfdocument>

Solution

  • Here in its simplest form is how to append an existing PDF on disk to a dynamically created PDF and serve it to the browser without writing anything new to the physical or virtual file system.

    <!--- Create a new PDF and store it in a variable called newPdf --->
    <cfdocument format="PDF" name="newPdf">
      Content of new PDF here. Content of an existing PDF to be appended below.
    </cfdocument>
    
    <!--- Append to the new PDF the contents of the existing PDF located in the same folder as this script and store it in a variable called mergedPdf --->
    <cfpdf action="merge" name="mergedPdf">
        <cfpdfparam source="newPdf">
        <cfpdfparam source="existing.pdf">
    </cfpdf>
    
    <!--- Output the merged PDF to the browser --->
    <cfcontent type="application/pdf" variable="#ToBinary( mergedPdf )#" reset="true">
    

    (You might want to add a <cfheader> to suggest how the PDF should be handled by the browser, ie inline or attachment.)