PDF ドキュメントのページは、Zend_Pdf
の
Zend_Pdf_Page
クラスで表されます。
PDF ページは既存の PDF から読み込むこともできますし、 新しく作成することもできます。
新しいページを取得するには、直接 Zend_Pdf_Page
オブジェクトを作成するか、Zend_Pdf::newPage()
メソッドをコールします。このメソッドは Zend_Pdf_Page
オブジェクトを返します。Zend_Pdf::newPage()
の場合は、すでにドキュメントにアタッチされているページを作成するという点が異なります。
アタッチされたページは複製されない限り、他の PDF で使用できません。
詳しくは ページの複製 セクションをご覧ください。
Zend_Pdf::newPage()
メソッドおよび Zend_Pdf_Page
のコンストラクタは、どちらも同じ形式のパラメータを受け取ります。
ページサイズを ($x, $y) 形式のポイント数 (1/72 インチ)
で表したものか、定義済みの定数のうちのいずれかになります。
以下の定数が定義されています。
-
Zend_Pdf_Page::SIZE_A4
-
Zend_Pdf_Page::SIZE_A4_LANDSCAPE
-
Zend_Pdf_Page::SIZE_LETTER
-
Zend_Pdf_Page::SIZE_LETTER_LANDSCAPE
ドキュメントのページは、Zend_Pdf
クラスの public メンバである
$pages
に保存されます。これは Zend_Pdf_Page
オブジェクトの配列です。これによってページの並び順も定義され、
一般的な配列と同じように操作できます。
例641 PDF ドキュメントのページの操作
... // ページの並び順を反転します $pdf->pages = array_reverse($pdf->pages); ... // 新しいページを追加します $pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4); // 新しいページを追加します $pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4); // 指定したページを削除します unset($pdf->pages[$id]); ...
既存の PDF ページを繰り返すには、新しい Zend_Pdf_Page
オブジェクトを作成する際に既存のページをパラメータとして指定します。
例642 既存のページを繰り返す
... // テンプレートページを別の変数に格納します $template = $pdf->pages[$templatePageIndex]; ... // 新しいページを追加します $page1 = new Zend_Pdf_Page($template); $page1->drawText('Some text...', $x, $y); $pdf->pages[] = $page1; ... // 別のページを追加します $page2 = new Zend_Pdf_Page($template); $page2->drawText('Another text...', $x, $y); $pdf->pages[] = $page2; ... // テンプレートページをドキュメントから削除します unset($pdf->pages[$templatePageIndex]); ...
これは、ひとつのテンプレートから複数のページを作成したい場合に便利です。
注意
注意! 繰り返されたページは、テンプレートページと同じ PDF リソースを共有します。つまり、 テンプレートページと同じドキュメントしか使用できません。 ドキュメントを修正したら、新しいページとして保存できます。
clone
operator may be used to create page which is not attached to any document.
It takes more time than duplicating page since it needs to copy all dependent objects
(used fonts, images and other resources), but it allows to use pages from different source
documents to create new one:
例643 既存のページを複製
$page1 = clone $pdf1->pages[$templatePageIndex1]; $page2 = clone $pdf2->pages[$templatePageIndex2]; $page1->drawText('Some text...', $x, $y); $page2->drawText('Another text...', $x, $y); ... $pdf = new Zend_Pdf(); $pdf->pages[] = $page1; $pdf->pages[] = $page2;
If several template pages are planned to be used as templates then it could be more efficient
to utilize Zend_Pdf_Resource_Extractor
class which gives an ability
to share resources between cloned pages - fonts, images, etc. (otherwise new resource copy
will be created for each cloned page):
例644 Zend_Pdf_Resource_Extractor クラスを使用して既存のページを複製
$extractor = new Zend_Pdf_Resource_Extractor(); .... $page1 = $extractor->clonePage($pdf->pages[$templatePageIndex1]); $page2 = $extractor->clonePage($pdf->pages[$templatePageIndex2]); $page1->drawText('Some text...', $x, $y); $page2->drawText('Another text...', $x, $y); ... $pdf = new Zend_Pdf(); $pdf->pages[] = $page1; $pdf->pages[] = $page2;