嗨社区我正在使用插件CakePdf与库tcpdf,并在生成pdf时它显示以下错误
Error:
Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69
Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69
Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69
Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69
Warning: htmlspecialchars() [function.htmlspecialchars]: charset `ASCII' not supported, assuming utf-8 in G:\Trabajos_Web_PHP\diplomas\vendor\cakephp\cakephp\src\Core\functions.php on line 69
Warning (2): htmlspecialchars() [<a href='http://php.net/function.htmlspecialchars'>function.htmlspecialchars</a>]: charset `ASCII' not supported, assuming utf-8 [CORE\src\Core\functions.php, line 69]
我的配置是这样的
Plugin::load('CakePdf', ['bootstrap' => true]);
Configure::write('CakePdf', [
'engine' => 'CakePdf.Tcpdf',
'encoding' => 'UTF-8'
'download' => true
]);
在我的行动中生成pdf是这样的
public function pdfdo($names = null) {
$file = new File(WWW_ROOT.'bd/'.'base_datos_do.json');
$json = $file->read(TRUE,'r');
$config = json_decode($json,TRUE);
$this->set('config',$config);
$persons = explode(',', $names);
$this->set('lastnames',$persons);
$this->viewBuilder()->setLayout('ajax');
$this->viewBuilder()->setTemplate('pdf/pdfdo');
$this->response->withType('application/pdf');
}
在我的模板里面配置就是这样,也应用函数mb_internal_encoding('UTF-8');重置enconding但仍然继续错误
$pdf = new TCPDF('L',PDF_UNIT,PDF_PAGE_FORMAT,TRUE,'UTF-8',FALSE);
$pdf->SetCreator(PDF_CREATOR);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// build my pdf
// finalization of my pdf
mb_internal_encoding('UTF-8');
$pdf->Output('Diplomas-DO.pdf', 'D');
header('Content-Type: application/pdf; charset=utf-8');
请帮助我好几天了解问题谢谢。
我最近用TCPDF制作了一个pdf并遇到了同样的问题。看起来您正在使用TCPDF引擎直接构建PDF。
当CakePHP在PDF输出开始之前抛出错误时会发生此错误...例如,它可能是“试图获取非对象的属性......”错误或类似的东西。您应该能够在htmlspecialchars()警告下面看到特定的错误消息信息。
我建议先检查一下,确保你的pdf工作正常...而不是你//构建我的pdf代码,做一个简单的行
$pdf->setXY(13, 13);
$pdf->Write(5, 'Test Hello');
如果这样可行,那么您的配置正在运行,错误很可能出现在您的变量中,因此请逐步开始构建您的pdf,然后进行测试。
我还要补充一点,我也选择直接使用TCPDF引擎,所以我没有使用CakePDF插件(效果很好,但不能满足我对这个特殊问题的需求)。如果需要,我可以提供更多相关信息。
编辑:
我将提供一些关于我如何在没有CakePDF的项目中直接使用TCPDF的信息,以防您或任何人发现它有用。
首先,我想直接使用TCPDF引擎有以下几个原因:
所以我直接用composer安装了TCPDF
composer require tecnickcom/tcpdf
将其添加到app / vendor / cakephp-plugins.php
'Tecnickcom/Tcpdf' => $baseDir . '/vendor/tecnickcom/tcpdf/'
然后在app / config / bootstrap.php中
Plugin::load('Tecnickcom/Tcpdf', ['bootstrap' => true]);
然后在app / config / routes.php中
Router::extensions(['pdf']);
然后在app / src / controller / mycontroller.php中,我创建了方法outputpdf。在那个方法中,我设置了要在pdf中使用的所有数据集合,然后
$this->viewBuilder()->template('mypdf');
然后在app / src / template / mycontroller / pdf / i中创建了mypdf.php。该文件仅包含以下代码:
header("Content-type:application/pdf");
$this->layout = 'mypdf';
然后在app / src / template / layout / pdf /我创建了文件mypdf.php。在这个文件中,我使用控制器中的数据构建了PDF。
header("Content-type:application/pdf");
// Extend the TCPDF class to create custom Header and Footer
class MYPDF extends TCPDF {
//And build the header and footer in here
}
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
//And make all the body content here
$pdf->Output('mypdf.pdf', 'I');
这种方法的一个缺点是使用外语字体,您需要在app / vendor / tecnickcom / tcpdf / fonts文件夹中添加和使用所需的字体,这些都可用于您的pdf。
请随意批评或建议改进这种方法。
我发现错误是我在pdf中使用的图像,其中一个我用作pdf的背景而另一个就像一个小图像。
$pdf = new TCPDF('L',PDF_UNIT,PDF_PAGE_FORMAT,TRUE,'UTF-8',FALSE);
$pdf->SetCreator(PDF_CREATOR);
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$fontname = TCPDF_FONTS::addTTFfont(WWW_ROOT.'font'.DS.'Mada'.DS.'Mada-Regular.ttf', 'TrueTypeUnicode', '', 96);
$tagvs = array(
'div'=> array(
0 => array('h'=>0,'n' => 0),
1 => array('h'=>0,'n' => 0)),
'p'=> array(
0 => array('h'=>0,'n' => 0),
1 => array('h'=>0,'n' => 0)),
'h2' => array(
0 => array('h'=>0,'n' => 0),
1 => array('h'=>0,'n' => 0)),
'img' => array(
0 => array('h'=>0,'n' => 0),
1 => array('h'=>0,'n' => 0)
)
);
//variable that has small image
$imglogo = WWW_ROOT.'logos'.DS.'logoempresa.png';
foreach ($lastnames as $names) {
$pdf->AddPage();
$bMargin = $pdf->getBreakMargin();
$auto_page_break = $pdf->getAutoPageBreak();
$pdf->SetAutoPageBreak(false, 0);
//image for background
$img = WWW_ROOT.'img'.DS.'Diploma_DO.png';
$pdf->Image($img, 0, 0, 300, 210, 'png', '', '', false, 600, '', false, false, 0);
$pdf->SetAutoPageBreak($auto_page_break, $bMargin);
$pdf->setPageMark();
$pdf->setHtmlVSpace($tagvs);
$html_title = '<table cellspacing="0">'
. '<tr style="text-aling:center;line-height:11px">'
. '<td style="font-size: 37pt;font-weight: 600;color: #034bdb;color:#003275">'.$names.'</td>'
. '</tr>'
. '</table>';
$html_text_content = '<div style="text-align: center">'
. '<p style="color:#333;font-size: 16px;text-align: center">Ha completado con éxito el '.$config["Nombre-Taller-Curso"].',</p>'
. '<p style="color:#333;font-size: 16px;text-align: center">efectuada el '.$config["Fecha-Inicio-Fin"].' de '.$config["Mes-Ano"].' con una duración de '.$config["Horas"].' Horas.</p>'
. '</div>';
$html_text_content_bussines = '<div style="text-align: center">'
. '<p style="color:#333;font-size: 16px;text-align: center">Este taller ha sido diseñado especialmente para '.$config["Empresa"].'.</p>'
. '</div>';
$html_text_content_close = '<div style="text-align: center">'
. '<p style="color:#333;font-size: 16px;">'.$config["Fecha-Curso-Ubicacion"].'</p>'
. '</div>';
//img tag that contains the small image
$html_logo_bussines = '<img src="'.$imglogo.'" width="150" height="100">';
$pdf->SetFont($fontname, 'B', 26, '',false);
$pdf->writeHTMLCell(300,0,0,78,$html_title, '', 1, 0, true, 'C',true);
$pdf->SetFont($fontname,'',14,'',false);
$pdf->writeHTMLCell(300, 0, 0, 88, $html_text_content, '', 1, 0, true, 'C', true);
$pdf->writeHTMLCell(300, 0, 0, 109, $html_text_content_bussines, '', 1, 0, true, 'C', true);
$pdf->writeHTMLCell(300,0,0,125,$html_text_content_close,'',1,0,true,'C',true);
//use of the small image
$pdf->writeHTMLCell(300,0,0,155,'<div style="text-align:center">'.$html_logo_bussines.'<div>',0,0,0,true,'C',true);
$pdf->lastPage();
}
错误继续,错误停止显示我评论行所在的位置
$pdf->writeHTMLCell(300,0,0,155,'<div style="text-align:center">'.$html_logo_bussines.'<div>',0,0,0,true,'C',true);
我不知道我做错了什么我读了文档和这个函数,如果你接受img标签。
你可以调试你的$ imglogo变量来查看文件路径是否正确吗?
或者,尝试使用$ pdf-> Image()函数显示图像?
请注意,TCPDF在vendor \ tecnickcom \ tcpdf \ config \ tcpdf_config.php中有一个配置选项:
define ('K_PATH_IMAGES', 'C:\\windowsfolder\\htdocs\\app\\webroot\\img\\');
因此,您可以通过以下方式调用PDF中的图像:
$image_file = K_PATH_IMAGES.'imagefile.jpg';
看看是否有效......