Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

javascript - 使用javascript将HTML页面中的div下载为pdf(Download a div in a HTML page as pdf using javascript)

I have a content div with the id as "content".(我有一个id为“content”的内容div。)

In the content div I have some graphs and some tables.(在内容div中我有一些图表和一些表格。) I want to download that div as a pdf when user click on download button.(我想在用户点击下载按钮时将该div下载为pdf。) Is there a way to do that using javascript or jQuery?(有没有办法使用javascript或jQuery?)   ask by Midhun Mathew translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do it using jsPDF(你可以用jsPDF来做)

HTML:(HTML:) <div id="content"> <h3>Hello, this is a H3 tag</h3> <p>A paragraph</p> </div> <div id="editor"></div> <button id="cmd">generate PDF</button> JavaScript:(JavaScript的:) var doc = new jsPDF(); var specialElementHandlers = { '#editor': function (element, renderer) { return true; } }; $('#cmd').click(function () { doc.fromHTML($('#content').html(), 15, 15, { 'width': 170, 'elementHandlers': specialElementHandlers }); doc.save('sample-file.pdf'); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...