【问题标题】:fatal error in php pdf creation don't know what the errorphp pdf创建中的致命错误不知道错误是什么
【发布时间】:2013-04-11 13:26:19
【问题描述】:
<?php
$pdf=pdf_new();
pdf_open_file($pdf, "test.pdf");
pdf_begin_page($pdf, 595, 842);
$arial = pdf_findfont($pdf, "Arial", "host", 1); 
pdf_setfont($pdf, $arial, 10);
pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,",50, 750);
pdf_end_page($pdf);
pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50,730);
pdf_close($pdf);
?>

错误是:

致命错误:在 /var/www/Sample/sam.php:4 中带有消息“不得在“对象”范围内调用函数”的未捕获异常“PDFlibException” 堆栈跟踪: #0 /var/www/Sample/sam.php(4): pdf_begin_page(资源 id #2, 595, 842) #1 {main} 在第 4 行的 /var/www/Sample/sam.php 中抛出

【问题讨论】:

  • 这几乎是同一个问题:stackoverflow.com/questions/997627/… 这能解决您的问题吗?
  • @Xavjer 还有问题没有解决它看起来像未回答的问题
  • 请将您的问题更新为您的新代码和错误...因为第一个错误是因为您使用了折旧的 pdf_begin_page 而不是 PDF_begin_page_ext
  • 在 eclipse 中没有类似 PDF_begin_page_ext 的函数,我在上面的代码中使用 eclipse 它没有显示语法错误并且你建议的函数不存在

标签: php


【解决方案1】:

问题是 pdf_open_file() 返回一个被忽略/未检查的错误。

当您尝试执行 pdf_begin_page() 尽管没有有效的打开文件时,PDFlib 会引发异常。

这里是 PDFlib 手册的解释:

PDFlib 应用程序必须遵守某些易于理解的结构规则。例如,您显然在结束文档之前开始了它。 PDFlib 使用严格的范围系统强制执行函数调用的正确排序。范围定义可在 PDFlib 手册中找到(表 1.3)。所有 API 函数描述都指定了每个函数的允许范围。在允许的范围之外调用函数会导致异常。您可以使用 PDF_get_option() 的范围 keward 查询当前范围。

此外,建议使用 PDFlib 提供的当前示例作为起点,您可以在此处查看当前使用的 API 以及正确的错误处理。

一个完整的 PHP 示例如下所示:

<?php
/* $Id: hello.php,v 1.20 2013/01/24 16:58:59 rp Exp $
 *
 * PDFlib client: hello example in PHP
 */

try {
    $p = new PDFlib();

    # This means we must check return values of load_font() etc.
    $p->set_option("errorpolicy=return");

    /* all strings are expected as utf8 */
    $p->set_option("stringformat=utf8");

    /*  open new PDF file; insert a file name to create the PDF on disk */
    if ($p->begin_document("", "") == 0) {
        die("Error: " . $p->get_errmsg());
    }

    $p->set_info("Creator", "hello.php");
    $p->set_info("Author", "Rainer Schaaf");
    $p->set_info("Title", "Hello world (PHP)!");

    $p->begin_page_ext(595, 842, "");

    $font = $p->load_font("Helvetica-Bold", "unicode", "");
    if ($font == 0) {
        die("Error: " . $p->get_errmsg());
    }

    $p->setfont($font, 24.0);
    $p->set_text_pos(50, 700);
    $p->show("Hello world!");
    $p->continue_text("(says PHP)");
    $p->end_page_ext("");

    $p->end_document("");

    $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=hello.pdf");
    print $buf;

}
catch (PDFlibException $e) {
    die("PDFlib exception occurred in hello sample:\n" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "\n");
}
catch (Exception $e) {
    die($e);
}

$p = 0;
?>

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    相关资源
    最近更新 更多