Pdftohtml Poppler utils 无法在 centOs 上运行

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

我正在尝试使用 mgufrone 库在 php 中将 pdf 转换为 html (https://github.com/mgufrone/pdf-to-html)。我在我的 mac 上运行它,工作正常。 但是当我在 centos 服务器中运行时,.html 文件在 /vendor/gufy/pdftohtml-pdf/output 文件夹中创建为空白。 在我的 Mac 中,本地多个文件是在 /output 文件夹中创建的。但在服务器中仅创建一个包含空内容的文件。 请帮忙。

php centos6 poppler pdf-to-html
2个回答
4
投票

问题就在这里:

sudo yum install poppler-utils

安装旧版本(0.12.4),它没有 pdftohtml 命令选项,如“-s”和“-fmt”。

转到此页面 https://medium.com/@jakebathman/building-poppler-utils-for-centos-6-5-really-e52eccffc6ae 指导您如何获取更高版本的 poppler-utils。我按照指示安装了 https://poppler.freedesktop.org/poppler-0.22.5.tar.gz 而不是 0.13.4。

祝一切顺利!


0
投票

in centOs you can use below code

async function pdfToImageConvert(pdfData, req, res) {
  const projectDirectory = process.cwd();
  const scriptDirectory = projectDirectory;

  function fileToBase64(fileObject) {
    return new Promise((resolve, reject) => {
      const base64Data = fileObject.data.toString("base64");
      resolve(base64Data);
    });
  }

  fileToBase64(pdfData)
    .then(async (base64Data) => {
      const pdfPath = path.join(scriptDirectory, 'input.pdf');
      fs.writeFileSync(pdfPath, Buffer.from(base64Data, 'base64'));

      const outputDirectory = path.join(scriptDirectory, 'public', 'upload');
      console.log("outputDirectory", outputDirectory);

      convertPdfToImage(pdfPath, outputDirectory);

      async function convertPdfToImage(pdfPath, outputDir) {
        const command = `pdftoppm -jpeg ${pdfPath} ${path.join(outputDir, 'image')}`;

        try {
          await executeCommand(command);

          // List the files in the output directory
          fs.readdir(outputDir, (err, files) => {
            if (err) {
              console.log('Error listing files:', err);
              return;
            }

            const pdfImages = files.filter(file => file.startsWith('image'));

            const baseURL = 'http://localhost:3001/upload/'; // Change this to your actual base URL

            const modifiedPDFImages = pdfImages.map(filename => baseURL + filename);

            console.log('Images created from the PDF:', modifiedPDFImages);
            console.log('Number of images created:', modifiedPDFImages.length);

            fs.unlink(pdfPath, err => {
              if (err) {
                console.log('Error deleting input.pdf:', err);
              } else {
                console.log('input.pdf deleted.');
              }
            });

            let resultSuccess = encryptData(
              JSON.stringify({ images: modifiedPDFImages })
            );
            return res.json({ result: resultSuccess });
          });
        } catch (error) {
 console.log(error)
        }
      }
    })
    .catch((error) => {
 console.log(error)
    });
}

function executeCommand(command) {
  return new Promise((resolve, reject) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
       console.log(error)
      } else {
       console.log("success")
      }
    });
  });
}

© www.soinside.com 2019 - 2024. All rights reserved.