Skip to content

Instantly share code, notes, and snippets.

@tanaikech
Last active January 11, 2023 02:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanaikech/94ff0713a7bfe2d3e43afbfe54611190 to your computer and use it in GitHub Desktop.
Save tanaikech/94ff0713a7bfe2d3e43afbfe54611190 to your computer and use it in GitHub Desktop.
Converting All Pages in PDF File to PNG Images using Google Apps Script

Converting All Pages in PDF File to PNG Images using Google Apps Script

This is a sample script for converting all pages in a PDF file to PNG images using Google Apps Script.

I have already published "Merging Multiple PDF Files as a Single PDF File using Google Apps Script". In this post, it was found that pdf-lib can be used with Google Apps Script. From this, in this post, I would like to propose a sample script for converting all pages in a PDF file to PNG images using Google Apps Script. This cannot be directly achieved with Google Apps Script. So, I thought that this might be useful for users.

Sample script

In order to test this script, please prepare a sample PDF file on your Google Drive. Or, please prepare a direct link to a sample PDF file. And please set the folder ID to const folderId = "###";. The converted PNG files are put in this folder.

This method uses Drive API. Please enable Drive API at Advanced Google services.

/**
 * This is a method for converting all pages in a PDF file to PNG images.
 * PNG images are returned as BlobSource[].
 * IMPORTANT: This method uses Drive API. Please enable Drive API at Advanced Google services.
 *
 * @param {Blob} blob Blob of PDF file.
 * @return {BlobSource[]} PNG blobs.
 */
async function convertPDFToPNG_(blob) {
  // Convert PDF to PNG images.
  const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
  eval(UrlFetchApp.fetch(cdnjs).getContentText()); // Load pdf-lib
  const setTimeout = function (f, t) {
    // Overwrite setTimeout with Google Apps Script.
    Utilities.sleep(t);
    return f();
  };
  const data = new Uint8Array(blob.getBytes());
  const pdfData = await PDFLib.PDFDocument.load(data);
  const pageLength = pdfData.getPageCount();
  console.log(`Total pages: ${pageLength}`);
  const obj = { imageBlobs: [], fileIds: [] };
  for (let i = 0; i < pageLength; i++) {
    console.log(`Processing page: ${i + 1}`);
    const pdfDoc = await PDFLib.PDFDocument.create();
    const [page] = await pdfDoc.copyPages(pdfData, [i]);
    pdfDoc.addPage(page);
    const bytes = await pdfDoc.save();
    const blob = Utilities.newBlob(
      [...new Int8Array(bytes)],
      MimeType.PDF,
      `sample${i + 1}.pdf`
    );
    const id = DriveApp.createFile(blob).getId();
    Utilities.sleep(3000); // This is used for preparing the thumbnail of the created file.
    const link = Drive.Files.get(id, { fields: "thumbnailLink" }).thumbnailLink;
    if (!link) {
      throw new Error(
        "In this case, please increase the value of 3000 in Utilities.sleep(3000), and test it again."
      );
    }
    const imageBlob = UrlFetchApp.fetch(link.replace(/\=s\d*/, "=s1000"))
      .getBlob()
      .setName(`page${i + 1}.png`);
    obj.imageBlobs.push(imageBlob);
    obj.fileIds.push(id);
  }
  obj.fileIds.forEach((id) => DriveApp.getFileById(id).setTrashed(true));
  return obj.imageBlobs;
}

// Please run this function.
async function main() {
  // Retrieve PDF data.
  const fileId = "###"; // Please set file ID of PDF file on Google Drive.
  const blob = DriveApp.getFileById(fileId).getBlob();

  // const url = "###url1###"; // Please set the direct link of the PDF file.
  // const blob = UrlFetchApp.fetch(url).getBlob();

  // Use a method for converting all pages in a PDF file to PNG images.
  const imageBlobs = await convertPDFToPNG_(blob);

  // As a sample, create PNG images as PNG files.
  const folderId = "###"; // Please set your folder ID.
  const folder = DriveApp.getFolderById(folderId || "root");
  imageBlobs.forEach((b) => folder.createFile(b));

  // As another sample, create a zip file including the converted PNG images.
  // const zip = Utilities.zip(imageBlobs, "sample.zip");
  // DriveApp.createFile(zip);
}
  • Please set the file ID of PDF file or the direct link of PDF file. When you use the URL, please modify as follows.

    • From

      const fileId = "###"; // Please set file ID of PDF file on Google Drive.
      const blob = DriveApp.getFileById(fileId).getBlob();
    • To

      const url = "###url1###"; // Please set the direct link of the PDF file.
      const blob = UrlFetchApp.fetch(url).getBlob();
  • When main is run, all pages of the PDF file are converted to PNG images, and those images are put to the destination folder.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment