如何使用jsPDF为生成的PDF每页添加一致的下边距?
使用jsPDF生成简历PDF时无法设置一致下边距的问题
我正在做一个用JavaScript的jsPDF库生成简历模板PDF的项目,想给PDF每页添加一致的下边距,确保内容不触碰页面底部,但试了各种方法都没达到预期效果。我试过设置footer函数、调整位置等方式,但所有页面还是没法实现一致的下边距,内容依旧会碰到页面底部。我已经尝试多种方法,以下是相对合适的代码,但仍无法实现PDF的下边距,核心问题就是PDF的下边距设置。
const handlePrint = () => { const pdf = new jsPDF({ orientation: "portrait", unit: "mm", format: [297, 210], // A4 page size in mm }); const content = types?.doctype === "resume" ? Resumeref.current : Coverref.current; const fontSize = 12; console.log("Page height:", pdf.internal.pageSize.getHeight()); // Calculate the zoom level to fit content within the page const pageWidth = pdf.internal.pageSize.getWidth() - 17; // Adjust for left and right margins const pageHeight = pdf.internal.pageSize.getHeight() + 10; console.log("Page height:", pdf.internal.pageSize.getHeight()); const contentWidth = content.clientWidth; const scale = pageWidth / contentWidth; const bottomMargin = 10; // Setting bottom margin to 10mm // Set the font size and scale the content pdf.setFontSize(fontSize); pdf.html(content, { x: 7, y: 7, html2canvas: { scale }, callback: function (pdf) { pdf.save("document.pdf"); }, }); };
问题效果截图:
内容的提问来源于stack exchange,提问作者Viosa
相关产品推荐
相关产品推荐

