jspdf 生成pdf文件
The Portable Document Format has been one the major innovations in the fields of desktop publishing and office automations.
可移植文档格式已成为桌面出版和办公室自动化领域的主要创新之一。
It’s widely used in web publishing too, but unfortunately very often in wrong ways – like using it to replace contents that should have been built with HTML. This causes many problems regarding usability, accessibility, SEO and so on.
它也广泛用于Web发布中,但不幸的是,它经常以错误的方式出现-例如使用它来替换本应由HTML构建的内容。 这会导致许多与可用性,可访问性,SEO等有关的问题。
However, there are some cases in which PDF files are required: when a document needs to be archived and must be used outside the web (for example an invoice) or when you need a deep control on printing.
然而,也有一些案件中,PDF文件是必需的:当一个文件需要被存档,并且必须在网络(例如发票),或者当你需要打印上了深刻的控制之外使用。
It was just the need to control printing that brought me to research a way to easily generate a PDF.
正是由于需要控制打印,才使我研究了一种轻松生成PDF的方法。
The purpose of this article is not to simply explain how a PDF can be created (there are many easy way to do this), but also to focus on the circumstances where a PDF file can solve a problem, and how a simple tool like jsPDF can help with this.
本文的目的不是简单地解释如何创建PDF(有许多简单的方法可以做到这一点),而是着眼于PDF文件可以解决问题的情况以及像jsPDF这样的简单工具的方式。可以帮助您。
处理印刷 (Dealing with Printing)
Anyone who has dealt with CSS printing rules knows how difficult it is to achieve a decent level of cross-browser compatibility (take a look, for example, at the Page-break support table at Can I Use). Therefore, when I need to build something that must be printed, I always try to avoid CSS, and the simplest solution is to use PDF.
任何处理过CSS打印规则的人都知道实现良好的跨浏览器兼容性水平是多么困难(例如,看一下“我可以使用”的分页支持表 )。 因此,当我需要构建必须打印的内容时,我总是尽量避免使用CSS,最简单的解决方案是使用PDF。
I’m not talking here about the simple conversion of HTML to PDF. (I’ve tried several tools of that type, but none of them has fully satisfied me.) My goal is to have complete control over the positioning and size of elements, page breaks and so on.
我不是在这里谈论HTML到PDF的简单转换。 (我尝试过几种此类工具,但没有一个使我完全满意。)我的目标是完全控制元素的位置和大小,分页符等。
In the past I’ve often used FPDF, a PHP tool that can easily give you such controls and that can be easily expanded with many plugins.
过去我经常使用FPDF (PHP工具),它可以很容易地给你这样的控件,并且可以通过许多插件轻松地扩展。
Unfortunately, the library seems to be abandoned (its last version dates back to 2011) (Update: actually, the latest version appears to be from December 2015), but thanks to some JavaScript libraries, we now have the ability to build PDF files directly in the clients (thus making their generation faster).
不幸的是,该库似乎