cfdocument タグを使用した PDF および FlashPaper 出力の作成

注意:

アドビでは 2020 年 12 月 31 日をもって Flash Player のサポートを終了します。2021 年 1 月 12 日以降は Flash Player で Flash コンテンツが動作しなくなるので、システムを保護するためにすべてのユーザーが Flash Player を直ちにアンインストールすることを強くお勧めします。 

詳しくは、Adobe Flash Player サポート終了情報ページを参照してください。

cfdocument タグの開始タグと終了タグの間にあるデータはすべて PDF または FlashPaper 出力形式に変換され、ブラウザーに返されるか、ファイルに保存されます。このタグを次の例のように使用すると、HTML を簡単に印刷可能出力に変換できます。

<p>This is a document rendered by the cfdocument tag.</p>
</cfdocument>

cfdocument タグでは、次の例外を除き、すべての HTML タグと CFML タグをサポートしています。

  • cfchart
  • Flash Player で表示されるコンテンツを生成するタグ
  • form、cfform、cfapplet などのインタラクティブタグ
  • 要素または要素の位置を動的に変更する JavaScript
    さらに、cfdocument タグの開始タグと終了タグの間にある HTML コードは正しい形式になっている必要があります。つまり、それぞれの開始タグに対応する終了タグが存在し、ブロックレベルの要素が適切にネストされている必要があります。

注意:<cfdocument> タグと </cfdocument> タグのペアの外側にある HTML や CFML は返されません。

HTML および CFML からの基本的なレポートの作成

HTML ベースのレポートを PDF または FlashPaper 出力に変換するには、cfdocument タグの開始タグと終了タグの間に HTML を記述し、必要に応じて cfdocument の属性を指定して次の項目をカスタマイズします。

  • ページサイズ
  • ページの向き
  • マージン
  • 暗号化 (PDF のみ)
  • ユーザーパスワードとオーナーパスワード(PDF のみ)
  • 権限(PDF のみ)
    これらのオプションについて詳しくは、『CFML リファレンス』の cfdocument タグに関する説明を参照してください。

注意:レポートにフォントを埋め込むと、ブラウザーやプラットフォームの違いによる表示のばらつきを解消できます。フォントの埋め込みに関する詳細については、簡単なレポートの作成を参照してください。

次の例では、cfoutput タグを使用してクエリを反復処理し、従業員リストを表示します。

<h1>Employee List</h1>
<! --- インラインクエリは例を示すためにのみ使用。--->
<cfquery name="EmpList" datasource="cfdocexamples">
SELECT FirstName, LastName, Salary, Contract
FROM Employee
</cfquery>
<cfoutput query="EmpList">
#EmpList.FirstName#, #EmpList.LastName#, #LSCurrencyFormat(EmpList.Salary)#,
#EmpList.Contract#<br>
</cfoutput>
</cfdocument>

セクション、ヘッダ、フッタの作成

cfdocument および cfdocumentsection タグを使用すると、印刷可能出力を次のように微調整できます。

  • cfdocumentitem:改ページ、ヘッダー、フッターのいずれかを作成します。
  • cfdocumentsection:出力をいくつかのセクションに分割し、必要に応じてカスタムマージンを指定します。セクションごとに一意のヘッダーとフッターを指定するには、セクション内で cfdocumentitem タグを使用します。各ドキュメントセクションは新しいページから開始されます。
cfdocumentitem タグ

ヘッダーやフッターを指定したり、改ページを作成するには、1 つまたは複数の cfdocumentitem タグを使用します。cfdocumentitem タグを cfdocumentsection タグと組み合わせて使用する場合と、組み合わせずに使用する場合の結果は次のようになります。

  • cfdocumentsection と組み合わせて使用する場合:cfdocumentitem 属性はそのセクションにのみ適用され、以前に指定したヘッダーとフッターは上書きされます。
  • cfdocumentsection と組み合わせずに使用する場合:cfdocumentitem 属性は次のようにドキュメント全体に適用されます。
    • タグがドキュメントの先頭にある場合は、ドキュメント全体に適用されます。
    • タグがドキュメントの中央にある場合は、ドキュメントの残りの部分に適用されます。
    • タグがドキュメントの終わりにある場合は、何も効果を持ちません。
      ドキュメント全体の欄外ヘッダーを作成するには、次の例のように cfdocumentitem タグを使用します。

<! --- 欄外ヘッダー --->
<cfdocumentitem type="header">
<font size="-3"><i>Directory Report</i></font>
</cfdocumentitem>
<h3>cfdirectory Example</h3>
<! --- cfdirectory を使用して名前とサイズ別にディレクトリを表示 --->
<cfdirectory
directory="#GetDirectoryFromPath(GetTemplatePath())#"
name="myDirectory" recurse="yes"
sort="directory ASC, name ASC, size DESC">
<! ---- cfdirectory の内容を cftable として出力 ----->
<cftable query="myDirectory"
htmltable colheaders>
<cfcol header="DIRECTORY:" text="#directory#">
<cfcol header="NAME:" text="#Name#">
<cfcol header="SIZE:" text="#Size#">
</cftable>
</cfdocument>

