Skip to content

Instantly share code, notes, and snippets.

@learncfinaweek
Created November 20, 2012 21:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save learncfinaweek/4121345 to your computer and use it in GitHub Desktop.
Save learncfinaweek/4121345 to your computer and use it in GitHub Desktop.
Document Handling - Hands On 24

In this next hands on, you are going to add functionality that will allow you to export a blog post in PDF format. We will also add a watermark to the PDF.

Tags Used: <cfparam>, <cfset>, <cfif>, <cfdocument>, <cfdocumentitem>, <cfpdf>, <cfcontent>

Functions Used: EntityLoad, isNull, toBinary

  1. Create a new file in the /www/ folder called ExportToPDF.cfm.
  2. Open the /www/ExportToPDF.cfm file in your code editor.
  3. On the first line of the file, create a <cfparam> tag with the following attributes:
    • name: url.id
    • default: 0
  4. Next, load in the blog post so you can get the data needed. To do this, create a <cfset> that does an entityLoad call requesting a blogPost with the ID of 'url.id' and sets it into a blogPost variable by adding the following line of code:
    <cfset blogPost = EntityLoad('blogPost',url.id,true) />	
    
  5. Before proceeding with the PDF generation, make sure that the ID that was passed was for a valid blog post by checking to make sure the blogPost object does not have a null value. To do this, create a <cfif> statement and use the isNull function on the blogPost variable. Your code should look similar to this:
    <cfparam name="url.id" default="0"  />
    <cfset blogPost = EntityLoad('blogPost',url.id,true) />
    

    <cfif !isNull(blogPost)>

    </cfif>

  6. Inside of the <cfif> block, create a <cfdocument> tag with the following attribute:
    • format: PDF
  7. Add a closing </cfdocument> tag.
  8. Inside the <cfdocument> tags add the following code:

    <cfoutput>
    <h1>
    	#blogPost.title#
    </h1>
    <p>
    	<strong>Date Posted</strong>: #dateformat(blogPost.datePosted,'mm/dd/yyyy')#
    </p>
    <p>	
    #blogPost.body#
    </p>		
    </cfoutput>
    
    	</li>
    	<li>
    		Open up the <span class="code">/www/blog.cfm</span> page in your browser, navigate to a blog post, and click on 'Export To PDF'. You should see a PDF of the blog post.
    	</li>
    	<li>
    		Next, add a header to the PDF which states it came from the website. Go back to the <span class="code">ExportToPDF.cfm</span> file in your code editor and add a <span class="code">&lt;cfdocumentitem></span> tag inside the <span class="code">&lt;cfdocument></span> tag that has the following property:
    		<ul>
    			<li>
    				<strong>type</strong>: header
    			</li>
    		</ul>		
    	</li>
    	<li>
    		Inside the <span class="code">&lt;cfdocumentitem></span> add the following code:
    
    <h1 style="text-align:center;">Generated from our website</h1>
    
    	</li>
    	<li>
    		Your &lt;cfdocument> code should looks similar to this:
    
    <cfdocument format="PDF">
    	<cfdocumentitem type="header">
    		<h1 style="text-align:center;">Generated from our website</h1>
    	</cfdocumentitem>	
    	<cfoutput>
    	<h1>
    		#blogPost.title#
    	</h1>
    	<p>
    		<strong>Date Posted</strong>: #dateformat(blogPost.datePosted,'mm/dd/yyyy')#
    	</p>
    	<p>
    	#blogPost.body#
    	</p>		
    	</cfoutput>	
    </cfdocument>
    
    	</li>
    	<li>
    		Go back to the <span class="code">ExportToPDF.cfm</span> file in your browser and refresh. You should now see the title displayed in the PDF.
    	</li>
    	<li>
    		Now add a watermark to the PDF. First, save the generated PDF to a variable rather than having it automatically output on the page. You can do this by updating the <span class="code">&lt;cfdocument</span>> tag and adding the following property:
    		<ul>
    			<li>
    				<strong>name</strong>:  myGeneratedPDF
    			</li>
    		</ul>
    	</li>
    	<li>
    		If you were to re-run the <span class="code">ExportToPDF.cfm</span> file in your browser, you would no longer see the PDF as it is now stored in a variable. Now, create a <span class="code">&lt;cfpdf></span> tag that will allow us to add the watermark. After the closing <span class="code">&lt;/cfdocument></span> tag, create a <span class="code">&lt;cfpdf></span> tag with the following attributes:
    		<ul>
    			<li>
    				<strong>action</strong>: addWatermark
    			</li>
    			<li>
    				<strong>source</strong>: myGeneratedPDF
    			</li>
    			<li>
    				<strong>image</strong>: assets/images/watermark.jpeg
    			</li>
    			<li>
    				<strong>foreground</strong>: yes
    			</li>
    			<li>
    				<strong>overwrite</strong>: yes
    			</li>
    		</ul>
    	</li>
    	<li>
    		Once again, if you were to run this file, you would still see no output; you need to manually tell ColdFusion to display the PDF. This is done with the <span class="code">&lt;cfcontent></span> tag. After the <span class="code">&lt;cfpdf></span> tag, create a <span class="code">&lt;cfcontent></span> tag with the following attributes:
    		<ul>
    			<li>
    				<strong>variable</strong>: #toBinary(myGeneratedPDF)#
    			</li>
    			<li>
    				<strong>type</strong>: application/pdf
    			</li>
    		</ul>
    	</li>
    	<li>
    		Reload the <span class="code">ExportToPDF.cfm</span> file in your browser and you should now see your PDF with a large W in the middle. The W is the Watermark.
    	</li>
    </ol>
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment