cfdocument

Creates PDF or FlashPaper output from a text block containing CFML and HTML.

  <cfdocument format="PDF">html</cfdocument>

 cfDocument(format="PDF") { writeOutput(html); };

Attribute Reference

format string
Required

Specifies the report format.
Values:
  • PDF
  • FlashPaper

filename string

Specifies the fully qualified path name of a file to
contain the PDF or FlashPaper output. If you omit the
filename attribute, ColdFusion MX streams output to
the browser.

localurl boolean
Default: false

Specifies whether to retrieve image files directly
from the local drive:
* yes: ColdFusion retrieves image files directly from
the local drive rather than by using HTTP, HTTPS, or proxy.
* no: ColdFusion uses HTTP, HTTPS, or proxy to retrieve
image files even if the files are stored locally.
Values:
  • true
  • false

overwrite boolean
Default: false

Specifies whether ColdFusion MX overwrites an
existing file. Used in conjunction with filename.
Default is: false
Values:
  • true
  • false

name string

Specifies the name of an existing variable into which
the tag stores the PDF or FlashPaper output.

pagetype string
Default: A4

Specifies the page size into which ColdFusion
generates the report.
- legal: 8.5 inches x 14 inches
- letter: 8.5 inches x 11 inches
- A4: 8.27 inches x 11.69 inches
- A5: 5.81 inches x 8.25 inches
- B5: 9.81 inches x 13.88 inches
- Custom: Custom height and width.
If you specify custom, you must also specify the pageheight
and pagewidth attributes, can optionally specify margin
attributes, and can optionally specify whether the units
are inches or centimeters.
Values:
  • legal
  • letter
  • A4
  • A5
  • B5
  • custom

pageheight numeric

Specifies the page height in inches (default) or
centimeters. This attribute is only valid if
pagetype=custom. To specify page height in
centimeters, include the unit=cm attribute.

pagewidth numeric

Specifies the page width in inches (default) or
centimeters. This attribute is only valid if
pagetype=custom. To specify page width in
centimeters, include the unit=cm attribute.

orientation string
Default: portrait

Specifies the page orientation. Specify either of the
following:
- portrait (default)
- landscape
Values:
  • portrait
  • landscape

margintop numeric

Specifies the top margin in inches (default) or
centimeters. To specify top margin in centimeters,
include the unit=cm attribute.

marginbottom string

Specifies the bottom margin in inches (default) or
centimeters. To specify bottom margin in
centimeters, include the unit=cm attribute.

marginleft string

Specifies the left margin in inches (default) or
centimeters. To specify left margin in centimeters,
include the unit=cm attribute.

marginright string

Specifies the right margin in inches (default) or
centimeters. To specify right margin in centimeters,
include the unit=cm attribute.

unit string
Default: in

Specifies the default unit (inches or centimeters) for
pageheight, pagewidth, and margin attributes.
Values:
  • in
  • cm

encryption string
Default: none

Specifies whether the output is encrypted (format="PDF" only).
Default is: none
Values:
  • 128-bit
  • 40-bit
  • none

ownerpassword string

Specifies an owner password (format="PDF" only).

userpassword string

Specifies a user password (format="PDF" only).

permissions string

Specifies one or more permissions (format="PDF" only).
Separate multiple permissions with a comma.
Values:
  • AllowPrinting,AllowCopy,AllowScreenReaders
  • AllowPrinting
  • AllowModifyContents
  • AllowCopy
  • AllowModifyAnnotations
  • AllowFillIn
  • AllowScreenReaders
  • AllowAssembly
  • AllowDegradedPrinting
  • AllowPrinting,AllowModifyContents,AllowCopy,AllowModifyAnnotations,AllowFillIn,AllowScreenReaders,AllowAssembly,AllowDegradedPrinting

fontembed boolean
Default: true

Specifies whether ColdFusion embeds fonts in the output.
Specify one of the following:
- true: Embed fonts
- false: Do not embed fonts.
Selective: Embed all fonts except Java fonts and core fonts.
Values:
  • true
  • false

backgroundvisible boolean
Default: false

Specifies whether the background prints when the
user prints the document:
- yes: include the background when printing.
- no: do not include the background when printing.
Values:
  • true
  • false

scale numeric

Specifies a scale factor as a percentage. Use this
option to reduce the size of the HTML output so that
it fits on that paper. Specify a number less than 100.
Values:
  • 100
  • 90
  • 80
  • 70
  • 60
  • 50
  • 40
  • 30
  • 20
  • 10

authpassword string

Password sent to the target URL for Basic Authentication.
Combined with username to form a base64 encoded string that is passed in the Authenticate header.
Does not provide support for Integrated Windows, NTLM, or Kerebos authentication.

authuser string

User name sent to the target URL for Basic Authentication.
Combined with password to form a base64 encoded string that is passed in the Authenticate header.
Does not provide support for Integrated Windows, NTLM, or Kerebos authentication.

bookmark boolean
Default: false

Specifies whether bookmarks are created in the document:
* yes: creates bookmarks.
* no: does not create bookmarks.
Values:
  • true
  • false

mimetype string
Default: text/html

MIME type of the source document
Values:
  • text/html
  • text/plain
  • application/xml
  • image/bmp
  • image/jpeg
  • image/png
  • image/gif

proxypassword string

Password required by the proxy server.

proxyuser string

User name to provide to the proxy server.

saveasname string

(format="PDF" only) The filename that appears in the SaveAs dialog when a user saves a PDF file written to the browser.

src string

URL or the relative path to the web root. You cannot specify both the src and srcfile attributes.
The file must be in a browser-writable format such as, HTML, HTM, BMP, PNG, and so on.

srcfile string

Absolute path of a file that is on the server. You cannot specify both the src and srcfile attributes.
The file must be in a browser-writable format such as, HTML, HTM, BMP, PNG, and so on.

useragent string

Text to put in the HTTP User-Agent request header field. Used to identify the request client software.

proxyhost string

IP address or server name for proxy host.

proxyport string

port of the proxy host.

tagged boolean
Default: false

Determines if PDF are created by using special tags also known as Tagged PDF
Values:
  • true
  • false

pdfa boolean
Default: false

Creates a PDF of type PDF/A-1 (ISO 19005-1:2005)
Values:
  • true
  • false

formFields boolean
Default: true

Specifies whether form fields are exported as widgets or only their fixed print representation is exported.
Values:
  • true
  • false

formsType string
Default: FDF

Specifies the submitted format of a PDF form.
Values:
  • FDF
  • PDF
  • HTML
  • XML

permissionsPassword string

This is the password that allows the user to access some permissions restricted if some permissions need to be restricted. The permissions are defined in "permissions" attribute

openPassword string

If the source document is protected specify using this attribute.

Examples
Sample code using the cfdocument tag

Use 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.

Fork me on GitHub