Php 在yii2中,关于使用TCPDF库,我得到了;未能加载PDF文档。”;错误

Php 在yii2中,关于使用TCPDF库,我得到了;未能加载PDF文档。”;错误,php,pdf,yii2,tcpdf,Php,Pdf,Yii2,Tcpdf,我正在尝试生成示例pdf文件,如下所示: 输出---> myController public function actionTcpdf() { Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; Yii::$app->response->headers->add('Content-Type','application/pdf'); return

我正在尝试生成示例pdf文件,如下所示:

输出--->

myController

public function actionTcpdf()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_RAW;
    Yii::$app->response->headers->add('Content-Type','application/pdf');

        return $this->render('tcpdf');
    }
查看

<?php

/**
 * @copyright Copyright &copy;2014 Giandomenico Olini
 * @company Gogodigital - Wide ICT Solutions 
 * @website http://www.gogodigital.it
 * @package yii2-tcpdf
 * @github https://github.com/cinghie/yii2-tcpdf
 * @license GNU GENERAL PUBLIC LICENSE VERSION 3
 * @tcpdf library 6.0.075
 * @tcpdf documentation http://www.tcpdf.org/docs.php
 * @tcpdf examples http://www.tcpdf.org/examples.php
 */
// echo "12345";
// exit();

// Load Component Yii2 TCPDF 
\Yii::$app->get('tcpdf');

// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Gogogital.it');
$pdf->SetTitle('Yii2 TCPDF Example');
$pdf->SetSubject('Yii2 TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'Yii2 TCPDF Example', 'Gogodigital - Wide ICT Solutions | gogodigital.it', array(0,64,255), array(0,64,128));
$pdf->setFooterData(array(0,64,0), array(0,64,128));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// ---------------------------------------------------------

// set default font subsetting mode
$pdf->setFontSubsetting(true);

// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
$pdf->SetFont('dejavusans', '', 14, '', true);

// Add a page
// This method has several options, check the source code documentation for more information.
$pdf->AddPage();

// set text shadow effect
$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));

// Set some content to print
$html = "<h1>Yii2 TCPDF Works Fine!</h1>";

// Print text using writeHTMLCell()
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

// ---------------------------------------------------------

// Close and output PDF document
// This method has several options, check the source code documentation for more information.
$pdf->Output('yii2_tcpdf_example.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+

// Close Yii2
\Yii::$app->end();

?>


这是视图代码。我在不同的地方进行了回音,效果很好,但是当我在视图中回音时,我在开始中为pdf创建了一个新变量,回音没有出现,而是出现了错误。

你确定你的临时目录具有完全的读/写权限吗?我在哪里可以检查这些权限?如果你有根访问权限,你可以直接在文件上检查它系统。在linux系统上通常是/tmp或/var/tmp。您可以检查phpinfo()的输出以查看tmp目录的位置。但是问题在视图中,我会在不同的位置回显以查看问题所在,所以当我在视图中回显“$pdf=new TCPDF(pdf_PAGE_ORIENTATION,pdf_UNIT,pdf_PAGE_FORMAT,true,'UTF-8',false);”行后的回显不会出现错误。$pdf->output('myfilename.pdf','I');将从控制器将pdf输出到浏览器,这就是我正在使用的,并且工作正常。