我嘗試使用jsPdf和html2canvas從html生成pdf。jsPdf從隱藏html生成pdf
但是,當我的股利是不可見的,我得到錯誤:
IndexSizeError: Index or size is negative or greater than the allowed amount and cant create pdf.
如果我的div可見一切OK。
我該如何解決我的問題?如何從隱藏的HTML創建PDF?
我試圖做類似的東西:
$("#btn").click(function() {
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.addHTML($('#testDiv')[0], function() {
pdf.save('PdfFile.pdf');
});
});
#testDiv {
position: absolute;
left: -9999px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="testDiv" style="display: none">
<p>Some text to pdf</p>
</div>
但我得到的PDF與黑色區域,這一切。
加入我的代碼以CSS:
<div id="template_invoice">
<div id="first_head">
<div id="logo_invoice">
<img src="logo.PNG" width="200px">
</div>
<div id="main_header_info">hth</div>
</div>
<div class="clearFix"></div>
</div>
<style>
html{
background: #fff !important;
}
#first_head,#second_head,#content_invoice{
width: 100%;
}
#logo_invoice, #main_header_info,#lead_address,#lead_invoice_info{
width: 50%;
float:left;
}
.clearFix{
float:none;
clear:both;
}
#second_head{
margin-top: 100px;
}
#template_invoice{
margin: 0 auto;
width: 1170px;
}
#template_invoice{
visibility: hidden;
width: 0px;
height: 0px;
}
</style>
<script>
function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter');
source = $('#template_invoice')[0];
specialElementHandlers = {
'#bypassme': function (element, renderer) {
return true
}
};
margins = {
top: 10,
bottom: 10,
left: 10
};
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) {
pdf.save('Test.pdf');
}, margins);
}
</script>
設置'opacity'爲0或'visibility'隱藏,而不是'display'ñ一。 – Swanand
它不適合,因爲在頁面上仍然有空格:http://clip2net.com/s/3Nl398y –