如何在Node.js中获得以像素为单位的PDF尺寸



我尝试了pdf2json:

const PDFParser = require("pdf2json");
let pdfParser = new PDFParser();
pdfParser.loadPDF("./30x40.pdf"); // ex: ./abc.pdf
pdfParser.on("pdfParser_dataReady", pdfData => {
width = pdfData.formImage.Width; // pdf width
height = pdfData.formImage.Pages[0].Height; // page height
console.log(`Height : ${height}`) // logs 70.866
console.log(`Width : ${width}`) // logs 53.15
});

但它给出了未知单位的尺寸!

以像素为单位的尺寸将帮助我将它们包含在pdf poppler模块中,该模块将pdf文件转换为图像,并且需要以像素为单元的pdf文件高度。

试试卡尺。代码示例:

const Calipers = require('calipers')('png', 'pdf');
Calipers.measure('./30x40.pdf')
.then(data => {
const { width, height } = data.pages[0];
});

或者,尝试一个无需宽度/高度即可转换的模块:pdf2pic pdf图像节点imagemagik如果您打算使用pdf2json,请阅读这段描述输出单位的文档。

派对有点晚,但正如这里所讨论的:stackoverflow pdf2json单元您可以将宽度和高度乘以24就像:

width = pdfData.formImage.Width * 24; // pdf width
height = pdfData.formImage.Pages[0].Height * 24; // page height

然后你得到Pixel。

相关内容

最新更新