【问题标题】:How can I get PDF dimensions in pixels in Node.js?如何在 Node.js 中以像素为单位获取 PDF 尺寸?
【发布时间】:2020-07-09 10:51:11
【问题描述】:

我试过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 文件转换为图像的 pdf-poppler 模块中,它需要以像素为单位的 pdf 文件高度。

【问题讨论】:

标签: node.js pdf pdf2json


【解决方案1】:

试试calipers。 代码示例:

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

或者,尝试一个无需宽度/高度即可转换它的模块: pdf2picpdf-imagenode-imagemagick 如果您打算使用 pdf2json,请阅读 this 描述输出单位的文档。

【讨论】:

    【解决方案2】:

    聚会有点晚了,但正如这里所讨论的那样:stackoverflow pdf2json unit 您可以将宽度和高度乘以 24 就像:

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

    然后你就得到了 Pixel。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 2010-11-04
      相关资源
      最近更新 更多