Dan*_*uva 75 html javascript jquery jspdf
我想将我的一些div转换为PDF,我尝试过jsPDF库但没有成功.似乎我无法理解我需要导入什么才能使库工作.我经历过这些例子,但我仍然无法理解.我尝试过以下方法:
<script type="text/javascript" src="js/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
在jQuery之后:
$("#html2pdf").on('click', function(){
var doc = new jsPDF();
doc.fromHTML($('body').get(0), 15, 15, {
'width': 170
});
console.log(doc);
});
Run Code Online (Sandbox Code Playgroud)
出于测试目的但我收到:
"Cannot read property '#smdadminbar' of undefined"
Run Code Online (Sandbox Code Playgroud)
#smdadminbar身体的第一个div 在哪里.
Wel*_*her 120
您可以使用html中的pdf,如下所示,
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
自定义此项以传递标识符或仅将#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>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
use*_*332 16
您只需要此链接jspdf.min.js
它拥有一切.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
根据最新版本(1.5.3),没有fromHTML()方法了。相反,您应该使用 jsPDF HTML 插件,请参阅:https ://rawgit.com/MrRio/jsPDF/master/docs/module-html.html#~html
您还需要添加 html2canvas 库以使其正常工作:https : //github.com/niklasvh/html2canvas
JS(来自 API 文档):
var doc = new jsPDF();
doc.html(document.body, {
callback: function (doc) {
doc.save();
}
});
Run Code Online (Sandbox Code Playgroud)
您也可以提供 HTML 字符串而不是对 DOM 元素的引用。
这最终是为我做了什么(并触发了处理):
function onClick() {
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.canvas.height = 72 * 11;
pdf.canvas.width = 72 * 8.5;
pdf.fromHTML(document.body);
pdf.save('test.pdf');
};
var element = document.getElementById("clickbind");
element.addEventListener("click", onClick);Run Code Online (Sandbox Code Playgroud)
<h1>Dsdas</h1>
<a id="clickbind" href="#">Click</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.3/jspdf.min.js"></script>Run Code Online (Sandbox Code Playgroud)
而对于KnockoutJS倾向的那些,有点约束力:
ko.bindingHandlers.generatePDF = {
init: function(element) {
function onClick() {
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.canvas.height = 72 * 11;
pdf.canvas.width = 72 * 8.5;
pdf.fromHTML(document.body);
pdf.save('test.pdf');
};
element.addEventListener("click", onClick);
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
265301 次 |
| 最近记录: |