cfdocumentsection タグ

cfdocumentsection を使用する場合は、ドキュメント内のすべてのテキストを cfdocumentsection タグで囲む必要があります。cfdocumentsection タグの外側にある HTML および CFML コードは無視されます。各セクションのマージン属性は、前のセクションまたは親の cfdocument タグで指定されたマージンよりも優先されます。マージン属性を指定した場合は、親の cfdocument タグの unit 属性によって単位が制御されます。unit 属性のデフォルトはインチです。
セクションごとに一意のヘッダーとフッターを指定し、各セクションの前に改ページを挿入するには、次の例のように、セクション内で cfdocumentitem タグを使用します。

SELECT Emp_ID, firstname, lastname, e.dept_id, salary, d.dept_name
FROM employee e, departmt d
WHERE e.dept_id = d.dept_id
ORDER BY d.dept_name
</cfquery>

<cfdocument format="PDF">
<cfoutput query="empSalary" group="dept_id">
<cfdocumentsection>
<cfdocumentitem type="header">
<font size="-3"><i>Salary Report</i></font>
</cfdocumentitem>
<cfdocumentitem type="footer">
<font size="-3">Page #cfdocument.currentpagenumber#</font>
</cfdocumentitem>
<h2>#dept_name#</h2>
<table width="95%" border="2" cellspacing="2" cellpadding="2" >
<tr>
<th>Employee</th>
<th>Salary</th>
</tr>
<cfset deptTotal = 0 >
<! --- 内側の cfoutput --->
<cfoutput>
<tr>
<td><font size="-1">
#empSalary.lastname#, #empSalary.firstname#</font>
</td>
<td align="right"><font size="-1">
#DollarFormat(empSalary.salary)#</font>
</td>
</tr>
<cfset deptTotal = deptTotal + empSalary.salary>
</cfoutput>
<tr>
<td align="right"><font size="-1">Total</font></td>
<td align="right"><font size="-1">#DollarFormat(deptTotal)#</font></td>
</tr>
<cfset deptTotal = 0>
</table>
</cfdocumentsection>
</cfoutput>
</cfdocument>

cfdocument スコープの使用

cfdocument タグを使用すると、ColdFusion によって cfdocument という名前のスコープが作成されます。このスコープには、次の変数が含まれます。

  • currentpagenumber現在のページ番号を表示します。
  • totalpagecount:総ページ数を表示します。
  • currentsectionpagenumber:現在のセクション番号を表示します。
  • totalsectionpagecount:総セクション数を表示します。

    注意:cfdocument スコープの変数は、cfdocumentitem タグ内の式でのみ使用できます。

    次の例のように、これらの変数をヘッダーまたはフッターで使用すると、現在のページ番号と総ページ数を表示できます。

#cfdocument.totalpagecount#</cfdocumentitem>

PDF ファイルのしおりの作成

PDF ドキュメント内のセクションごとにしおりを作成するには、次の例のように、cfdocument タグの bookmark 属性を使用します。

<cfdocumentitem type="header">
<font size="-1" align="center"><i>Building Better Applications</i></font>
</cfdocumentitem>
<cfdocumentitem type="footer">
<font size="-1"><i>Page <cfoutput>#cfdocument.currentpagenumber# of
#cfdocument.totalpagecount#</cfoutput></i></font>
</cfdocumentitem>
<cfdocumentsection name="Introduction">
<h3>Introduction</h3>
<p>The introduction goes here.</p>
</cfdocumentsection>
<cfdocumentsection name="Chapter 1">
<h3>Chapter 1: Getting Started</h3>
<p>Chapter 1 goes here.</p>
</cfdocumentsection>
<cfdocumentsection name="Chapter 2">
<h3>Chapter 2: Building Applications</h3>
<p>Chapter 2 goes here.</p>
</cfdocumentsection>
<cfdocumentsection name="Conclusion">
<h3>Conclusion</h3>
<p>The conclusion goes here.</p>
</cfdocumentsection>
</cfdocument>

PDF ドキュメントのしおりパネルにしおりが表示されます。

cfhttp タグを使用した Web ページの表示

Web ページ全体を PDF または FlashPaper 出力形式で表示するには、次の例のように、cfhttp タグを cfdocument タグと組み合わせて使用します。

<cfparam name="url.target_url" default="http://www.boston.com">
<cfoutput>
<cfhttp url="#url.target_url#" resolveurl="yes">

