简体   繁体   English

php-使用html表生成pdf并将其保存在文件服务器上

[英]php - generate pdf with html table and save it on file server

I am working with a tool which lets user upload a .csv file. 我正在使用一个工具,该工具可让用户上传.csv文件。 That csv file contains an address column. csv文件包含一个地址列。 I have to use the address from each row in another HTML template. 我必须使用另一个HTML模板中每一行的地址。 That HTML template is like this . 该HTML模板就是这样 After creating that template I then need to convert it into a PDF, store the PDF on a file server and give the user a link to the PDF. 创建该模板之后,我需要将其转换为PDF,将PDF存储在文件服务器上,并为用户提供PDF的链接。

I've finished the first two steps - csv upload and created complete template with address , but I'm stuck on how I can convert a template into a PDF. 我已经完成了前两个步骤csv uploadcreated complete template with address ,但是我仍然坚持如何将模板转换为PDF。

I have looked into a few php-pdf libraries like fpdf mpdf . 我已经看着像一些PHP-PDF库fpdf mpdf I'm facing a problem in creating pdf with html template. 我在使用html模板创建pdf时遇到问题。

A link to a library wich convert HTML to PDF and works pretty well. 到库的链接将HTML转换为PDF,并且效果很好。

First the link to the library 首先到图书馆的链接

HTML2PDF HTML2PDF

Then some code* to create your PDF using your own generated HTML, where $content is your HTML string. 然后使用您自己生成的HTML的一些代码*来创建PDF,其中$content是您的HTML字符串。

$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');

*Code taken from the "example page" of the site. *代码来自网站的“示例页面”。

I have used tcpdf in many cases, https://tcpdf.org/ Works well with tables, I have made receipts and accounting related stuff with it. 我在很多情况下都使用过tcpdf, https: //tcpdf.org/可以很好地与表格配合使用,我已经制作了与收据和会计相关的东西。 Handle UTF-8 without problems, why it's my way to go. 毫无问题地处理UTF-8,这是我要走的路。 Only downside is that code is bit long and complicated and it doesn't keep tables as tables in pdf and turns them to divs, so paddings and other styles might be bit trickier to do. 唯一的缺点是代码太长且太复杂,并且不能将表保留为pdf表,而是将其转换为div,因此填充和其他样式可能会比较棘手。

One way is to use webkit based HTML to PDF converter. 一种方法是使用基于Webkit的HTML到PDF转换器。

Pros are that it is easy to customize and style and to see in the browser how it will look and then you can be sure that it will look as same in PDF as well. 优点是易于自定义和设置样式,并在浏览器中查看外观,然后可以确保它在PDF中的外观也相同。 You could use CSS and JavaScript as well to style and modify. 您也可以使用CSS和JavaScript进行样式和修改。

Cons are that it is hard to install it on the production server sometimes. 缺点是有时很难将其安装在生产服务器上。 But there are web services and APIs that get you covered. 但是有些Web服务和API可以帮助您解决问题。

For example one service is https://pdfapi.io . 例如,一项服务是https://pdfapi.io It is free to use. 它是免费使用。 Only when your amounts get bigger, then it will charge like a cup of coffee. 只有当您的金额增加时,它才会像一杯咖啡一样收费。

Hope that helps. 希望能有所帮助。

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

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