如何创建格式良好的 PDF?
我正在致力于公司发票系统的自动化。目前,所有数据都存储在我们本地的 MySQL 数据库中,有人手动更新 Excel 电子表格,然后将这些数据合并到 MS Word 模板中。我们的目标是自动化此过程,以便可以从我们的内联网网站生成 PDF 格式的发票。
我最初的计划是用 HTML/CSS 创建一个模板并使用 wkhtmltopdf 生成 PDF,但我遇到了在每个页面上获取可重复的页眉和页脚的问题。 Webkit 不支持 thead
和 tfoot
,建议修复 在另一个问题中似乎也不起作用。
所以我后来偶然使用了 XML 和 XSL-FO,后者我一无所知。这是最好的选择吗?是否有任何库或实用程序可以使我的 HTML+CSS 转换为 XML+XSL-FO 更容易?还有其他我忽略的选择吗?
编辑
目前服务器是带有 MySQL 数据库的 CentOS Linux。目前所有其他代码均采用 PHP 语言,但随着整个系统的改造,情况可能会发生变化。不过,Linux 和 MySQL 几乎肯定会保留下来。
I'm working on automating our company invoicing system. Currently all data is stored in our local MySQL database and someone manually updates an excel spreadsheet and then merges this data into a MS Word template. The goal is to automate this process so that the invoice can be generated from our intranet website as a PDF.
My original plan was to create a template in HTML/CSS and use wkhtmltopdf to generate the PDF but I ran into problems with getting a repeatable header and footer on each page. thead
and tfoot
aren't supported by Webkit and the fix suggested in this other question does not seem to work either.
So I then stumbled on using XML and XSL-FO, the latter I know nothing about. Is this the best path to take? Are there any libraries or utilities out there that will make converting my HTML+CSS into XML+XSL-FO easier? Are there any other alternatives I'm overlooking?
EDIT
Currently the server is CentOS Linux with a MySQL database. All other code is currently in PHP currently but that may change as the whole system is being revamped. Linux and MySQL will almost certainly remain, though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于您的要求,XSL-FO 可能可以满足您的要求。直接从数据生成pdf,然后走繁琐的html路径会干净得多,除非你也需要显示html,那么你可能会考虑从html转换为pdf,但它总是会很混乱。
您可以非常轻松地从 mysql 获取 xml 结果 (mysql --xml),然后为数据编写一个(或多个)xsl-fo 样式表。那么,您不仅可以生成 pdf 文件,还可以使用某些处理器生成 postscript 文件或 rtf 文件。
XSL-FO 虽然有其局限性,但对于您的情况来说,它应该足够了。
我承认,学习曲线可能很陡峭,维护 xslt 样式表可能会变得非常累人,但是当您开始了解更多它时,您最终会编写更少的代码。
另一种可能性是在例如java或c#中完成整个事情 - 发送选择语句并循环结果并使用像 这样的库迭代构建pdf iText。
For your requirement, XSL-FO might just do the trick. It is much cleaner to produce the pdf's directly from the data, then going the cumbersome html path, unless you need to display the html as well, then you might consider converting from html to pdf, but it will always be messy.
You can get xml results from mysql quite easily (mysql --xml) and then you write one (or several) xsl-fo stylesheet for the data. then, you cannot only produce pdfs, but also postscript files or rtf's with some processors.
XSL-FO has its limitations tho, but for your situation, it should suffice.
I admit, the learning curve can be steep, and maintaining xslt-stylesheets can get very tiring, but as you start knowing more about it, you end up writing less code.
another possibility is to do the whole thing in e.g. java or c# - send select statements and loop the results and iteratively build the pdf using a library like iText.
您可以尝试 JODReports 或 Docmosis 作为代码密集度较低的选项。您提供 Word 或 OpenOffice Writer 文档作为模板,并使用这些引擎来操作/填充模板,然后以您需要的格式输出文档。这可能意味着您现有的 Word 模板可以直接使用,这应该可以节省您的一些精力/时间。
iText 是另一个库,可让您从代码构建和提取 PDF。非常好。
You could try JODReports or Docmosis as less-code intensive options. You supply Word or OpenOffice Writer documents to act as templates and use these engines to manipulate/populate the templates then spit out the documents in the format(s) you require. This may mean your existing Word-templates can be used directly which should save you some effort/time.
iText is another library that will let you build and pump out PDFs from code. It's pretty good.
如果您云使用 ASP.NET for web,您可以使用免费的 ReportViewer 库和设计器来自动发布 PDF。
以下是一些参考资料:
http://gotreportviewer.com
http://weblogs.asp.net/srkirkland/archive/2007/10/29/exporting-a-sql-server-reporting-services-2005-report-directly-to-pdf-or- excel.aspx
If you cloud use ASP.NET for web you can use free ReportViewer library and designer for automated of publishing PDF-s.
Here is some references:
http://gotreportviewer.com
http://weblogs.asp.net/srkirkland/archive/2007/10/29/exporting-a-sql-server-reporting-services-2005-report-directly-to-pdf-or-excel.aspx
如果您可以使用 .NET 和 C#,则可以使用 Atalasoft 的 DotPdf(强制性免责声明:我在 Atalasoft 工作并编写了大部分 DotPdf)。生成命名空间完全适合您想要执行的操作:自动生成报告。从最基础的开始,您可以直接使用工具包创建文档,也可以创建具有未填充文本字段的模板文档,您可以稍后重新加载和填充这些文本字段(请参阅 此处 和 此处作为示例)。
If you're OK using .NET and C#, you could use DotPdf from Atalasoft (obligatory disclaimer: I work for Atalasoft and wrote most of DotPdf). The Generating namespace is geared for exactly what you're trying to do: automate report generation. From the very basics, you could just create docs directly with the toolkit or you can create template documents that have unpopulated text fields that you can reload and fill later (see here and here for examples).