如何使用npm包将pdf转换为图像

问题描述 投票:0回答:1

上传/1664344016499.pdf 错误:命令失败:magick Convert uploads/1664344016499.pdf -quality 100 output-%3d.jpg /bin/sh: 1: magick: 未找到

at ChildProcess.exithandler (child_process.js:383:12)
at ChildProcess.emit (events.js:400:28)
at maybeClose (internal/child_process.js:1088:16)
at Socket.<anonymous> (internal/child_process.js:446:11)
at Socket.emit (events.js:400:28)
at Pipe.<anonymous> (net.js:686:12) {

杀死:假的, 代码:127, 信号:空, cmd: 'magick 转换 uploads/1664344016499.pdf -quality 100 输出-%3d.jpg'

node.js image pdf file-upload imagemagick
1个回答
0
投票

您看到错误

magick: not found
,因为您必须先安装ImageMagick

这可以在 Ubuntu 上按如下方式完成:

# 1. Update the package list
sudo apt-get update

# 2. Install ImageMagick
sudo apt-get install imagemagick

# 3. Verify the installation
convert --version

之后,您应该能够运行脚本。

一般来说,我建议使用 pdf-img-convert 将 PDF 转换为图像。

示例:

import fs from 'fs';
import pdf2img from 'pdf-img-convert';

const pdfPages = await pdf2img.convert('./document.pdf');

for (let i = 0; i < pdfPages.length; i++) {
  const page = pdfPages[i];
  if (page) {
    // We increment the file name during output to not start with page number 0
    fs.writeFileSync(`./pdf-page-${i + 1}.png`, page);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.