结合tfpdf和fpdi

时间:2014-04-30 17:11:39

标签: php utf-8 fpdf fpdi

我正在使用以下代码

<?php

$hd1 = $_POST["hd1"];
require_once('fpdi.php');
require_once('fpdf.php');
require('tfpdf.php');

$pdf =& new FPDI();

$pagecount = $pdf->setSourceFile('template.pdf');
$tplIdx = $pdf->importPage(1); 
$s = $pdf->getTemplatesize($tplIdx);
$pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L', array($s['w'], $s['h'])); 
$pdf->useTemplate($tplIdx,0, 0, 0, 0, true); 

$pdf->AddFont('DejaVu','','DejaVuSansCondensed.ttf',true);
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true);


$pdf->SetFont('DejaVu', '', 14);

$pdf->Cell(50,10,$hd1,0,1);
// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial', '', 14);
$pdf->Ln(10);
$pdf->Write(5, 'The file uses font subsetting.');

$pdf->Output('doc.pdf', 'I');
?>

我收到错误:

Fatal error: Class 'FPDF' not found in C:\xampp\htdocs\san\ak-form\pdf\fpdi_bridge.php on line 33

当我使用http://www.setasign.com/products/fpdi/demos/tfpdf-demo/

中的以下代码时
<?php

$hd1 = $_POST["hd1"];

// require tFPDF
require_once('tfpdf.php');

// map FPDF to tFPDF so FPDF_TPL can extend it
class FPDF extends tFPDF
{
    /**
     * "Remembers" the template id of the imported page
     */
    protected $_tplIdx;

    /**
     * Draw an imported PDF logo on every page
     */
    public function Header()
    {
        if (is_null($this->_tplIdx)) {
            $this->setSourceFile("template.pdf");
            $this->_tplIdx = $this->importPage(1);
        }
        $size = $this->useTemplate($this->_tplIdx, 130, 5, 60);

        $this->SetFont('DejaVu', 'B', 16);
        $this->SetTextColor(0);
        $this->SetXY($this->lMargin, 5);

        $text = 'tFPDF (v' . tFPDF_VERSION . ') and FPDI (v'
              . FPDI::VERSION . ')';
        $this->Cell(0, $size['h'], $text);
        $this->Ln();
    }
}

// just require FPDI afterwards
require_once('fpdi.php');

// initiate PDF
$pdf = new FPDI();

// Add some Unicode font (uses UTF-8)
$pdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.ttf', true);
$pdf->AddFont('DejaVu', 'B', 'DejaVuSansCondensed-Bold.ttf', true);

// add a page
$pdf->AddPage();

$pdf->SetFont('DejaVu', '', 14);

// Select a standard font (uses windows-1252)
$pdf->SetFont('Arial', '', 14);
$pdf->Ln(10);
$pdf->Write(5, 'The file uses font subsetting.');

$pdf->Output('doc.pdf', 'I');
?>

收到以下错误:

Warning: fopen(/home/content/w/i/s/wiseinmotion/html/test3/pdf/font/unifont/DejaVuSansCondensed.ttf): failed to open stream: No such file or directory in C:\xampp\htdocs\san\ak-form\pdf\font\unifont\ttfonts.php on line 496
Can't open file /home/content/w/i/s/wiseinmotion/html/test3/pdf/font/unifont/DejaVuSansCondensed.ttf

我可以使用现有的pdf并使用utf-8字体的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

你在这里问两个不同的问题:

  1. 为什么第一个代码示例中未定义FPDF

  2. 为什么不能在第二个代码示例中加载我的字体。

  3. 我将首先回答#2,因为它很简单:找不到字体文件。检查您的路径并确保代码在正确的位置查找字体。

    关于#1:我怀疑问题归结为你的require_once陈述。具体来说,当示例代码没有时,您需要在代码中fpdf.php。我的猜测是,某处存在冲突。

    我不是花很多时间浏览库中的问题,而是从示例代码开始(来自开发人员的网站,所以我们漂亮确定它有效 - 也许不是,但我们假设它确实如此,直到证明不然)。假设代码有效(并且您需要做的就是修复字体的路径),请从中开始并修改它以执行您想要的操作。

相关问题