【问题标题】:Division by zero error with FPDF + WriteHTML add-on使用 FPDF + WriteHTML 附加组件除以零错误
【发布时间】:2014-08-31 08:44:21
【问题描述】:

我正在尝试使用 FPDF 生成 PDF 文件,这是我第一次尝试。

我有最新的 FPDF 文件并且还设置了 WriteHTML 插件。下面的代码一直在运行,直到最底部的 WriteHTML 部分。我收到错误“警告:第 796 行 /home4/fwall/public_html/fpdf/fpdf.php 中除以零”。当我查看 FPDF.php 的第 796 行时,我发现:

// Output text in flowing mode
    $cw = &$this->CurrentFont['cw'];
    $w = $this->w-$this->rMargin-$this->x;
    $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; // <--LINE 796
    $s = str_replace("\r",'',$txt);
    $nb = strlen($s);
    $sep = -1;
    $i = 0;
    $j = 0;
    $l = 0;
    $nl = 1;

如果我添加一个条件语句,大致如下:

if ($this->FontSize != 0) {
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; // <--LINE 796
}

我可以让错误消失,但我知道这不可能是正确的。有人在我的代码中看到会导致这种情况的错误吗?

require ('/home4/fwall/public_html/fpdf/fpdf.php'); 

//create a FPDF object
$pdf=new FPDF();
$pdfhtml=new PDF_HTML();

//set document properties
$pdf->SetAuthor('Author Name');
$pdf->SetTitle('PRESS RELEASE - NAME OF SHOW');

//set font for the entire document
$pdf->SetFont('Helvetica','B',10);
$pdf->SetTextColor(0,0,0);

//set up a page
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');

//display the top block
$contact = 'Contact Name';
$addline1 = '4002 2nd Ave NE, #2';
$addline2 = 'Address Line 2';
$cityzip = 'Seattle, WA 98105';
$pdf->Cell(0, 4, 'PRESS RELEASE', 0, 0, 'L');
$pdf->Cell(0, 4, 'FOR IMMEDIATE RELEASE', 0, 1, 'R');
$pdf->Cell(0, 4, 'CONTACT: '.$contact, 0, 0, 'L');
$pdf->Cell(0, 4, 'KILL DATE: August 1, 2014', 0, 1, 'R');
$pdf->Cell(0, 4, $addline1, 0, 1, 'L');
$pdf->Cell(0, 4, $addline2, 0, 1, 'L');
$pdf->Cell(0, 4, $cityzip, 0, 1, 'L');


//display the title
$pdf->SetFontSize(20);
$pdf->Cell(0,10,'PRESS RELEASE TITLE',0,1,'C',0);

//display the sub-title
$pdf->SetFontSize(16);
$pdf->Cell(0,10,'The Subtitle',0,1,'C',0);

//display the italic summary
$pdf->SetXY(10,55);
$pdf->SetFont('Helvetica','I',10);
$summary = 'SEATTLE - Theatre Off Jackson presents SPF 8, the annual exhibition of solo performance. Featuring four featured performers and one shorts night, the festival will occur between February 6th and March 1st, 2014. This year\'s festival is an exciting mix of experienced artists and new-comers with exciting stories to tell. From tales of a ten-day vows of silence to what it\'s like growing up with deaf parents and siblings, this year\'s festival is a potpourri of styles and stories.';
$pdf->MultiCell(0, 4, $summary , 0, 'J');

//display the main content
$pdf->SetFont('Helvetica','',10);
$maincontent = '
    First line.
    Second Line?
    <ul>
        <li>
        Item 1
        </li>
    </ul>
    ';
$pdfhtml->WriteHTML($maincontent);


//Output the document
$pdf->Output('example1.pdf','I');

【问题讨论】:

  • 找出为什么 $this-&gt;FontSize 设置为零并修复它?顺便说一句,您的if 声明提到了$thisFontSize,我很确定这是$this-&gt;FontSize 的一个单独变量。
  • 谢谢,这是问题中的错字,在我的代码中是正确的。我会在上面修复它。是的,我一直在试图弄清楚为什么它没有运气就设置为 0。我希望对 FPDF 有更多经验的人能够启发我。
  • 您能否提供一个指向您正在使用的 WriteHTML 插件的链接?
  • 您可以使用comment replies 将评论指向用户。这样,用户就会收到有关您的评论的通知。

