カスタム検索

フォントの指定

Zend_Pdfは、標準のPDFフォント14種類、および、独自のTTFなどのフォントを使用することができます。独自フォントを適用すれば、日本語の表示も可能です。

標準フォント

標準フォントは以下のように定数として定義されています。

定数名 フォント
Zend_Pdf_Font::FONT_COURIER Courier
Zend_Pdf_Font::FONT_COURIER_BOLD Courier Bold
Zend_Pdf_Font::FONT_COURIER_ITALIC Courier Bold Italic
Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC Courier Bold Italic
Zend_Pdf_Font::FONT_TIMES Times
Zend_Pdf_Font::FONT_TIMES_BOLD Times Bold
Zend_Pdf_Font::FONT_TIMES_ITALIC Times Italic
Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC Times Bold Italic
Zend_Pdf_Font::FONT_HELVETICA Helvetica
Zend_Pdf_Font::FONT_HELVETICA_BOLD Helvetica Bold
Zend_Pdf_Font::FONT_HELVETICA_ITALIC Helvetica Italic
Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC Helvetica Bold Italic
Zend_Pdf_Font::FONT_SYMBOL Symbol
Zend_Pdf_Font::FONT_ZAPFDINGBATS Zapfdingbats

フォントの生成は、Zend_Pdf_FontのfontWithName()メソッドに上記定数を指定します。フォントの適用は、setFont()メソッドに生成したフォントのインスタンスとサイズを引数に与えます。

Zend_Pdf-ex7.php (標準フォントの指定)
<?php
/**
 * Zend_Pdf
 * 標準フォントの指定
 */

/** Zend_Pdfのロード */
require_once 'Zend/Pdf.php';


/** 生成 */
// Zend_Pdfのインスタンス生成
$pdf = new Zend_Pdf();

// A4サイズのページを作成
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);


/** 設定 */
// フォントの指定
$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER);

// フォントを24ptで適用
$pdf->pages[0]->setFont($font24);

// 文字列定義
$text 'Courier 24pt';

// UTF-8で描画
$pdf->pages[0]->drawText($text40780'UTF-8');


// フォントの指定
$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER_BOLD);

// フォントを24ptで適用
$pdf->pages[0]->setFont($font24);

// 文字列定義
$text 'Courier Bold 24pt';

// UTF-8で描画
$pdf->pages[0]->drawText($text40756'UTF-8');


$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER_ITALIC);
$pdf->pages[0]->setFont($font24);
$text 'Courier Italic 24pt';
$pdf->pages[0]->drawText($text40732'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_COURIER_BOLD_ITALIC);
$pdf->pages[0]->setFont($font24);
$text 'Courier Bold Italic 24pt';
$pdf->pages[0]->drawText($text40708'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES);
$pdf->pages[0]->setFont($font24);
$text 'Times 24pt';
$pdf->pages[0]->drawText($text40674'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD);
$pdf->pages[0]->setFont($font24);
$text 'Times bold 24pt';
$pdf->pages[0]->drawText($text40650'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_ITALIC);
$pdf->pages[0]->setFont($font24);
$text 'Times italic 24pt';
$pdf->pages[0]->drawText($text40626'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_TIMES_BOLD_ITALIC);
$pdf->pages[0]->setFont($font24);
$text 'Times bold italic 24pt';
$pdf->pages[0]->drawText($text40602'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
$pdf->pages[0]->setFont($font24);
$text 'Helvetica 24pt';
$pdf->pages[0]->drawText($text40568'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
$pdf->pages[0]->setFont($font24);
$text 'Helvetica bold 24pt';
$pdf->pages[0]->drawText($text40544'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_ITALIC);
$pdf->pages[0]->setFont($font24);
$text 'Helvetica italic 24pt';
$pdf->pages[0]->drawText($text40520'UTF-8');

$font Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD_ITALIC);
$pdf->pages[0]->setFont($font24);
$text 'Helvetica bold italic 24pt';
$pdf->pages[0]->drawText($text40496'UTF-8');


/** 出力 */
// HTTPヘッダ:PDFを出力
header('Content-type: application/pdf');

// HTTPヘッダ:ファイル名をhello_world.pdfに
header('Content-Disposition: attachment; filename="hello_world.pdf"');

// ドキュメントを出力
echo $pdf->render();
Zend_Pdf-ex7.phpを実行し、保存後、ビューアーで表示

独自フォント

独自フォントの生成は、Zend_Pdf_FontのfontWithPath()メソッドにフォントファイルのパスを指定します。フォントの適用は、標準フォントの適用と同様、setFont()メソッドに生成したフォントのインスタンスとサイズを引数に与えます。

Zend_Pdf-ex8.php (独自フォントの指定)
<?php
/**
 * Zend_Pdf
 * 独自フォントの指定
 */

/** Zend_Pdfのロード */
require_once 'Zend/Pdf.php';


/** 生成 */
// Zend_Pdfのインスタンス生成
$pdf = new Zend_Pdf();

// A4サイズのページを作成
$pdf->pages[] = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);


/** 設定 */
// フォントのパス
$fontPath '/usr/share/fonts/VLGothic/VL-Gothic-Regular.ttf';
// フォントの指定
$font Zend_Pdf_Font::fontWithPath($fontPath);

// フォントを24ptで適用
$pdf->pages[0]->setFont($font24);

// 文字列定義
$text 'VL-Gothic';

// UTF-8で描画
$pdf->pages[0]->drawText($text40780'UTF-8');


/** 出力 */
// HTTPヘッダ:PDFを出力
header('Content-type: application/pdf');

// HTTPヘッダ:ファイル名をhello_world.pdfに
header('Content-Disposition: attachment; filename="hello_world.pdf"');

// ドキュメントを出力
echo $pdf->render();
Zend_Pdf-ex8.phpを実行し、保存後、ビューアーで表示
レンタルサーバーなら使えるねっと
CopyLeft 2010 PEABOX.COM サイト管理者に連絡