简体   繁体   English

使用PHP将HTML / CSS转换为PDF时出错

[英]Error converting HTML/CSS to PDF using PHP

I build bar codes using jquery in a div in my web app. 我在Web应用程序的div中使用jquery构建条形码。 The black bars are contructed as a set of div tags with their background as black or white as follows, 黑条被构造为一组div标签,其背景如下所示为黑色或白色,

<div style='padding: 0px; overflow: auto; width: 194px;' id='bar1'>
  <div style='float: left; font-size: 0px; background-color: #FFFFFF; height: 40px; width: 60px'>
  <div style='float: left; font-size: 0px; width:0; border-left: 2px solid #000000; height: 40px;'>

I have tried multiple PHP packages (htmp2pdf, dompdf, tcpdf) to convert this to PDF but come up with either a blank PDF or an error as it sees nested div in the content. 我已经尝试了多个PHP软件包(htmp2pdf,dompdf,tcpdf)将其转换为PDF,但出现空白PDF或错误,因为它在内容中看到嵌套的div。 Can anyone tell me how can I convert these generated barcodes to PDF suing PHP? 谁能告诉我如何使用PHP将这些生成的条形码转换为PDF?

Try wkhtmltopdf which is a simple shell utility to convert html to pdf using the webkit rendering engine. 尝试使用wkhtmltopdf ,这是一个简单的Shell实用程序,可使用Webkit呈现引擎将html转换为pdf。

To use it, unpack it and run it like this: 要使用它,请解压缩并运行它,如下所示:

> wkhtmltopdf www.mydomain.com/barcode3 barcode3.pdf > wkhtmltopdf www.mydomain.com/barcode3条码3.pdf

Basically, it will run over n HTML pages and generate PDF's without the overhead of purely dynamic PDF generation! 基本上,它将在n个 HTML页面上运行并生成PDF,而没有纯动态PDF生成的开销!

This might offer you a solution, but you'll need to check the requirements and see if it fits with your server constraints. 这可能为您提供了一个解决方案,但是您需要检查需求并查看它是否适合您的服务器约束。 It worked really well when we used it for producing accurate reports directly from HTML. 当我们使用它直接从HTML生成准确的报告时,它的效果非常好。

EDIT: EG, running it as a potential user function from within PHP: 编辑:EG,从PHP内部将其作为潜在的用户功能运行:

For Linux: 对于Linux:

exec("wkhtmltopdf http://mydomain.com /home/user/$user.barcode3.pdf 2>&1");

For Windows: 对于Windows:

<?php
exec('C://path/to/package//wkhtmltopdf http://mydomain.html barcode3.pdf');

echo "PDF Created Successfully";
?>

Would it help if you used <img> data instead of drawing the bars with <div> rectangles? 如果您使用<img>数据而不是使用<div>矩形绘制条形会有所帮助吗? Not sure if you need Code 128 or another symbology, but there is a complete set of inline (base64) Code 128 images at http://notionovus.com/blog/code-128-barcode/ . 不知道您是否需要Code 128或其他符号,但是在http://notionovus.com/blog/code-128-barcode/上有一整套完整的内联(base64)Code 128图像。

I initially tried to generate barcodes by drawing rectangles, but found that letting the browser do the scaling saved me a lot of work. 最初,我尝试通过绘制矩形来生成条形码,但发现让浏览器进行缩放可以节省很多工作。 Sounds like the <div> tags have their down side as well. 听起来<div>标签也有缺点。

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

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