我正在尝试将PDFlib支持到PHP中,但在最终弄清楚如何安装PDFlib之后,我收到了这个错误:
Fatal error: Uncaught exception 'PDFlibException' with message 'Function must not be called in 'object' scope'
使用php.net上的示例代码:
<?php
// create handle for new PDF document
$pdf = pdf_new();
// open a file
pdf_open_file($pdf, "test.pdf");
// start a new page (A4)
pdf_begin_page($pdf, 595, 842);
// get and use a font object
$arial = pdf_findfont($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);
// print text
pdf_show_xy($pdf, "There are more things in heaven and earth, Horatio,",50, 750);
pdf_show_xy($pdf, "than are dreamt of in your philosophy", 50,730);
// end page
pdf_end_page($pdf);
// close and save file
pdf_close($pdf);
?>
有没有人对可能导致这种情况的原因有任何想法?我试过谷歌搜索,但我一直无法找到任何解决方案。
答案 0 :(得分:1)
您使用的是哪个版本的PDFLib?如果它是6.0或更高,请尝试以下代码:
<?php
// create handle for new PDF document
$pdf = PDF_new();
// open a file
PDF_begin_document($pdf, "test.pdf");
// start a new page (A4)
PDF_begin_page_ext($pdf, 595, 842);
// get and use a font object
$arial = PDF_load_font($pdf, "Arial", "host", 1); pdf_setfont($pdf, $arial, 10);
// print text
PDF_show_xy($pdf, "There are more things in heaven and earth, Horatio,",50, 750);
PDF_show_xy($pdf, "than are dreamt of in your philosophy", 50,730);
// end page
PDF_end_page_exit($pdf);
// close and save file
PDF_end_document($pdf);
?>
函数pdf_open_file, pdf_begin_page, pdf_findfont, and pdf_close
全部弃用。
答案 1 :(得分:0)
或者以“硬”而非常不好的方式 - 尝试将代码移动到全局范围。
答案 2 :(得分:0)
请检查您创建文件的路径。
pdf_open_file($pdf, "test.pdf");
确保路径正确并且错误消失。
答案 3 :(得分:0)
检查您所在位置的许可。我的做法是固定的。它应该具有写入权限。
chmod 0777 -R <PATH>
-R是递归的
路径当然是存储在
中pdf_open_file($pdf, "test.pdf");
in $ pdf。