[英]html2canvas, jsPDF - how scale/size the page for a "legal letter" PDF page?
I'm creating a PDF by using the entire document.body
, turning it into a canvas and passing that to jsPDF
.我正在使用整个
document.body
创建 PDF ,将其转换为 canvas 并将其传递给jsPDF
。 But the image/canvas is too wide.但是图像/画布太宽了。 I want to scale it for the page, but
jsPDF
doesn't have pixel size as a measurement.我想为页面缩放它,但
jsPDF
没有像素大小作为衡量标准。
The options are: pt
, mm
, cm
.选项有:
pt
、 mm
、 cm
。 How do I properly size for that?我该如何正确调整尺寸? And how do I scale my image if needed?
如果需要,我如何缩放我的图像?
Should I be using the addImage
function to scale, or scale by using canvas.getContect( "2d" ) and drawing on to a new canvas?我应该使用
addImage
function 进行缩放,还是使用 canvas.getContect("2d") 并绘制到新的 canvas 进行缩放?
html2canvas(
document.body,
{
//When the canvas is created, our callback
onrendered: function(canvas)
{
//Create jsPDF with what measurements?
var doc = new jsPDF('p', 'pt');
/*
* Put image on page. Are these measurements
* in pts? How does that compare to pixels?
*
* Is the x/y (the 10, 10) the x and y in the image?
* Or in the page where the image is printed?
*
* Should I be using this to scale, or scale by
* using canvas.getContect( "2d" ) and drawing on
* to a new canvas?
*/
doc.addImage(canvas, 'PNG', 10, 10, 1024, 1000);
//Save
doc.save('sample-file.pdf');
}
});
This needs a bit more tweaking but is the best solution arrived at thus far.这需要更多的调整,但这是迄今为止最好的解决方案。 Perhaps more can contribute to get it perfect.
也许更多可以帮助使它变得完美。 This solution is grabbing a
form
element of a ReactJS application.此解决方案正在获取 ReactJS 应用程序的
form
元素。 It is creating a PDF of a large form.它正在创建一个大型的 PDF。 The html2canvas
windowWidth
is needed because the similar jsPDF setting states it does not affect media queries.需要 html2canvas
windowWidth
,因为类似的 jsPDF 设置声明它不会影响媒体查询。
toPdf = (callback) => {
const capture = document.querySelector('form')
const pdf = jsPDF({orientation: 'p', format: 'letter'})
pdf.html(capture, {
callback: function (doc) {
if (!callback) {
return
}
callback(doc)
},
x: 0,
y: 0,
margin: [4, 4, 4, 4], // mm
width: 208, // 216 = letter paper width in mm, 208 = less the 8mm margin
windowWidth: 786, // 816 = letter paper pixel width at 96dpi (web), 786 = less the 30px margin
html2canvas: {
logging: false,
windowWidth: 786 // 816 = letter paper pixel width at 96dpi (web), 786 = less the 30px margin
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.