0
由於jspdf不支持utf8字符,我試圖用圖像替換一些字符,然後以pdf格式導出。爲了幫助您理解,我製作了代碼示例,在此嘗試將圖像放入html div中,然後使用jspdf將其粘貼到pdf中。文本以pdf格式顯示,但沒有圖像,那麼問題是什麼?我是初學者,所以請向我解釋如何做到這一點。如何使用jsPDF庫將圖像從HTML插入到PDF中?
嘗試此鏈接,看到了問題:http://www.balkanex.info/test/start.html
和代碼低於:
的start.html
<!doctype>
<html>
<head>
<meta charset="utf-8">
<script type="text/javascript" src="runner.js" charset="utf-8"></script>
<script type="text/javascript" src="script.js" charset="utf-8"></script>
<script type="text/javascript" src="jspdf/jquery/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="jspdf/jspdf.js"></script>
<script type="text/javascript" src="jspdf/libs/Deflate/adler32cs.js"></script>
<script type="text/javascript" src="jspdf/libs/FileSaver.js/FileSaver.js"></script>
<script type="text/javascript" src="jspdf/libs/Blob.js/BlobBuilder.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.addimage.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.standard_fonts_metrics.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.addimage.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.split_text_to_size.js"></script>
<script type="text/javascript" src="jspdf/jspdf.plugin.from_html.js"></script>
</head>
<body>
<input type="button" id="btn" value="PROCEED TO THE NEXT PAGE" onClick="run();pdf();">
</body>
</html>
runner.js
function run() {
document.write('<div id="myid">This is the image: <img src="button.png" alt="Submit"> </div><br/>');
document.write('<button id="pdf">Export in pdf</button></div>');
}
的script.js
function pdf() {
$(function() {
var doc = new jsPDF();
var specialElementHandlers = {
'body': function (element, renderer) {
return true;
}};
$('#pdf').click(function() {
doc.fromHTML($('#myid').html(), 15, 35, {
'width': 170,
'elementHandlers': specialElementHandlers
});
doc.save('sample.pdf');
});
});
}
請注意Mozilla的** pdf.js ** _reads_ PDF,而** jsPDF ** _writes_。 –