萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

PHP 產生 PDF 文件完整指南:PDFLib 擴充模組實戰應用

PHP 擁有強大的擴充生態,PDFLib 讓開發者能直接在 PHP 中動態產生專業級 PDF 文件。本文從安裝設定、基礎繪圖、文字排版、圖片嵌入、幾何繪圖到文件屬性設定,完整涵蓋 PDF 產生的核心技術。

環境安裝與設定

  • Linux:從 PDFLib 官網 下載原始碼編譯安裝,或使用套件管理器 pecl install pdflib
  • Windows:下載預編譯 php_pdf.dll 放入 ext/,於 php.ini 啟用 extension=php_pdf.dll
  • 需安裝 Adobe Acrobat Reader 或相容檢視器預覽輸出結果

四步驟建立 PDF 文件

  1. 建立文件控制代碼:$pdf = PDF_new();
  2. 開啟輸出檔案:PDF_open_file($pdf, 'output.pdf');
  3. 撰寫頁面內容(文字、圖片、幾何圖形)
  4. 關閉並儲存:PDF_close($pdf);

完整範例:商業報表雛形

<?php
$pdf = PDF_new();
PDF_open_file($pdf, 'report.pdf');
PDF_begin_page($pdf, 595, 842); // A4: 595x842 pt

// 字體設定
$font = PDF_findfont($pdf, 'Helvetica', 'host', 1);
PDF_setfont($pdf, $font, 12);

// 標題
PDF_show_xy($pdf, 'Monthly Sales Report', 50, 780);

// 繪製分隔線
PDF_setcolor($pdf, 'stroke', 'rgb', 0, 0, 0);
PDF_moveto($pdf, 50, 770);
PDF_lineto($pdf, 545, 770);
PDF_stroke($pdf);

// 表格標頭
$y = 740;
$headers = ['Product', 'Qty', 'Unit Price', 'Total'];
$xPos = [50, 250, 350, 450];
PDF_setfont($pdf, $font, 10);
foreach ($headers as $i => $h) {
    PDF_show_xy($pdf, $h, $xPos[$i], $y);
}

// 表格資料(示範)
$data = [
    ['Widget A', 100, 12.50, 1250.00],
    ['Widget B', 50,  25.00, 1250.00],
    ['Gadget C', 200, 8.75, 1750.00],
];
$y -= 20;
foreach ($data as $row) {
    foreach ($row as $i => $val) {
        PDF_show_xy($pdf, is_numeric($val) ? number_format($val,2) : $val, $xPos[$i], $y);
    }
    $y -= 18;
}

// 嵌入圖片(Logo)
$img = PDF_open_image_file($pdf, 'jpeg', 'logo.jpg');
PDF_place_image($pdf, $img, 50, 785, 0.5);

// 繪製矩形與圓形
PDF_setcolor($pdf, 'fill', 'rgb', 1, 1, 0); // 黃填滿
PDF_setcolor($pdf, 'stroke', 'rgb', 0, 0, 0); // 黑邊框
PDF_rect($pdf, 50, 500, 200, 100);
PDF_fill_stroke($pdf);

PDF_setcolor($pdf, 'fill', 'rgb', 0, 1, 0); // 綠填滿
PDF_setcolor($pdf, 'stroke', 'rgb', 0, 0, 1); // 藍邊框
PDF_circle($pdf, 400, 550, 50);
PDF_fill_stroke($pdf);

// 文件資訊
PDF_set_info_author($pdf, 'Sales Dept');
PDF_set_info_title($pdf, 'Monthly Report');
PDF_set_info_subject($pdf, 'PHP PDFLib Demo');
PDF_set_info_keywords($pdf, 'PHP, PDF, PDFLib, Report');

PDF_end_page($pdf);
PDF_close($pdf);
echo 'PDF generated: report.pdf';
?>

關鍵函式速查表

功能函式說明
新頁面PDF_begin_page($pdf, $w, $h)單位為 point (1/72 inch),A4=595x842
字體載入PDF_findfont($pdf, $name, $enc, $embed)回傳字體物件,$embed=1 內嵌字體
設定字體PDF_setfont($pdf, $font, $size)
指定座標寫文字PDF_show_xy($pdf, $text, $x, $y)原點(0,0)在左下角
顏色設定PDF_setcolor($pdf, $type, $model, $c1, $c2, $c3)type: stroke/fill/both;model: rgb/cmyk;值 0~1
直線PDF_moveto/lineto/stroke移動筆觸→定義終點→描繪
矩形PDF_rect($pdf, $x, $y, $w, $h)左下角座標+寬高
圓形PDF_circle($pdf, $cx, $cy, $r)圓心座標+半徑
填滿並描邊PDF_fill_stroke($pdf)同時套用 fill 與 stroke 顏色
圖片嵌入PDF_open_image_file/PDF_place_image支援 JPEG, PNG, TIFF, GIF;最後參數為縮放比例

常見問題與對策

  • 中文亂碼:需使用支援 Unicode 的字體(如 MSung-Light, MHei-Medium),並以 PDF_findfont($pdf, 'MSung-Light', 'UniCNS-UCS2-H', 1) 載入
  • 記憶體不足:大量圖片/頁面時調整 memory_limit,或分批產生再合併
  • 效能優化:重複使用字體物件、圖片物件;避免在迴圈中重複 PDF_findfont
copyright © 萬盛學電腦網 all rights reserved