我正在使用 MPDF 来生成 PDF。我必须使用 Tahoma 字体。MPDF 通过 Composer 安装,版本为 7+。
我使用以下代码:
$defaultConfig = (new Mpdf\Config\ConfigVariables())->getDefaults();
$fontDirs = $defaultConfig['fontDir'];
$defaultFontConfig = (new Mpdf\Config\FontVariables())->getDefaults();
$fontData = $defaultFontConfig['fontdata'];
$mpdf = new \Mpdf\Mpdf([
'mode' => 'en',
'default_font_size' => 10,
'margin_left' => 9.5,
'margin_right' => 0,
'margin_top' => 42.5,
'margin_bottom' => 0,
'margin_header' => 7.1,
'margin_footer' => 2,
'fontDir' => array_merge($fontDirs, [__DIR__ . '/customFontsMPDF']),
'fontData' => $fontData + [
'tahoma' => [
'R' => 'tahoma.ttf',
'B' => 'tahomabd.ttf'
]
]
]);
Run Code Online (Sandbox Code Playgroud)
我输入以下内容以添加 CSS:
$mpdf->WriteHTML('<style>' . …Run Code Online (Sandbox Code Playgroud) 它不会在pdf中显示ite1值.为什么?你能不能纠正我的错误?
<?php
set_include_path($_SERVER['DOCUMENT_ROOT'] .'/mpdf/');
require('mpdf.php');
$n = 0;
$mpdf=new mPDF();
require_once('../../includes/common.php');
$result = mysql_query("SELECT * from sgc where year = '2010'") or die(mysql_error());
$row=mysql_fetch_array($result);
$nric=$row['nric'];
$row=mysql_fetch_array($result);
$meta_ite=$row['meta_ite'];
$mpdf->WriteHTML('<p>Hello World</p>');
$html1 = '<table>
<tr>
<td width="388">Module Description</td>
<td width="111"><div align="center">Grade</div></td>
<td width="92"><div align="center">Earned</div></td>
<td width="96"><div align="center">Authority</div></td>
<td width="74"><div align="center">Series</div></td>
</tr>';
$unser = $meta_ite;
$unser = unserialize($unser);
$count = count($unser);
for($j=0;$j<$count;$j++)
{
$ite1 = $unser[$i][0];
//print_r ($unser[$i][0]."--".$unser[$i][1]."--".$unser[$i][2]);
$html1 .= '<tr><td width="388">'.$nric.'</td>
<td width="111">'.$count.'</td>
<td width="92"><div align="center">'.$ite1.'</div></td>
<td width="96">ITE</td>
<td width="74">2010</td>
</tr>';
} …Run Code Online (Sandbox Code Playgroud) 我使用php和mpdf生成特定的pdf.我在pdf中有一些饼图.为此,我正在使用pChart.我正在动态生成饼图以便以pdf格式呈现.
在我的pdf中,所有静态图像都正确呈现,但没有动态生成的饼图.
这个(静态)有效,
<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/color-block2.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>
Run Code Online (Sandbox Code Playgroud)
但不是这个(动态的)
<div class="chart">
<?php echo CHtml::image(Yii::app()->baseUrl.'/images/pie.png', 'Logo screenshot', array('class' => 'logo_image')); ?>
</div>
Run Code Online (Sandbox Code Playgroud)
Ps:我正在使用Yii url规则获取饼图的干净网址.
当我将pdf_template渲染为html时,两个图像都被正确渲染.
如何在没有$mpdf->Image()方法的情况下使用mpdf实现动态图像.
我怀疑我不能使用$mpdf->Image()从html生成pdf.
更新:
后
$mPDF->showImageErrors = true;
Run Code Online (Sandbox Code Playgroud)
我越来越
mPDF error: IMAGE Error (http://csrt.dev/images/pie.png): Could not find image file
Run Code Online (Sandbox Code Playgroud) 嗨,我使用mpdf生成pdfs.现在我在html中遇到表边框问题.我在html中有以下表格样式
<table border="10" cellpadding="1" cellspacing="1" style="width: 500px;">
<tbody>
<tr>
<td>Test header</td>
<td>Test header</td>
</tr>
<tr>
<td>body</td>
<td>body</td>
</tr>
<tr>
<td>body</td>
<td>body</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
因此,生成的pdf不符合表格的边框大小.我附上了pdf.请checj
我在我的系统上有一个mPDF报告,并且在报告中有一个页眉和页脚,我可以在其中使用$mpdf->setHeader();&$mpdf->setFooter();设置页眉和页脚。但在页眉中显示底边框,在页脚中显示顶边框。谁能帮我如何删除此文件?
图像如下:
这是我的代码:
$mpdf=new mPDF('','LETTER-L','','',35,35,60,25,10,10);
//left margin, right margin, body, top margin,top margin,bottom margin,
/*HEADER Monthly Accomplishment Report*/
$mpdf->SetHeader('Sample');
/*FOOTER xMonthly Accomplishment Report*/
$mpdf->SetFooter('Sample');
//==============================================================
//=====================FILE DESCRIPTION=========================
//==============================================================
$mpdf->SetTitle('Monthly Accomplishment Report');
$mpdf->SetSubject('Report');
$mpdf->SetAuthor('sample');
$mpdf->Output('sample.pdf','I');
exit;
//==============================================================
//==============================================================
//==============================================================
Run Code Online (Sandbox Code Playgroud) 我知道如何使用mPDF库生成PDF并使用PHPMailer将其作为附件发送-像这样:
...
$emailAttachment = $mpdf->Output('file.pdf', 'S');
$mail = new PHPMailer();
$mail->AddStringAttachment($emailAttachment, 'file.pdf', 'base64', 'application/pdf');
...
Run Code Online (Sandbox Code Playgroud)
但是,如果我在单独的PHP文件中生成PDF(不应修改),该怎么办-像这样-invoice.php:
...
$mpdf = new mPDF();
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
Run Code Online (Sandbox Code Playgroud)
如何使用PHPMailer附加此动态创建的PDF文件?我尝试了这个:
$mail = new PHPMailer();
...
$mail->addStringAttachment(file_get_contents('invoice.php'), 'invoice.pdf', 'base64', 'application/pdf');
$mail->send();
Run Code Online (Sandbox Code Playgroud)
电子邮件发送的内容正确,但是PDF附件已损坏,因此无法显示。如何正确编码?我尝试了其他几种方法,但文件已损坏或根本没有连接(在一种情况下,整个电子邮件正文为空)。
提前谢谢你的帮助!:)
使用mPDF(最新版本)显示GIF扩展图像时,我面临以下错误。
除了gif,所有其他扩展名均受支持。
错误:
mPDF错误:IMAGE错误(tiger.gif):解析使用GD库创建的临时文件图像对象以解析GIF图像时出错
我尝试了谷歌为此提供的许多链接,但它们无法正常工作。
任何建议将不胜感激。
谢谢。
我尝试以PDF格式打印证书,但是当我将代码推送到暂存时,它说
Temporary files directory "/var/www/protected/vendor/mpdf/mpdf/src/Config/../../tmp" is not writable
Run Code Online (Sandbox Code Playgroud)
我不确定如何更改权限以及如何更改自定义目录。
这是我单击以获取证书的按钮的代码:
<a class="btn btn-sd btn-sd-ghost btn-sd-ghost-black margin-right-lg" href="<?php echo $this->createUrl('//idea/frontend/pdf', array('id'=>$model->id))?>" target="_blank">Get Your Certificate<i class="icon-right-small"></i></a>
<?php endif; ?>
Run Code Online (Sandbox Code Playgroud)
这是控制器:
public function actionPdf($id){
$model = HUB::getOrganization($id);
$orgtitle = $model->title;
$mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4-L']);
$mpdf->WriteHTML("<html><body style='background-image:url(\"/images/cert-idea.jpg\"); background-image-resize: 5; background-position: top center;'></body></html>");
$mpdf->WriteHTML("<div style='text-align:center; display:table; height:100%; width:100%; padding-top:28%;'><h1 style='display:table-cell; vertical-align:middle; font-size:40px;'>".$orgtitle."</h1></div>");
$mpdf->Output('IDEA-CERT-'.$orgtitle.'.pdf', 'I');
}
Run Code Online (Sandbox Code Playgroud)
希望有人可以帮助我解决这个问题。谢谢!
我正在开发使用MPDF库从HTML代码生成PDF的代码。对于HTML代码,我正在读取外部HTML文件。但是它不适用于较大的HTML代码大小。有什么办法可以解决它,或者我们还有其他支持我功能的库吗?
对于较大的html文件,显示错误:
致命错误:未捕获的Mpdf \ MpdfException:HTML代码大小大于pcre.backtrack_limit1000000。您应使用字符串长度较小的WriteHTML()。
我需要使用mPDF生成发票,并且它们都将具有页眉,页脚和覆盖整个页面的相同背景图像。
我在网上看到了其他解决方案,但没有一个对我有用。
这些尝试是几次,所有这些都会导致失败(使用图像时图像不显示,50 *错误或图像覆盖了部分内容):
1-使用外部CSS文件并将其作为CSS内容注入
$pdf = new mPDF('es', 'A4', 0, '', 5, 5, 0, 0, 0, 0, 'P');
$pdf->useSubstitutions=false;
$pdf->setAutoTopMargin = 'stretch';
$pdf->SetDisplayMode('fullpage');
$css = file_get_contents('./styles.css');
$cabecera = file_get_contents('./header.html');
$cuerpo = file_get_contents('./body.html');
$pie = file_get_contents('./footer.html');
$pdf->SetHTMLHeader($cabecera);
$pdf->SetHTMLFooter($pie);
$pdf->WriteHTML($css, 1);
$pdf->WriteHTML($cuerpo, 2);
$pdf->Output();
Run Code Online (Sandbox Code Playgroud)
以及styles.css文件:
body {
background-image: url('background_300dpi.png') ;
}
Run Code Online (Sandbox Code Playgroud)
这将引发超时错误。如果我使用低分辨率图像背景,则可以使用,但会破坏我的布局。
2-尝试使用SetDefaultBodyCSS设置背景图像
$pdf = new mPDF('es', 'A4', 0, '', 5, 5, 0, 0, 0, 0, 'P');
$pdf->useSubstitutions=false;
$pdf->setAutoTopMargin = 'stretch';
$pdf->SetDisplayMode('fullpage');
$cabecera = file_get_contents('./cabecera.html');
$cuerpo = file_get_contents('./cuerpo.html');
$pie = file_get_contents('./pie.html'); …Run Code Online (Sandbox Code Playgroud)