jspdf 生成pdf文件_使用jsPDF快速从网页生成PDF

本文介绍了如何使用jsPDF库在Web页面上生成PDF文件,强调在某些情况下PDF文件的必要性,如存档和控制打印。jsPDF虽然文档有限,但能提供基本的PDF生成功能。作者通过一个创建传单的示例展示了如何使用jsPDF添加文本、图像并控制布局。尽管存在替代方案,如pdfmake,但jsPDF仍然是一个实用的工具。
摘要由CSDN通过智能技术生成

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).

不幸的是,该库似乎

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值