<cfdocument format="FlashPaper">
<cfdocumentitem type="header">
<cfoutput>#url.target_url#</cfoutput>
</cfdocumentitem>
<cfdocumentitem type="footer">
<cfoutput>#cfdocument.currentpagenumber# / #cfdocument.totalpagecount#</cfoutput>
</cfdocumentitem>
<! --- ページを表示 --->
#cfhttp.filecontent#
</cfdocument>
</cfoutput>

高度な PDF オプションの使用

cfdocument タグでは、次の表に示す Acrobat セキュリティオプションをサポートしています。

セキュリティオプション

説明

暗号化

PDF 出力を暗号化するかどうかを指定するには、encryption 属性を使用します。次のいずれかを指定します。

  • 128-bit
  • 40-bit
  • none

ユーザーパスワード

ドキュメントを表示するためにユーザーが入力する必要があるパスワードを指定するには、userpassword 属性を使用します。

オーナーパスワード

ドキュメントを表示し、必要に応じて変更を加えるためにユーザーが入力する必要があるパスワードを指定するには、ownerpassword 属性を使用します。

また、cfdocument タグの permissions 属性では、次の Acrobat セキュリティ権限がサポートされます。次のいずれかの値を指定します。複数の権限を指定する場合は、値をカンマで区切ります。

権限

説明

印刷

AllowPrinting 属性を指定すると、閲覧者がドキュメントを印刷できます。

変更

AllowModifyContents 属性を指定すると、閲覧者がドキュメントを変更できます(ただし、そのためのソフトウェアが必要です)。

コピー

AllowCopy 属性を指定すると、閲覧者がドキュメント内のテキストを選択してコピーすることができます。

注釈

AllowModifyAnnotations を指定すると、閲覧者がドキュメントにコメントを追加できます。ユーザーが注釈を追加する場合は、本人が手動で PDF を保存する必要があります。

スクリーンリーダー

AllowScreenReaders を指定すると、スクリーンリーダーを使用してドキュメントにアクセスできます。

入力

AllowFillIn を指定すると、ユーザーがフォームフィールドを使用できます。

組み立て

AllowAssembly を指定すると、ユーザーがページの挿入、削除、回転だけでなく、しおりやサムネールの作成もできます。

低品質印刷

AllowDegradedPrinting を指定すると、低解像度印刷が可能です。この形式では、各ページがビットマップとして印刷されるため、印刷時間が長くなる可能性があります。

 

注意:これらのオプションのデフォルト値は、暗号化レベルによって異なります。これらのオプションは、PDF のみに適用されます。詳細については、『CFML リファレンス』の cfdocument に関する説明を参照してください。

次の例では、コピーのみが許可された PDF ドキュメントを作成します。

ownerpassword="us3rpa$$w0rd" userpassword="us3rpa$$w0rd"
permissions="AllowCopy" >
<h1>Employee List</h1>
<cfquery name="EmpList" datasource="cfdocexamples">
SELECT FirstName, LastName, Salary
FROM Employee
</cfquery>
<cfoutput query="EmpList">
#EmpList.FirstName#, #EmpList.LastName#, #LSCurrencyFormat(EmpList.Salary)#<br>
</cfoutput>
</cfdocument>

印刷可能レポートをファイルに保存する方法

生成された PDF または SWF 出力をファイルに保存するには、次の例のように cfdocument タグの filename 属性を使用します。

cfdocs ディレクトリ下にあるチュートリアルアプリケーション。--->
<cfquery datasource="compasstravel" name="compasstrips">
SELECT tripName, tripDescription, tripLocation, price
FROM trips
ORDER BY price
</cfquery>
<cfdocument format="pdf"
filename="#GetDirectoryFromPath(GetTemplatePath())#/compasstrips.pdf"
overwrite="yes">
<cfdocumentsection>
<h1 align="center">Compass Travel</h1>
<h2 align="center">Destination Guide</h2>
<p align="center"><img src="cfdocs/getting_started/photos/somewhere.jpg"></p>
</cfdocumentsection>
<cfdocumentsection>
<cfdocumentitem type="header">
<font size="-3"> <i>Compass Travel Trip Descriptions</i></font>
</cfdocumentitem>
<cfdocumentitem type="footer">
<font size="-3">
<cfoutput>Page #cfdocument.currentpagenumber#</cfoutput>
</font>
</cfdocumentitem>
<cfoutput query="compasstrips">
<hr>
<h2>#tripName#</h2>
<p><b>#tripLocation#</b></p>
<p>Price: #DollarFormat(price)#</p>
<p>#tripDescription#</p>
</cfoutput>
</cfdocumentsection>
</cfdocument>

 

ヘルプをすばやく簡単に入手

新規ユーザーの場合