gpt4 book ai didi

javascript - 如何检测jsPDF的第一页

转载 作者:行者123 更新时间:2023-11-29 22:03:57 25 4
gpt4 key购买 nike

如何在 jsPDF 的第一页上检测/调整条件??

我想检测第一页,这样我就可以只在第一页而不是下一页应用上边距。这样我就可以使用 .text() 方法静态添加我的标题。

这是我当前的代码:

<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<title></title>

{{ HTML::script("js/jspdf/js/jquery/jquery-1.7.1.min.js")}}
{{ HTML::script("js/jspdf/js/jquery/jquery-ui-1.8.17.custom.min.js")}}
{{ HTML::script("js/jspdf/js/libs/polyfill.js")}}
{{ HTML::script("js/jspdf/jspdf.js")}}
{{ HTML::script("js/jspdf/js/libs/deflate.js")}}
{{ HTML::script("js/jspdf/js/libs/adler32cs.js/adler32cs.js")}}
{{ HTML::script("js/jspdf/js/libs/FileSaver.js/FileSaver.js")}}
{{ HTML::script("js/jspdf/js/libs/Blob.js/Blob.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.standard_fonts_metrics.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.split_text_to_size.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.addimage.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.cell.js")}}
{{ HTML::script("js/jspdf/jspdf.plugin.from_html.js")}}


</head>
<body>



<div style="border-width: 2px; border-style: dotted; padding: 1em; font-size:120%;line-height: 1.5em;" id="fromHTMLtestdiv">
<div>
<br>
<br>
<br>
</div>

<table>
<colgroup>
<col width="10%">
<col width="30%">
<col width="40%">
<col width="10%">
<col width="10%">
</colgroup>
<thead>
<tr>
<th>id</th>
<th>item name</th>
<th>description</th>
<th>price</th>
<th>qty</th>
<th>subtotal</th>
</tr>
</thead>
<tbody>

@for ($x=0; $x < 20; $x++)


@foreach ($items as $item => $value)
<tr>

<td>{{$value->itemId}}</td>
<td>{{$value->itemName}}</td>
<td>{{$value->description}}</td>
<td>{{$value->itemPrice}}</td>
<td>{{$value->quantity}}</td>
<td>{{$value->quantity*$value->itemPrice}}</td>

</tr>
@endforeach

@endfor
</tbody>

</table>



</div>
<div>

<button onclick="javascript:demoFromHTML()" class="button">Run Code</button></p></div></div>

</div>
</div>


<script>
$(document).ready(function() {

demoFromHTML();
});




function demoFromHTML() {
var pdf = new jsPDF('p', 'pt', 'letter')

pdf.setFontSize(16)
pdf.text(40, 60, 'Doe, John Smith')
pdf.setFontSize(12)
pdf.text(40, 72, '795 Folsom Ave, Suite 600')
pdf.text(40, 84, 'San Francisco, CA 94107')
pdf.text(40, 96, '(123) 456-7890')
, source = $('#fromHTMLtestdiv')[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>

</body>
<footer>
</footer>
</html>

上面的代码生成了这个 pdf:enter image description here

我尝试增加 margin-top 的值,但它会影响所有页面:/

我尝试在顶部添加 <br> ,但它也不起作用。任何帮助将不胜感激!感谢您的时间。如果不清楚或您需要更多信息,请在下面评论。!祝你有美好的一天!

最佳答案

问题可能太旧而无法回答,但您可以通过如下所示的内部方法检测页码:

let doc = new Pdf();
let pageInfo = doc.internal.getCurrentPageInfo();
console.log(pageInfo.pageNumber) // Logs your current page number.

关于javascript - 如何检测jsPDF的第一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22017394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com