Merging PDF documents

ColdFusion lets you merge PDF documents in the following ways:

  • Merge all of the PDF files in a specified directory.

  • Merge a comma-separated list of PDF files.

  • Merge individual PDF files, and pages within those files, explicitly, even if the source files are stored in different locations.

  • Merge the contents of a PDF variable generated by the cfdocument tag or a cfpdf tag

  • Create PDF packages

To merge the contents of a directory, use the merge action and specify the directory where the source PDF files are located, as the following example shows:

<cfpdf action="merge" directory="c:/BookFiles" destination="myBook.pdf" overwrite="yes">

By default, ColdFusion merges the source files in descending order by timestamp. You can control the order in which the PDF files are added to the book by setting the order and ascending attributes. The following code merges the files in ascending order according to the timestamp on the files:

<cfpdf action="merge" directory="c:/BookFiles" destination="myBook.pdf" order="name" 
    ascending="yes" overwrite="yes">

By default, ColdFusion continues the merge process even if it encounters a file that is not a valid PDF document in the specified directory. To override this setting, set the stopOnError attribute to yes, as the following example shows:

<cfpdf action="merge" directory="c:/BookFiles" destination="myBook.pdf" order="time" 
    ascending="yes" overwrite="yes" stopOnError="yes">

You can merge a comma-separated list of PDF files. To do this merge, specify the absolute path for each file, as the following example shows:

<cfpdf action="merge" 
    source="c:\coldfusion\wwwroot\lion\Chap1.pdf,c:\coldfusion\wwwroot\lion\Chap2.pdf" 
    destination="twoChaps.pdf" overwrite="yes">

For more control over which files are added to the merged document, use the cfpdfparam tag with the cfpdf tag. The cfpdfparam tag merges documents or pages from documents located in different directories into a single output file. When you use the cfpdfparam tag, the PDF files are added to the output file in the order they appear in the code. In the following example, the cover, title, and copyright pages are followed by the first five pages of the introduction, then all of the pages in Chapter 1, and then the first page followed by pages 80–95 in Chapter 2:

<!--- Use the cfdocument tag to create PDF content and write the output to a variable called coverPage.---> 
<cfdocument format="PDF" name="coverPage"> 
<html> 
<body> 
    <h1>Cover page</h1> 
    <p>Please review the enclosed document for technical accuracy and completeness.</p> 
</body> 
</html> 
</cfdocument> 
 
<!--- Use the cfpdf tag to merge the cover page generated in ColdFusion with pages from PDF     files in different locations. ---> 
<cfpdf action="merge" destination="myBook.pdf" overwrite="yes" keepBookmark="yes"> 
    <cfpdfparam source="coverPage"> 
    <cfpdfparam source="title.pdf"> 
    <cfpdfparam source="e:\legal\copyright.pdf"> 
    <cfpdfparam source="boilerplate\intro.pdf" pages="1-5"> 
    <cfpdfparam source="bookfiles\chap1.pdf"> 
    <cfpdfparam source="bookfiles\chap2.pdf" pages="1,80-95"> 
</cfpdf>

Because the keepbookmark attribute is set to yes, ColdFusion retains the bookmarks from the source documents in the output file.

Note: You cannot use the cfpdf tag to create bookmarks in a PDF document.

Creating PDF Portfolios

You can now create PDF packages using the package = "true" attribute with the merge action:

<cfpdf action="merge" package="yes" destination="./myBook/adobetest.pdf" overwrite="yes"> 
    <cfpdfparam source="./inputFiles/c.zip" > 
    <cfpdfparam source="./inputFiles/d.jpg" > 
    <cfpdfparam source="./inputFiles/a.pdf" > 
    <cfpdfparam source="./inputFiles/z.txt" > 
    <cfpdfparam source="./inputFiles/MSTribute.pps" > 
    <cfpdfparam source="./inputFiles/Test1.docx" > 
    <cfpdfparam source="./inputFiles/NewMovie.mp3" > 
    <cfpdfparam source="./inputFiles/testserver.air" > 
    <cfpdfparam source="./inputFiles/123.xml" > 
    <cfpdfparam source="./inputFiles/New_test_case.xls" > 
</cfpdf>