首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结合tfpdf和fpdi

结合tfpdf和fpdi
EN

Stack Overflow用户
提问于 2014-04-30 17:11:39
回答 1查看 3.7K关注 0票数 1

我使用以下代码

代码语言:javascript
复制
<?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');
?>

我正在犯错误:

代码语言:javascript
复制
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/的以下代码时

代码语言:javascript
复制
<?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');
?>

并得到以下错误:

代码语言:javascript
复制
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字体?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-30 17:27:18

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

  1. 为什么在第一个代码示例中没有定义FPDF,以及
  2. 为什么它不能在第二个代码示例中加载我的字体。

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

关于#1:我怀疑问题归结于您的require_once语句。具体来说,当示例代码不需要时,您就需要在代码中使用fpdf.php。我猜在某个地方发生了冲突。

与其花很多时间在库中寻找问题,我会从示例代码开始(它来自开发人员的网站,所以我们非常肯定它是有效的--也许不行,但让我们假设它确实有效,直到证明不是这样)。假设代码有效(您所需要做的就是修复字体的路径),从这个开始,并修改它以完成您想做的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23394161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档