使用 JavaScript 从 HTML 创建 PDF 文档

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14770719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:51:15  来源:igfitidea点击:

Create PDF document from HTML using JavaScript

javascriptpdf

提问by Vishnu

I want to create a PDF document from HTML in JavaScript. I tried using the library jsPDFbut all the HTML formatting are not supported in it. Is there any other library that I can use to create the PDF?

我想用 JavaScript 从 HTML 创建一个 PDF 文档。我尝试使用该库,jsPDF但其中不支持所有 HTML 格式。是否有任何其他库可用于创建 PDF?

回答by chandru

I've just written a library called jsPDF (Give you the link below) which generates PDFs using Javascript alone. It's still very young, and I'll be adding features and bug fixes soon. Also got a few ideas for workarounds in browsers that do not support Data URIs. It's licensed under a liberal MIT license.

我刚刚编写了一个名为 jsPDF 的库(给你下面的链接),它单独使用 Javascript 生成 PDF。它仍然很年轻,我将很快添加功能和错误修复。还对不支持数据 URI 的浏览器中的变通方法有了一些想法。它是在自由的 MIT 许可下获得许可的。

I came across this question before I started writing it and thought I'd come back and let you know :)

我在开始写之前遇到了这个问题,并认为我会回来告诉你:)

http://code.google.com/p/jspdf/

http://code.google.com/p/jspdf/

http://hayageek.com/generate-pdf-with-javascript///Try this link with step by step process

http://hayageek.com/generate-pdf-with-javascript///通过一步一步的过程尝试这个链接

回答by Prateek Chandan

You can create the html page as you like to have PDF then from javascript you can call print function of the browser which eventually exports a webpage to a pdf or in printable form

您可以根据需要创建 html 页面,然后从 javascript 调用浏览器的打印功能,最终将网页导出为 pdf 或可打印形式

回答by Vikram

Consider installing gitbook-pdfgitbook-pdfuses PhantomJSand rasterize.jsto build PDFfiles from HTML

考虑安装gitbook-pdfgitbook-pdf用途PhantomJSrasterize.js从中构建PDF文件HTML

You would require NodeJSand NPM

你需要NodeJS和 NPM

After installing run commands:

安装运行命令后:

gitbook-pdf --format A4 generate index.html index.pdf

gitbook-pdf --format A4 generate index.html index.pdf

A4is the default paper format..there are other options too! check it out

index.htmlis your main HTML file

index.pdfis your final product

A4是默认的纸张格式……还有其他选项!一探究竟

index.html是您的主要 HTML 文件

index.pdf是您的最终产品

Hope that helps!

希望有帮助!