Creates PDF or FlashPaper output from a text block containing CFML and HTML.
<cfdocument format="PDF">html</cfdocument>
cfDocument(format="PDF") { writeOutput(html); };
PDFFlashPaperfalsetruefalsefalsetruefalseA4legalletterA4A5B5customportraitportraitlandscapeinincmnone128-bit40-bitnoneAllowPrinting,AllowCopy,AllowScreenReadersAllowPrintingAllowModifyContentsAllowCopyAllowModifyAnnotationsAllowFillInAllowScreenReadersAllowAssemblyAllowDegradedPrintingAllowPrinting,AllowModifyContents,AllowCopy,AllowModifyAnnotations,AllowFillIn,AllowScreenReaders,AllowAssembly,AllowDegradedPrintingtruetruefalsefalsetruefalse100908070605040302010falsetruefalsetext/htmltext/htmltext/plainapplication/xmlimage/bmpimage/jpegimage/pngimage/giffalsetruefalsefalsetruefalsetruetruefalseFDFFDFPDFHTMLXMLUse the cfdocument tag to generate a file on the server either to save to a folder or to attach to an email
<cfdocument format="pdf" filename="#getTempDirectory()#mydoc.pdf">
<h1>Hello ColdFusion</h1>
<p>This is <strong>PDF</strong> example document.</p>
<p>Generated at: <cfoutput>#TimeFormat(Now())# on #DateFormat(Now())#</cfoutput></p>
</cfdocument>
Expected Result: A saved Hello ColdFusion PDF to a temporary directory
PDF is sent back to the browser instead of saving to a file. The cfheader tag is used to suggest a file name to the browser. The cfcontent tag is used to set the Content-Type response header (that could also be done using cfheader) which tells the browser that it is a PDF file.
<cfcontent type="application/pdf">
<cfheader name="Content-Disposition" value="attachment;filename=example.pdf">
<cfdocument format="pdf">
<h1>Hello ColdFusion</h1>
<p>This is <strong>PDF</strong> example document.</p>
<p>Generated at: <cfoutput>#TimeFormat(Now())# on #DateFormat(Now())#</cfoutput></p>
</cfdocument>
This is a script representation of the the same code above.
Note that the writeoutput is indeed necessary for any body content in a script-based cfdocument--even if no CFML variables or expressions exist in the string. (This is true also for other script-based tags that may have bodies, including cfdocumentitem and cfdocumentsection, as well as cfhtmltopdf, cfsavecontent, etc.)
<cfscript>
cfcontent( type="application/pdf" );
cfheader( name="Content-Disposition", value="attachment;filename=example.pdf" );
cfdocument (format="pdf"){
writeoutput("
<h1>Hello ColdFusion</h1>
<p>This is <strong>PDF</strong> example document.</p>
<p>Generated at: <cfoutput>#TimeFormat(Now())# on #DateFormat(Now())#</cfoutput></p>
");
}
</cfscript>
must use evalatprint="true" in order to get current page numbering on the header/footer sections
<cfdocument format="pdf">
<cfdocumentitem type="header" evalatprint="true" >
<cfif (cfdocument.currentpagenumber mod 2) is 0>
<cfoutput>#cfdocument.totalpagecount#</cfoutput>
<cfelse>
<cfoutput>#cfdocument.currentpagenumber#</cfoutput>
</cfif>
<cfoutput>cfdocument.currentpagenumber :#cfdocument.currentpagenumber#</cfoutput>
<cfoutput>cfdocument.totalpagecount :#cfdocument.totalpagecount#</cfoutput>
<cfoutput>cfdocument.totalsectionpagecount :#cfdocument.totalsectionpagecount#</cfoutput>
<cfoutput>cfdocument.currentsectionpagenumber :#cfdocument.currentsectionpagenumber#</cfoutput>
</cfdocumentitem>
<cfdocumentitem type="footer" evalatprint="true" >
<cfif ! (cfdocument.currentpagenumber mod 2) is 0>
<cfoutput>if#cfdocument.totalpagecount#</cfoutput>
<cfelse>
<cfoutput>else#cfdocument.currentpagenumber#</cfoutput>
</cfif>
</cfdocumentitem>
<cfdocumentsection >Example Text
</cfdocumentsection>
</cfdocument>
Expected Result: A PDF with Header & Footer elements
Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.