繁体   English   中英

PDFLib工厂。 从类中的函数调用$ this-> pdf时出错

[英]PDFLib Factory. Error when calling $this->pdf from functions within class

想法如下,为了使PDF的创建更加简洁,我应该创建一个Factory / Abstract类,该类将具有一组预定义的函数,如下所示

class AbstractPDF{

    protected $pdf;
    protected $searchpath;

    public function __construct(){

        $this->pdf = PDF_new();
        $this->searchpath = "fonts/";

        pdf_set_option($this->pdf,"errorpolicy=return");

        pdf_set_option($this->pdf,"searchpath={" . $this->searchpath . "}");

        pdf_set_option($this->pdf,"stringformat=utf8");

    }

    protected function startAFourPage(){

        pdf_begin_page_ext($this->pdf, 0, 0, "width=a4.width height=a4.height");
                }



    /* When setting up any of the PDF content types, one should */
    /* remember that in PDFLib, x=>y axis start with 0(zero) at */
    /* lower left corner.                                       */
    /* The text line is set up in space by setting up the       */
    /* coordinates of the lower left corner and then providing  */
    /* height and width of the object as separate values        */  



protected function setupTextLine($xcoordinate, $ycoordinate, $width, $height,
                        $fontName, $fontEncoding, $fontSize, $text, $textPosition = "left"){

        //adding text directly through the PDFLib documentation
        $font = PDF_load_font($this->pdf, $fontName, $fontEncoding, "");
        PDF_setfont($this->pdf, $font, $fontSize);
        //PDF_set_text_pos($this->pdf, 25, 650);
        //PDF_show($this->pdf, $text);
        PDF_fit_textline ($this->pdf, $text, 111, 744, "boxsize {".$width." ".$height."} position=left");

    }


    /* When setting up any of the PDF content types, one should */
    /* remember that in PDFLib, x=>y axis start with 0(zero) at */
    /* lower left corner.                                       */
    /* The text flow is set up by providing the coordinate for  */
    /* lower left corner and upper right, as a rule.            */
    /* But overall PDFLib will placed it by coordinates for     */
    /* two corners diagonal to each other.                      */
    /* For this class we will identify these corners as         */
    /* lowLeft and upperRight                                   */


protected function setupMultilineTextflow($lowLeftX, $lowLeftY, $upperRightX, $upperRightY,
                        $fontName, $fontEncoding, $fontSize, $text){

        $orderDetails = 'Datum:
        Auftrags-NR:
        Auftragsname:
        Kunden-Nr:';

        $textFlow = PDF_create_textflow($this->pdf, $text, 
                    "fontname=".$fontName." 
                    fontsize=".$fontSize." 
                    encoding=".$fontEncoding);
        PDF_fit_textflow($this->pdf, $textFlow3, $lowLeftX, $lowLeftY, $upperRightX, $upperRightY,"");

    }

    protected function setupTable($headers=array('test'=>''), array $field){        
                }

}
?>

这个类是从下面调用的

class PDF extends AbstractPDF{

    public function __construct(){

        parent::__construct();

        parent::startAFourPage();

    }

    public function generateContent(){

        return "";

    }


}

对于有这么多代码,我深表歉意,但我想画一幅全景图。

问题是,当我尝试调用这些类时,出现以下错误:

2017/12/19 18:47:58 [错误] 465#465:* 252 FastCGI在stderr中发送:“ PHP消息:PHP致命错误:未捕获PDFlibException:不得在/ var / www的'object'范围内调用函数/dev-vm-stretch.de/htdocs/AbstractPDF.php:30

堆栈跟踪:

#0 /var/www/dev-vm-stretch.de/htdocs/AbstractPDF.php(30):pdf_begin_page_ext(资源ID#1、0、0,'width = a4.width ...')

#1 /var/www/dev-vm-stretch.de/htdocs/PDF.php(10):AbstractPDF-> startAFourPage()

#2 /var/www/dev-vm-stretch.de/htdocs/index.php(18):PDF-> __ construct()

#3 {main}

在从上游读取响应头的过程中,在第30行上的/var/www/dev-vm-stretch.de/htdocs/AbstractPDF.php中抛出:客户端:192.168.34.51,服务器:dev-vm-stretch.de,请求: GET /index.php HTTP / 1.1”,上游:“ fastcgi:// unix:/var/run/php/php7.0-fpm.sock:”,主机:“ dev-vm-stretch.de”

我认为这是因为我将与PDFLib相关的代码分成了函数,但是我不确定100%,因为我的PDFLib经验仅限于5天多一点,而且我还没有找到任何可解决此类问题的PDFLib教程。 :-(

欢迎就如何解决此错误提出任何想法。

问题很简单。 只有具有输出文档时,才能创建页面。 使用begin_document($file, $options);创建一个新的输出文档begin_document($file, $options); 您也可以使用__construct

您可以查看PDFlib 9.1.1 API参考的第1.2章“功能范围”。 另外,所有PDFlib PHP示例(包括PDFlib PHP软件包中的示例)都演示了正确的用法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM