2

I have a cloud function that runs in the Google/Firebase cloud environment that listens for a file to be added to a storage bucket, checks to see if it's of PDF format before attempting to convert all the pages to individual PNG files for uploading back into the bucket for use across the app.

In order to achieve this I am making use of the ImageMagick library already pre-installed in the environment as mentioned here.

I've imported the libary like so:

import * as gm from "gm";
const im = gm.subClass({ imageMagick: true });

I then download the pdf file from the storage bucket to the local directory:

const tempFilePath = path.join(os.tmpdir(), `${fileName}.pdf`);

const bucket = admin.storage().bucket();

return bucket.file(filePath).download({
    destination: tempFilePath
}).then(async () => {
... code to continue in a moment ...

With the PDF file downloaded locally, I then attempt to covert the first page of the file into a PNG file via the use of the ImageMagick library:

const newName = path.basename(filePath, ".pdf") + "_PAGE_0.png";
        
const tempNewPath = path.join(os.tmpdir(), newName);

im(`${tempFilePath}[0]`)
  .setFormat("png")
  .write(tempNewPath, (error) => {
    if (!error) {
      console.log("Finished saving PNG");
      return bucket.upload(tempNewPath, { destination: storagePath });
    } else {
      console.log(error);
      return false;
    }
  });

When the function runs, I get the following error printed into the logs:

Command failed: convert-im6.q16: unable to open image `/tmp/r4dTjOTUz6b92pm8arnu.pdf': No such file or directory    
convert-im6.q16: not authorized `/tmp/r4dTjOTUz6b92pm8arnu.pdf'

From looking about online, there are a couple of other posts I have come across with similar problems:

Any help at this point would be great. The main aim of this function like mentioned at the top of the question is to take a multi-page PDF, convert each page to a PNG image and then store those in the same bucket alongside the original PDF file.

lewisnewson
  • 410
  • 6
  • 22
  • Does this answer your question? [Convert PDF to PNG on Google Cloud Storage](https://stackoverflow.com/questions/54417900/convert-pdf-to-png-on-google-cloud-storage) – Alex Nov 10 '21 at 23:24
  • Hi, did you find a solution to this? I'm also completely stuck here. – digibake Aug 15 '22 at 15:34

1 Answers1

0

I encountered a similar issue (it took me hours to find a solution). If anyone has this problem in 2023, here is how to resolve it.

Note: the OP's issue is converting PDFs to jpeg.

Convert the PDF buffer or file to a PNG using pdf-to-png-converter. The library uses a server-side nodejs canvas library to convert PDFs to PNG without any OS dependencies.

Once you have your file in PNG you can then use imagemaigck to convert it into JPEG or perform other image processing.

Example using a PDF Buffer

import { pdfToPng, PngPageOutput } from 'pdf-to-png-converter'

// rest of your existing code
// ....

const pngPages: PngPageOutput[] = await pdfToPng(pdfBuffer)
const pngBuffer = Buffer.concat(pngPages.map((page) => page.content))
// you can write the buffer to a file or just pass it to imagemagick for 
// subsequent conversion

fs.writeFileSync('hello.png', pngBuffer)
theterminalguy
  • 1,842
  • 18
  • 20