PHP FPDF - “在非对象上调用成员函数 Stuff()”

发布于 2024-12-07 17:42:54 字数 1487 浏览 1 评论 0原文

所以基本上我一直在做的就是使用 wamp 将一个 xml 文件转换为格式良好的 pdf。 下面的代码是我的想法的简单说明。我从将 xml 解析为 html 的过程中学到了一些知识,我认为这就是告诉 xml 解析器您希望在屏幕上显示的方式。所以我像下面那样做了:

<?php 
 require('fpdf.php');

 class PDF extends FPDF
 {
   //...I skipped couple of functions like Footer()

   //Display stuff from string in pdf manner
   function Stuff($data)
   {
       $this->SetFont('Arial','',12);
       $this->Cell(0,4,"$data",0,1,'L',true);
       $this->Ln(1);
   }
 }

 //Parse xml start tags
 function start($parser,$element_name,$element_data)
 {
      ......
 }    

 //Parse xml end tags
 function stop($parser,$element_name)
 {
      ......
 }

 //Parse xml contents between start tags and end tags
 function data($parser,$data)
 {
      $pdf->Stuff($data);
 }

 //create pdf page
 $pdf = new PDF();
 $pdf->AliasNbPages();
 $pdf->AddPage();
 $pdf->SetFont('Times','',12);

 //Parse xml file to pdf
 $parser=xml_parser_create();
 xml_set_element_handler($parser,"start","stop");
 xml_set_character_data_handler($parser,"char");
 $fp=fopen("test.xml","r");

 while($data=fread($fp,4096))
 {
     xml_parse($parser,$data,feof($fp)) or 
     die (sprintf("XML Error:$s at line $d",
     xml_error_string(xml_get_error_code($parser)),
     xml_get_current_line_number($parser)));
 }

 xml_parser_free($parser);

?>

不幸的是,我收到了这个警告,正如我在标题中描述的那样。但是,我分别使用开始、停止和数据函数测试了我的 xml 解析器函数,它们都工作正常。然后我尝试将 xml 转换为 html,一切仍然很好。这是主要是转换为pdf,我在研究我的想法时遇到这种情况。 有人能给我一些关于这个问题的提示吗?

So basically what I have been doing is convert one xml file to a well-formed pdf with wamp.
The codes below are simple illustration of my idea.I learnt from the process of parsing xml to html and I suppose that the thing is all about telling xml parser the ways you would like to display on the screen.So then I did it like below:

<?php 
 require('fpdf.php');

 class PDF extends FPDF
 {
   //...I skipped couple of functions like Footer()

   //Display stuff from string in pdf manner
   function Stuff($data)
   {
       $this->SetFont('Arial','',12);
       $this->Cell(0,4,"$data",0,1,'L',true);
       $this->Ln(1);
   }
 }

 //Parse xml start tags
 function start($parser,$element_name,$element_data)
 {
      ......
 }    

 //Parse xml end tags
 function stop($parser,$element_name)
 {
      ......
 }

 //Parse xml contents between start tags and end tags
 function data($parser,$data)
 {
      $pdf->Stuff($data);
 }

 //create pdf page
 $pdf = new PDF();
 $pdf->AliasNbPages();
 $pdf->AddPage();
 $pdf->SetFont('Times','',12);

 //Parse xml file to pdf
 $parser=xml_parser_create();
 xml_set_element_handler($parser,"start","stop");
 xml_set_character_data_handler($parser,"char");
 $fp=fopen("test.xml","r");

 while($data=fread($fp,4096))
 {
     xml_parse($parser,$data,feof($fp)) or 
     die (sprintf("XML Error:$s at line $d",
     xml_error_string(xml_get_error_code($parser)),
     xml_get_current_line_number($parser)));
 }

 xml_parser_free($parser);

?>

Unfortunately I received this warning as I described in the title.However,I tested my xml parser functions with my start,stop and data functions individually,they all worked fine.Then I tried to convert xml to html,everything is still good.It is primarily converting to pdf that I encounter this situation when working on my idea.
Could anyone give me some hints about this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难忘№最初的完美 2024-12-14 17:42:54

在您的数据函数中,您引用了 $pdf->Stuff() 但没有变量 $pdf,因此这就是您看到错误的原因。因为 $pdf 不是一个对象,因为它不存在。您的意思是 $parser->Stuff($data) 吗?或者如果该函数位于您的类内部,那么也许应该是 $this->Stuff($data)

您可以在您的类中拥有处理程序,您只需要稍微不同地指定它们。你可以做

xml_set_object ( $parser, $this );
xml_set_element_handler ( $parser, 'start', 'stop' );
xml_set_character_data_handler ( $parser, 'char' );

In your data function, you reference $pdf->Stuff() but there is no variable $pdf so that is why you see the error. Because $pdf is not an object because it doesn't exist. Did you mean $parser->Stuff($data)? or if that function is inside of your class, then maybe it should be $this->Stuff($data)

You can have the handlers in your class, you just need to specify them a bit differently. You can do

xml_set_object ( $parser, $this );
xml_set_element_handler ( $parser, 'start', 'stop' );
xml_set_character_data_handler ( $parser, 'char' );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文