javascript - 如何为 jsPDF 调整画布的大小
问题描述
我在 chrome 中使用 jsPDF 在 react 中工作。我正在制作一个画布,然后尝试将我的网站的 png 图像放在该画布上并将其下载为 pdf。该过程有效,但图像显得拉伸。我尝试手动更改画布的大小并更改放置在画布上的图像的尺寸,但这似乎对最终的 pdf 没有影响。
在谁能提供一些指导之前,有没有人处理过这个烦人的问题?谢谢 :)
创建pdf的代码:
downloadPdf = (quality = 2) => {
const filename = 'ThisIsYourPDFFilename.pdf';
html2canvas(document.querySelector('#nodeToRenderAsPDF'),
{scale: quality}
).then(canvas => {
let pdf = new jsPDF('p', 'px', 'a4');
pdf.height="600"
pdf.width= "800" //this is the canvas resolution
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 450, 500); //this is the image resolution
pdf.save(filename);
});
}
解决方案
Your image dimensions are 450 x 500, but your pdf dimensions are 600 x 800.
450 / 500 = .9 = 90%
and 600 / 800 = .75 = 75%
, so your image will appear stretched as it converts from one aspect ratio to another.
If I were you, I would pick one set of sizes and stick with it. 600 x 800 seems reasonable, so try to change that last line to:
pdf.addImage(canvas.toDataURL('image/png'), 'PNG', 0, 0, 600, 800); //this is the image resolution
and declare your canvas size the same (ie, wherever that canvas html element is declared, set it like <canvas id="myCanvas" width="600" height="800"></canvas>
).
推荐阅读
- django - 使用 ValidationError 和 Constraint(Django 2.2 中的新功能)时的最佳实践是什么?
- voice - 我可以添加智能插头的语音操作,这样我就可以“在 10 分钟内关闭插头”
- c - 为什么 argv[argc] 是指向 NULL 的指针?
- apache-spark - 在 Spark 中读取使用 PartitionBy 或 BucketBy 写入的文件
- csv - 关于具有给定参考/引用数量的论文数量的问题
- python - sns.Facetgrid 未显示正常分布线并忽略空间警告
- linux - 查找过去 24 小时内仅修改过一次的文件
- javascript - 删除 Div Wrapper 或更改嵌套地图中某些分组元素的类名称
- c++ - 用推导类型声明类成员
- javascript - 如何从一个js文件访问一个变量到另一个js文件,两个js文件都在android studio的不同html文件中?