mooz / node-pdf-image

Provides an interface to convert PDF's pages to png files in Node.js by using ImageMagick
MIT License
237 stars 87 forks source link

Failed to convert page to image #55

Open VassilisPallas opened 4 years ago

VassilisPallas commented 4 years ago

Environment: Node: 10 Os: ubuntu:18.04 (docker)

const path = './tmp';

const saveTempFile = (file, buffer) => { ... }

const readTempFile = file => { ... }

const deleteTempFile = file => { ... }

module.exports = async ({ originalname, buffer }) => {
  try {
    const extension = getExtension(originalname);
    const file = `${path}/${randonString()}${extension}`;
    await saveTempFile(file, buffer);

    const pdfImage = new PDFImage(file);
    const image = await pdfImage.convertPage(0);

    await deleteTempFile(file);
    return readTempFile(image);
  } catch (e) {
    throw e;
  }
};
{ message: 'Failed to convert page to image',
  error:
   { Error: Command failed: convert "./tmp/o22s9w2cqmjhlf0jawr1iq.pdf[0]" "tmp/o22s9w2cqmjhlf0jawr1iq-0.png"
   convert-im6.q16: not authorized `./tmp/o22s9w2cqmjhlf0jawr1iq.pdf' @ error/constitute.c/ReadImage/412.
   convert-im6.q16: no images defined `tmp/o22s9w2cqmjhlf0jawr1iq-0.png' @ error/convert.c/ConvertImageCommand/3258.

       at ChildProcess.exithandler (child_process.js:294:12)
       at ChildProcess.emit (events.js:198:13)
       at maybeClose (internal/child_process.js:982:16)
       at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
     killed: false,
     code: 1,
     signal: null,
     cmd:
      'convert "./tmp/o22s9w2cqmjhlf0jawr1iq.pdf[0]" "tmp/o22s9w2cqmjhlf0jawr1iq-0.png"' },
  stdout: '',
  stderr:
   'convert-im6.q16: not authorized `./tmp/o22s9w2cqmjhlf0jawr1iq.pdf\' @ error/constitute.c/ReadImage/412.\nconvert-im6.q16: no images defined `tmp/o22s9w2cqmjhlf0jawr1iq-0.png\' @ error/convert.c/ConvertImageCommand/3258.\n' }
VassilisPallas commented 4 years ago

I removed the restriction with the command

sudo sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/g' /etc/ImageMagick-6/policy.xml

but know the image is blank [43]. Also the convert doesn't work if the density is not given.

    const pdfImage = new PDFImage(file, {
      combinedImage: true,
      convertOptions: {
        '-resize': '200x200',
        '-quality': '75',
        '-density': '800',
      },
    });
jamiegalbreath commented 4 years ago

I am also having this issue. Any luck?

toddself commented 4 years ago

This isn't an issue with this tool, rather an issue with the environment where it's being run.

As far as the image being blank, i would need a replicable test case to investigate.

VassilisPallas commented 4 years ago

It runs inside a docker image which is using ubuntu as base image. The pdf file is just a file inside /tmp. I have already provided to you the code I'm using. In saveTemp you can use fs to store the file.

toddself commented 4 years ago

Sorry, the code above does not run, nor is there a Dockerfile to build it. If you'd like help with this, I'd be happy to help, but I need a fully replicable test case first.

VolckaertAchiel commented 4 years ago
sudo sed -i 's/rights="none" pattern="PDF"/rights="read|write" pattern="PDF"/g' /etc/ImageMagick-6/policy.xml

This fixed my problem, thanks alot