2013-04-11 58 views
0
<?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); 
?> 

錯誤是什麼:致命錯誤不知道錯誤

 
Fatal error: Uncaught exception 'PDFlibException' with message 'Function must not be called in 'object' scope' in /var/www/Sample/sam.php:4 
Stack trace: 
#0 /var/www/Sample/sam.php(4): pdf_begin_page(Resource id #2, 595, 842) 
#1 {main} thrown in /var/www/Sample/sam.php on line 4 
+0

這幾乎是同樣的問題:http://stackoverflow.com/questions/997627/pdflib-giving-an-uncaught-exception-error做到這一點解決你的問題? – Xavjer 2013-04-11 13:29:10

+0

希望這可以幫助你http://php.net/manual/en/function.pdf-open-file.php – Rikesh 2013-04-11 13:30:08

+0

@Xavjer還有問題沒有解決它看起來像沒有答案的問題 – user1370510 2013-04-11 13:35:57

回答

0

的問題是,pdf_open_file()與被忽略的/沒有檢查返回錯誤。

當您嘗試執行pdf_begin_page()雖然沒有有效的打開文件時PDFlib會引發異常。

這裏從手工的PDFlib的解釋:

的PDFlib應用程序必須遵守這很容易理解的結構規則。例如,你顯然在結束之前開始一個文檔。 PDFlib使用嚴格的範圍系統強制執行函數調用的正確順序。範圍定義可以在PDFlib手冊中找到(表1.3)。所有的API函數描述都爲每個函數指定了允許的範圍。在允許的作用域之外調用一個函數會導致異常。您可以使用PDF_get_option()的範圍查詢當前範圍。

另外,建議使用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; 
?>