|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2012/9/19 2:16 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]用 PHP 處理中文 PDF
- 用PHP處理中文PDF
Posted on 2008-12-20 by wlx
因項目需要,現在想在服務器端動態生成PDF文件,已一個PDF為模板,然後把所需的數據動態填入。 本來在使用zend framework,因此很直接的就使用zend_pdf類來進行測試,代碼實現如下:
$pdf = Zend_Pdf::load($this->view->config->offline->template);
$pdf->pages = array_reverse($pdf->pages);
$style = new Zend_Pdf_Style();
$font = Zend_Pdf_Font::fontWithPath($this->view->config->offline->font, ( Zend_Pdf_Font::EMBED_DONT_SUBSET));
$pdf->pages[0]->setFont($font, 10);
$pdf->pages[0]->saveGS();
//datalist
$pdf->pages[0]->drawText(str_replace(";","\n",$datalist), 100, 570,"UTF-8");
//project
$pdf->pages[0]->drawText($formData['project'], 100, 430,"UTF-8");
$pdf->pages[0]->drawText($formData['realname'], 100, 78,"UTF-8");
$pdf->pages[0]->drawText($formData['realname'], 130, 590,"UTF-8");
$pdf->pages[0]->drawText($formData['unit'], 95, 58,"UTF-8");
$pdf->pages[0]->drawText($formData['address'], 285, 58,"UTF-8");
$pdf->pages[0]->drawText($formData['postcode'], 470, 58,"UTF-8");
$pdf->pages[0]->drawText($formData['email'], 95, 40,"UTF-8");
$pdf->pages[0]->drawText($formData['phone'], 295, 40,"UTF-8");
$t=getdate();
$pdf->pages[0]->drawText($t['year'], 465, 40,"UTF-8");
$pdf->pages[0]->drawText($t['mon'], 500, 40,"UTF-8");
$pdf->pages[0]->drawText($t['mday'], 525, 40,"UTF-8");
$pdf->pages[0]->restoreGS();
$fn=$formData['realname'].date('YmdHis').".pdf";
$pdf->save($this->view->config->offline->savepath."/".$fn);
這樣,的確能完成要求,但是令人惱火的是,其把中文字體內嵌到PDF文件中去,因此導致PDF文件過大,而且導致PHP所需內存也增大(64M)。 因此,就開始嘗試別的辦法,開始鎖定fpdf,PHP能完美支持,而且也能支持中文(所要單獨下載chinese.php和chinese-unicode.php)。 但是,FPDF不支持打開已有的PDF文件作為模板,因此還需要使用fpdi進行模板的支持,還要修改fpdi,使其父類為PDF-UNICODE。
然後就可以這樣使用:
$pagecount = $this->setSourceFile($this->template);
$tplidx = $this->importPage(1, '/MediaBox');
$this->addPage();
$this->useTemplate($tplidx);
$this->AddUniGBhwFont('ugb','AdobeSongStd-Light-Acro');
$this->SetFont('ugb','',$this->fontsize);
$this->setXY(45,65);
$this->Write($this->fontsize,$this->data['realname']);
其中,字形由默認的AdobeSongStd-Light改成AdobeSongStd-Light-Acro,是為了防止英文字體變寬和變窄,格式才會整齊,英文字不會擠在一起。(參考: http://bbs.erp100.com/thread-18424-1-1.html)
原文出處:用PHP处理中文PDF | LiangXu Wang
|
|
討論串
-
[轉貼]免費好用的 PDF Library 大搜集 (冷日 (冷日), 2010/1/31 3:43)
-
[分享]PHP 之 PDF Library UTF-8 中文解決方案 (冷日 (冷日), 2010/1/31 16:15)
-
[轉貼] PHP 生成 PDF 完美支持中文,解決 TCPDF 亂碼 (冷日 (冷日), 2012/9/4 8:36)
-
[轉貼]使用 PHP 開源類別 ( TCPDF ) 製作 UTF-8 的 PDF 報表 (冷日 (冷日), 2012/9/4 8:40)
-
[轉貼]undefined function bcadd() (冷日 (冷日), 2012/9/5 7:57)
-
[轉貼]PHP: BC Installing/Configuring (冷日 (冷日), 2012/9/5 8:03)
-
[轉貼]使用 PHP(FPDI)產生 UNICODE 中文 PDF (冷日 (冷日), 2012/9/18 14:48)
-
[轉貼]利用 FPDI 製作 PDF 檔案 (冷日 (冷日), 2012/9/18 14:51)
-
[轉貼]php 採用 fpdf 生成中文 pdf (冷日 (冷日), 2012/9/18 14:54)
- »
[轉貼]用 PHP 處理中文 PDF (冷日 (冷日), 2012/9/19 2:16)
-
[分享]碰上 error: Some data has already been output, can't send PDF file (冷日 (冷日), 2013/1/3 7:09)
-
Re: [分享]PHP 之 PDF Library UTF-8 中文解決方案 (筆痕, 2014/11/6 23:12)
|