node-pdf-image icon indicating copy to clipboard operation
node-pdf-image copied to clipboard

Failed to convert page to image

Open VassilisPallas opened this issue 5 years ago • 6 comments

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 avatar Jan 09 '20 15:01 VassilisPallas

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',
      },
    });

VassilisPallas avatar Jan 09 '20 15:01 VassilisPallas

I am also having this issue. Any luck?

jamiegalbreath avatar Apr 02 '20 11:04 jamiegalbreath

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.

toddself avatar Apr 03 '20 20:04 toddself

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.

VassilisPallas avatar Apr 04 '20 20:04 VassilisPallas

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.

toddself avatar Apr 06 '20 19:04 toddself

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

VolckaertAchiel avatar Apr 15 '20 13:04 VolckaertAchiel