我希望从一个包含图形和文本的初始 PDF 文件开始,然后采用一些具有某些用户输入动态值的 html 代码,将其生成为 PDF,希望可以使用初始 PDF 作为背景,或者之后以某种方式运行 PHP 脚本以“合并”两个 PDF,其中一个充当另一个的背景。
我有一些代码可以呈现 HTML 格式的 PDF:(使用 DOMPDF)
$initialpdf = file_get_contents('file_html.html');
$initialpdf = str_replace(array(
'%replaceText1%',
'%replaceText2%'
), array (
$replaceText1,
$replaceText2,
), $initialpdf);
$fp = fopen('file_html_new.html','w');
file_put_contents('file_html_new.html', $initialpdf);
require_once("dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
function pdf_create($html, $filename, $paper, $orientation, $stream=TRUE)
{
$dompdf = new DOMPDF();
$dompdf->set_paper($paper,$orientation);
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
@file_put_contents($filename . ".pdf", $pdf);
}
$filename = 'HTML_Generated_pdf';
$dompdf = new DOMPDF();
$html = file_get_contents('file_html_new.html');
pdf_create($html,$filename,'Letter','landscape');
Run Code Online (Sandbox Code Playgroud)
上面的代码采用 html 文件“file_html.html”并使用用户输入值进行字符串替换,将其呈现为名为“file_html_new.html”的新 HTML 文件,然后将其呈现为 PDF。
我还有其他 PHP 代码通过将 …
我已经检查了与这个问题相关的每个答案,但它没有用。
我想在页脚中添加页码,我已经尝试过下面的代码,但没有奏效。谁能告诉我我哪里出错了?
$dompdf = new Dompdf();
$dompdf->set_option("isPhpEnabled", true);
$html_content = "
<html>
<head>
<style>
@font-face {
font-family: kindergarten;
font-weight: normal;
font-style: normal;
src: url('fonts/kindergarten.ttf') format('truetype');
}
.test{ font-family: kindergarten; }
</style>
</head>
<body>
<script type='text/php'>
if ( isset($pdf) ) {
$pdf->page_script('
$font = $fontMetrics->get_font('Arial, Helvetica, sans-serif, 'normal');
$size = 12;
$pageText = 'Page 1';
$y = 15;
$x = 520;
$pdf->text($x, $y, $pageText, $font, $size);
');
}
</script>
<div>My Content goes here</div>
</body>
</html>
";
//echo $html_content; die;
$dompdf->loadHtml($html_content);
$dompdf->render();
Run Code Online (Sandbox Code Playgroud) 我使用dompdf从html生成pdf文件,问题是生成的filesize很大,即1页pdf的1MB!有没有办法减少它?
是否有类似的东西:
$dompdf = new DOMPDF();
$dompdf->resolution(200);
$dompdf->load_html($html);
$dompdf->render();
Run Code Online (Sandbox Code Playgroud)
而不是必须在dompdf_config.inc.php中设置,因为我有多个PDF文件,他们需要不同的DPI.
经过几个小时的搜索,我仍无法解决此问题.我在使用dompdf库生成的PDF文件中显示图像时遇到问题.
我有一个编辑器tinyMCE,用户可以通过按钮按下上传图像,编辑页面,查看PDF格式的预览.
这里有一件重要的事情:我在tinyMCE的配置中使用'convert_urls:true'来转换图像src =" https://mysite.com/public/images/image_name.png ",这是PDF库显示它们所必需的.
我设置了所需的dompdf配置:
在这里,我发现在使用dompdf更改附加的CSS后会出现:
img { display:block }
Run Code Online (Sandbox Code Playgroud)
一切都还是徒劳.
注意:在localhost上一切正常.但是在登台服务器上,我得到一个消息框,里面有一个十字架,上面写着"找不到图像"并显示图像路径.当我单击该路径时,图像已经存在于我的服务器上.我想知道为什么dompdf会给我这个消息?
我使用带有twig的Symfony2生成HTML,但是我生成的HTML不适用于Web浏览器,而是传递给将HTML转换为PDF文档的库.
我面临两个相关问题.
问题1.此PDF将包含图像,我不希望这些图像直接从互联网上可见(例如,我不希望这些图像显示在webroot下).通常我将资产放在"site/src/path-to-bundle/Resources/public/assets"中,但我不希望这个图像在Web上可见.任何人都可以建议一个好的文件夹,把"资产"放在那里,不能通过网络访问.(只是试图与Symfony2命名技术保持一致,以及其他程序员通常如何做到这一点)
问题2.由于我生成的HTML用于HTML到PDF生成器,因此在文件系统上引用具有绝对路径的图像是最实际的(在这种情况下,Web路径或URL不适合).
例如
<!-- This is the correct HTML code that is needed in this situation -->
<img src="/home/user/absoulte-file-system-path-to/image.png" />
<!-- These are actually incorrect in this situation -->
<img src="assets/image.png" />
<img src="http://www.somedomain.com/assets/image.png" />
Run Code Online (Sandbox Code Playgroud)
我想知道如何获得上面提到的图像的绝对路径(我不介意这是在Twig模板或控制器中完成的)
我正在使用一个PHP库Dompdf,将我的HTML文件转换为PDF。我已成功将单个HTML文件转换为PDF。
现在我有多个报告,我想将它们转换为PDF文件。
我这里有一个示例,其中我从数据库中获取一个表单,而直接从视图中获取另一个表单。
码:
$result['patients'] = $this->reports_model->get_xray_reports($id); // getting form from database
require_once "dompdf/dompdf_config.inc.php"; // include dompdf
$dompdf = new DOMPDF();
$dompdf->set_paper("A4");
$html = $this->load->view('wilcare/form_set_page_15',"",true);// loading direct from view
//this file is not converting
$html = $this->load->view('wilcare/report1',$result,true);//loading from db
$dompdf->load_html($html)
$dompdf->render();
$dompdf->stream("hello.pdf",array('Attachment'=>0));
Run Code Online (Sandbox Code Playgroud)
它仅转换一个文件。
我也尝试过这个:
$html = $this->load->view('wilcare/form_set_page_15',"",true);
$html1 = $this->load->view('wilcare/report1',$result,true);
$dompdf->load_html($html && $html1);
Run Code Online (Sandbox Code Playgroud) 使用dompdf时可以在文档信息字典中设置哪些PDF元数据?
最初在别处问过:
您是否能够解析在PDF生成期间添加到PDF信息的更多META信息?
/ Creator(DOMPDF)/ CreationDate(D:20150818031116-05'00')/ ModDate(D:20150818031116-05'00')
你能指定作者,版权等吗?
我找不到任何参考.只看到你的:创作者,创作日期和修改日期!
我从这里得到参考:https : //github.com/barryvdh/laravel-dompdf
我的控制器代码是这样的:
public function listdata()
{
$pdf=PDF::loadView('pdf.test_pdf');
return $pdf->stream('test_pdf.pdf');
}
Run Code Online (Sandbox Code Playgroud)
我尝试\config\dompdf像这样设置风景:
'show_warnings' => false, // Throw an Exception on warnings from dompdf
'orientation' => 'landscape',
Run Code Online (Sandbox Code Playgroud)
但是,它不起作用。结果仍然是肖像
有没有人可以帮助我?
致命错误:未捕获异常'DOMPDF_Exception',消息'找不到块级父级.不好.' 系统/库/ DOMPDF /包括/ inline_positioner.cls.php:37
dompdf ×10
php ×7
pdf ×2
codeigniter ×1
css ×1
dpi ×1
fpdf ×1
laravel ×1
laravel-5.3 ×1
symfony ×1
tcpdf ×1
tinymce ×1
twig ×1
wkhtmltopdf ×1