jsPDF 라이브러리를 올바르게 사용하는 방법
내 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는 어디에 있습니까 ?
다음과 같이 html에서 pdf를 사용할 수 있습니다.
1 단계 : 헤더에 다음 스크립트 추가
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
2 단계 : HTML 스크립트를 추가하여 jsPDF 코드 실행
이를 사용자 정의하여 식별자를 전달하거나 #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>
3 단계 : 신체 콘텐츠 추가
<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>
원본 튜토리얼 참조
작동하는 바이올린보기
이 링크 jspdf.min.js 만 필요합니다.
그 안에 모든 것이 있습니다.
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
This is finally what did it for me (and triggers a disposition):
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);
<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>
And for those of the KnockoutJS inclination, a little binding:
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);
}
};
Shouldn't you also be using the jspdf.plugin.from_html.js library? Besides the main library (jspdf.js), you must use other libraries for "special operations" (like jspdf.plugin.addimage.js for using images). Check https://github.com/MrRio/jsPDF.
first, you have to create a handler.
var specialElementHandlers = {
'#editor': function(element, renderer){
return true;
}
};
then write this code in click event:
doc.fromHTML($('body').get(0), 15, 15, {
'width': 170,
'elementHandlers': specialElementHandlers
});
var pdfOutput = doc.output();
console.log(">>>"+pdfOutput );
assuming you've already declared doc variable. And Then you have save this pdf file using File-Plugin.
how about in vuejs how is it applicable?
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);
<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>
참고URL : https://stackoverflow.com/questions/16858954/how-to-properly-use-jspdf-library
'IT TIP' 카테고리의 다른 글
moment.js에서 datetime 형식을 YYYY-MM-DD HH : mm : ss로 지정합니다. (0) | 2020.10.23 |
---|---|
대소 문자를 구분하지 않는 검색 및 sed로 대체 (0) | 2020.10.23 |
목록의 모든 연속 항목 쌍을 반복합니다. (0) | 2020.10.23 |
Ruby에서 두 배열의 조합 (0) | 2020.10.23 |
JIRA JQL 날짜로 검색-Now () (DateTime) 대신 Today () (Date)를 얻는 방법이 있습니까? (0) | 2020.10.23 |