Flattening forms involves removing the interactivity from the form fields. This is useful for displaying form data and presenting it without allowing it to be altered. Use the write action to flatten PDF forms, as the following example shows:
<cfpdf action="write" flatten="yes" source="taxForms\f1040.pdf" destination="taxforms/flatForm.pdf" overwrite="yes"> a href="http://localhost:8500/Lion/taxforms/flatForm.pdf">1040 form</a>
For efficient access of PDF files over the web, linearize PDF documents. A linearized PDF file is structured in a way that displays the first page of the PDF file in the browser before the entire file is downloaded from the web server. This means that linear PDF documents open almost instantly.
To linearize PDF documents, specify the saveOption attribute of the write action. The following example saves the output file in linear format:
<cfpdf action="write" saveOption="linear" source="myBook.pdf" destination="fastBook.pdf" overwrite="yes">
Use the thumbnail action to generate thumbnail images from PDF pages. If you specify only the source attribute with the thumbnail action, ColdFusion automatically creates a directory relative to the CFM page called thumbnails where it stores a generated JPEG image for each page in the document. The filenames are in the following format:
PDFdocumentName_page_n.JPG
For example, assume that the source file in the following example has 100 pages:
<cfpdf action="thumbnail" source="myBook.pdf">
ColdFusion generates the following files and stores them in the thumbnails directory:
myBook_page_1.jpg myBook_page_2.jpg myBook_page_3.jpg ... myBook_page_100.jpg
If you specify a destination, ColdFusion does not create the thumbnails directory and stores the files in the specified directory instead. The following code generates a thumbnail image called myBook_page_1.jpg from the first page of myBook.pdf and stores it in a directory called images, which is relative to the CFM page:
<cfpdf action="thumbnail" source="myBook.pdf" pages="1" destination="images">
You change the prefix for the thumbnail filename and the change image file format to PNG or TIFF by specifying the imagePrefix and format attributes. The following code generates a file called TOC_page_2.PNG from the second page of myBook.pdf:
<cfpdf action="thumbnail" source="myBook.pdf" pages="2" imagePrefix="TOC" format="PNG" destination="images">
The following code generates thumbnails from a range of pages and changes the image background to transparent (the default is opaque):
<cfpdf action="thumbnail" source="myBook.pdf" pages="1-10,15,8-16,59" transparent="yes" destination="\myBook\subset" imagePrefix="abridged">
For an example of how to generate thumbnail images and link them to pages in the source PDF document, see the cfpdf tag in the CFML Reference.
You can use the Duplicate function to clone PDF variables, which is an efficient way to create different versions of a PDF document from a single source file. For example, you can customize PDF output based on your audience by creating clones of a PDF variable and performing different actions on each clone. The following example shows how to create a clone of a PDF document in memory, and create one version of the document with a watermark and another version of the document where permissions are restricted:
<cfset filename="coldfusion.pdf"> <!--- This code reads a PDF document into a PDF variable called pdfVar1. ---> <cfpdf action="read" source="#filename#" name="pdfVar1"> <!--- This code uses the Duplicate function to create a clone of pdfVar1 called pdfVar2. ---> <cfset pdfVar2=Duplicate(pdfVar1)> <!--- This code creates a watermarked version of the source PDF document from the pdfVar1 variable. ---> <cfpdf action="addwatermark" source="pdfVar1" rotation="45" image="watermark.jpg" destination="watermark_coldfusion.pdf" overwrite="yes"> <!--- This code creates a protected version of the source PDF document from the pdfVar2 variable. ---> <cfpdf action=protect source="pdfVar2" encrypt="RC4_128" permissions="none" newownerpassword="owner1" destination="restricted_coldfusion.pdf" overwrite="yes">