Main Content

PDF と HTML のドキュメント パーツおよびホール

この例では、以下の方法を説明します。

  • ホールのあるドキュメント パーツ テンプレートを定義する。

  • プログラムによってドキュメント パーツをレポートに挿入し、ホールを埋める。

  • 目次のドキュメント パーツを挿入する。

この例では、PDF テンプレートおよびレポートを使用します。ただし、これと同じプロセスを HTML レポートに使用できます。この例を通じて、ドキュメント タイプ情報をそれに対応する HTML 情報に置き換えます。

PDF のドキュメント パーツ ライブラリへのテンプレートの追加

この例では、既定の PDF テンプレート パッケージから始めます。

  1. 既定のテンプレート パッケージのコピーを作成します。

    mlreportgen.dom.Document.createTemplate('myPDFtemplate','pdf');
  2. テンプレート パッケージを解凍します。

    unzipTemplate('myPDFtemplate.pdftx');
  3. 現在のフォルダーで、解凍したテンプレート フォルダー myPDFtemplate を開きます。docpart_templates.html を HTML またはテキスト エディターで開きます。

    dplibrary 要素はドキュメント パーツ ライブラリを定義します。dptemplate 要素は各ドキュメント パーツ テンプレートを定義します。このドキュメント パーツ ライブラリには、2 つのドキュメント パーツ テンプレートがあります。

    • rgChapter: 章のパーツ テンプレートを定義

    • ReportTOC: 目次を定義

    <html>
    <body>
       <dplibrary>
    
    	  <dptemplate name="rgChapter">
          <h1 class="rgChapterTitle">
          <hole id="rgChapterTitlePrefix" default-style-name="rgChapterTitlePrefix" />
            <span> </span>
          <hole id="rgChapterTitleNumber" default-style-name="rgChapterTitleNumber" />
            <span>. </span>
          <hole id="rgChapterTitleText" default-style-name="rgChapterTitleText" />
          </h1>
          <hole id="rgChapterContent"/>
         </dptemplate>
        
       <dptemplate name="ReportTOC">
           <TOC number-of-levels ="3" leader-pattern="dots" />
       </dptemplate>
    
       </dplibrary>
    
    </body>
    </html>
    
  4. Author という名前のドキュメント パーツ テンプレートを作成します。ドキュメント パーツは、固定テキストとホールの任意の組み合わせを含むことができます。このドキュメント パーツ テンプレートには、固定テキスト Author と作成者名のホールが含まれます。

    <dptemplate name="Author">
          <p class="Author">
          <span>Author: </span><hole id="AuthorName" />  
          </p>
                
     </dptemplate>
    
  5. 新しいドキュメント パーツ テンプレートをライブラリに追加します。ドキュメント パーツを API から呼び出すときは名前で参照するため、テンプレートをライブラリ内に任意の順序で置くことができます。ドキュメント パーツ テンプレートにはそれぞれ一意の名前を使用します。

     <dplibrary>
            
    	  <dptemplate name="rgChapter">
          <h1 class="rgChapterTitle">
          <hole id="rgChapterTitlePrefix" default-style-name="rgChapterTitlePrefix" />
            <span> </span>  
          <hole id="rgChapterTitleNumber" default-style-name="rgChapterTitleNumber" />
            <span>. </span>
          <hole id="rgChapterTitleText" default-style-name="rgChapterTitleText" />
          </h1>
          <hole id="rgChapterContent"/>
       </dptemplate>
       <dptemplate name="ReportTOC">
          <TOC number-of-levels ="3" leader-pattern="dots" />
      </dptemplate>
      <dptemplate name="Author">
          <p class="Author">
          <span>Author: </span><hole id="AuthorName" />  
          </p>
      </dptemplate>
    
    </dplibrary>
    
  6. テンプレートを myPDFtemplate2.pdftx という新しいテンプレートに再パッケージ化します。

    zipTemplate('myPDFtemplate2.pdftx','myPDFtemplate');

レポート プログラムでのドキュメント パーツ テンプレートの使用

mlreportgen.dom.DocumentPart を使用して、ドキュメント パーツ テンプレートを使用します。以下が必要です。

  • ドキュメント パーツを含むテンプレート パッケージの名前。この例では、テンプレート パッケージの名前は myPDFtemplate2 です。

  • 呼び出すドキュメント パーツ テンプレートの名前および埋めるホールの順序。この例では、次を呼び出します。

    • ドキュメント パーツ テンプレート rgChapter。接頭辞、番号、タイトルの順序で、最初の 3 個のホールを埋めます。

    • 目次を挿入する ReportTOC ドキュメント パーツ テンプレート

    • 作成した Author ドキュメント パーツ テンプレート。その 1 個のホールを埋めます。

import mlreportgen.dom.*
d = Document('myDocPartEx','pdf','myPDFtemplate2');
open(d);

% Assign the rgChapter document part template to the variable dp
dp = DocumentPart(d,'rgChapter');

% Move to each hole in this document part and append content 
moveToNextHole(dp);
append(dp,'Chapter');
moveToNextHole(dp);
append(dp,'5');
moveToNextHole(dp);
append(dp,'Creating Document Part Templates');

% Append this document part to the document
append(d,dp);

% Append the document part ReportTOC to the document
append(d,DocumentPart(d,'ReportTOC'));

% You can append any allowable object between document parts or holes
append(d,Paragraph('Append any allowable object or a document part.'));
append(d,Paragraph('Append a document part next:'));

% Assign the Author document part template to the variable dp2
dp2 = DocumentPart(d,'Author');

% Move to the next hole and fill it
% Append the document part to the document
moveToNextHole(dp2);
append(dp2,'Charles Brown');
append(d,dp2);

close(d);
rptview(d.OutputPath);

Author ドキュメント パーツ テンプレートには、ホールの前に固定テキストが含まれます。moveToNextHole は、テンプレート内で前のホール (またはドキュメント パーツの先頭) とカレント ホールの間にある固定コンテンツをドキュメントに追加します。

参考

関連するトピック