我想要将我的一些div转换成PDF,我已经尝试了jsPDF库,但没有成功。看起来我不能理解我需要导入什么才能使这个库工作。我已经看过这些例子了,但我还是搞不懂。我尝试过以下几种方法:
<script type="text/javascript" src="js/jspdf.min.js"></script>
在jQuery和之后:
$("#html2pdf").on('click', function(){
var doc = new jsPDF();
doc.fromHTML($('body').get(0), 15, 15, {
'width': 170
});
console.log(doc);
});
用于测试目的,但我收到:
"Cannot read property '#smdadminbar' of undefined"
其中#smdadminbar
是主体的第一个div。
发布于 2013-10-10 16:39:22
您可以使用html中的pdf,如下所示:
步骤1:将以下脚本添加到头部
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
第2步:添加执行jsPDF代码的HTML脚本
自定义此参数以传递标识符,或仅将#content更改为所需的标识符。
<script>
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');
// source can be HTML-formatted string, or a reference
// to an actual DOM element from which the text will be scraped.
source = $('#content')[0];
// we support special element handlers. Register them with jQuery-style
// ID selector for either ID or node name. ("#iAmID", "div", "span" etc.)
// There is no support for any other type of selectors
// (class, of compound) at this time.
specialElementHandlers = {
// element with id of "bypass" - jQuery style selector
'#bypassme': function (element, renderer) {
// true = "handled elsewhere, bypass text extraction"
return true
}
};
margins = {
top: 80,
bottom: 60,
left: 40,
width: 522
};
// all coords and widths are in jsPDF instance's declared units
// 'inches' in this case
pdf.fromHTML(
source, // HTML string or DOM elem ref.
margins.left, // x coord
margins.top, { // y coord
'width': margins.width, // max width of content on PDF
'elementHandlers': specialElementHandlers
},
function (dispose) {
// dispose: object with X, Y of the last line add to the PDF
// this allow the insertion of new lines after html
pdf.save('Test.pdf');
}, margins
);
}
</script>
第三步:添加你的正文内容
<a href="javascript:demoFromHTML()" class="button">Run Code</a>
<div id="content">
<h1>
We support special element handlers. Register them with jQuery-style.
</h1>
</div>
Refer to the original tutorial
See a working fiddle
发布于 2014-11-07 00:23:46
您只需要此链接jspdf.min.js
里面什么都有。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
发布于 2013-06-28 06:10:12
您不是也应该使用jspdf.plugin.from_html.js库吗?除了主库(jspdf.js)之外,您还必须使用其他库来执行“特殊操作”(比如使用图像的jspdf.plugin.addimage.js )。检查https://github.com/MrRio/jsPDF。
https://stackoverflow.com/questions/16858954
复制相似问题