node.js - 对图像使用旋转会导致带有 pdfkit 的空白 pdf
问题描述
我已经直接在存储库上提出了这个问题,但根据我的经验,SO 更具反应性。
嘿,
我正在尝试使用pdfkit从照片创建 pdf 。根据图像是横向还是纵向模式,我想翻转图像。
这基本上意味着以下(在打字稿中):
function toPostscriptPoint(mm: number) {
return mm * 2.8346456693;
}
const document = new PDFDocument({
size: [toPostscriptPoint(156), toPostscriptPoint(106)],
});
document.pipe(fs.createWriteStream('output.pdf'));
document.save();
document.rotate(90);
document.image(
'photos/sample.jpeg',
{ width: toPostscriptPoint(150), fit: [toPostscriptPoint(150), toPostscriptPoint(100)] });
document.restore();
document.end();
但是会发生什么是 pdf 呈现完全白色。但是,我确实看到正在发生一些事情,因为 pdf 具有输入图像的大小。
不支持图像旋转吗?可能的替代方案是什么?我想避免在将文件放入 pdf 之前重写我的文件。
谢谢
解决方案
好吧,经过调查,我可以回答我自己的问题:)。
由于文件的大小,我可以看到图像在 pdf 中,所以我更深入地研究。
发生的事情是图像被渲染出视口。这是由于多方面的原因: * 默认情况下,pdfkit 中旋转后的页面原点是页面的中心!(有关更多信息,请参阅文档) * 原点与变换一起旋转。* image 方法中的x和y实际上是倒置的。
因此,在完成所有这些之后,以下代码将按预期显示图像:
function toPostscriptPoint(mm: number) {
return mm * 2.8346456693;
}
const document = new PDFDocument({
size: [toPostscriptPoint(156), toPostscriptPoint(106)],
});
document.pipe(fs.createWriteStream('output.pdf'));
document.save();
document.rotate(90, {origin : [0, 0]});
document.image(
'photos/sample.jpeg',
toPostscriptPoint(0),
toPostscriptPoint(-150),
{ width: toPostscriptPoint(150), height: toPostscriptPoint(100) });
document.restore();
document.end();
注意:
- 旋转中的原点参数
- toPostscriptPoint(-150) 实际上考虑了原点的位置,对应X轴。
希望对以后有所帮助:)。
推荐阅读
- reactjs - 提供者值上的 Typescript React UseReducer 类型错误
- python - 向 Django 频道消费者发送数据
- javascript - 编辑功能不保存对 React 中状态数据的更改
- python - Pandas 基于另一列的条件移位
- javascript - 正确的工作流程 React.js
- php - 警告:DOMDocument::loadXML():需要开始标记,在实体中找不到“<”
- c# - 如何在 IdentityServer4 中实现自己的 ClientStore?
- reactjs - 如何将组件放在一行/块中?
- angular - ~/node_modules/ng-http-loader/ng-http-loader.d.ts 中的指令AbstractLoaderDirective 中的错误没有选择器,请添加
- ruby-on-rails - 没有为 RSpec 找到具有“主要”的连接池