标签: php pdf pdf-generation fpdf


【解决方案1】:

WriteHTML add-on 是一个名为PDF_HTML 的类,它扩展了原来的FPDF。要使用附加功能,您必须实例化子类并使用它:

$pdfhtml=new PDF_HTML();

您不需要父类的附加实例 ($pdf)。删除它并将$pdf 的所有引用更改为$pdfhtml,您就可以开始了:

<?php
define('FPDF_FONTPATH', '../Classes/FPDF/font/');
require ('../Classes/FPDF/fpdf.php');
require ('writeHtml.php');


//create a PDF_HTML object
$pdfhtml=new PDF_HTML();

//set document properties
$pdfhtml->SetAuthor('Author Name');
$pdfhtml->SetTitle('PRESS RELEASE - NAME OF SHOW');

//set font for the entire document
$pdfhtml->SetFont('Helvetica','B',10);
$pdfhtml->SetTextColor(0,0,0);

//set up a page
$pdfhtml->AddPage('P');
// $pdfhtml->SetDisplayMode(real,'default'); //<-- commented this line, what is real?

//display the top block
$contact = 'Contact Name';
$addline1 = '4002 2nd Ave NE, #2';
$addline2 = 'Address Line 2';
$cityzip = 'Seattle, WA 98105';
$pdfhtml->Cell(0, 4, 'PRESS RELEASE', 0, 0, 'L');
$pdfhtml->Cell(0, 4, 'FOR IMMEDIATE RELEASE', 0, 1, 'R');
$pdfhtml->Cell(0, 4, 'CONTACT: '.$contact, 0, 0, 'L');
$pdfhtml->Cell(0, 4, 'KILL DATE: August 1, 2014', 0, 1, 'R');
$pdfhtml->Cell(0, 4, $addline1, 0, 1, 'L');
$pdfhtml->Cell(0, 4, $addline2, 0, 1, 'L');
$pdfhtml->Cell(0, 4, $cityzip, 0, 1, 'L');


//display the title
$pdfhtml->SetFontSize(20);
$pdfhtml->Cell(0,10,'PRESS RELEASE TITLE',0,1,'C',0);

//display the sub-title
$pdfhtml->SetFontSize(16);
$pdfhtml->Cell(0,10,'The Subtitle',0,1,'C',0);

//display the italic summary
$pdfhtml->SetXY(10,55);
$pdfhtml->SetFont('Helvetica','I',10);
$summary = 'SEATTLE - Theatre Off Jackson presents SPF 8, the annual exhibition of solo    performance. Featuring four featured performers and one shorts night, the festival will occur between February 6th and March 1st, 2014. This year\'s festival is an exciting mix of experienced artists and new-comers with exciting stories to tell. From tales of a ten-day vows of silence to what it\'s like growing up with deaf parents and siblings, this year\'s festival is a potpourri of styles and stories.';
$pdfhtml->MultiCell(0, 4, $summary , 0, 'J');

//display the main content
$pdfhtml->SetFont('Helvetica','',10);

$maincontent = '
    First line.
    Second Line?
    <ul>
        <li>
        Item 1
        </li>
    </ul>
    ';
$pdfhtml->WriteHTML($maincontent);


//Output the document
$pdfhtml->Output('example1.pdf','I');

请注意,我更改了顶部的包含路径。我还评论了你打电话给SetDisplayMode 的那一行。

【讨论】:

  • 完美!非常感谢。 FPDF 对我来说仍然有点神秘,但你的回答让我有了一些见解(另外,它有效!)。虽然我确实注意到渲染的 html 没有以任何样式或格式显示,所以我会研究如何让它更好地工作,但这至少让我解决了功能问题。
  • 哦,另外,SetDisplayMode 设置为 real 意味着 PDF 的默认视图将是文档实际尺寸的 100%,据我所知。虽然看起来没有它也能正常工作。
  • 好的,那么您缺少字符串 real 周围的引号。
【解决方案2】:

您的 pdf html 中可能有一些元素,例如不平衡。某些元素可能尚未关闭或启动。 我花了 2 天时间才找到这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2013-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多