SkillAgentSearch skills...

PDFApp

This is a Google Apps Script library for managing PDFs.

Install / Use

/learn @tanaikech/PDFApp

README

PDFApp

<a name="top"></a> MIT License

<a name="overview"></a>

Overview

This is a Google Apps Script library for managing PDFs.

<a name="description"></a>

Description

Google Apps Script is one of the most powerful tools for cloud computing. When Google Apps Script is used, the result can be obtained even when the user doesn't stay in front of the PC and mobile phone by the triggers. One day, there might be a case where it is required to manage PDF data using Google Apps Script. The combination of Google Docs (Document, Spreadsheet, and Slide) and PDFs is useful for various situations. However, unfortunately, there are no built-in methods for directly managing PDFs using Google Apps Script. Fortunately, it seems that pdf-lib of the Javascript library can be used with Google Apps Script. By this, PDF data can be managed with Google Apps Script using this library. This Google Apps Script library manages PDFs by using it as a wrapper between Google Apps Script and pdf-lib.

I have already published the following posts.

I created this library to more efficiently manage PDFs with a simple script by summarising the scripts of these posts.

Library's project key

1Xmtr5XXEakVql7N6FqwdCNdpdijsJOxgqH173JSB0UOwdb0GJYJbnJLk

<a name="usage"></a>

Usage

1. Install library

In order to use this library, please install the library as follows.

  1. Create a GAS project.

    • You can use this library for the GAS project of both the standalone and container-bound script types.
  2. Install this library.

    • Library's project key is 1Xmtr5XXEakVql7N6FqwdCNdpdijsJOxgqH173JSB0UOwdb0GJYJbnJLk.

Scopes

This library uses the following 3 scopes.

  • https://www.googleapis.com/auth/script.external_request
  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/presentations

Methods

| Methods | Description | | :-------- | :------------------ | | setPDFBlob | Give the source PDF blob. This method is used with other methods. | | useStandardFont | When you want to use the standard font, please use this method. This method is used with other methods. | | useCustomFont | When you want to use the custom font, please use this method. This method is used with other methods. | ||| | exportPages | Export specific pages from a PDF blob. | | getMetadata | Get PDF metadata from a PDF blob. | | udpateMetadata | Update PDF metadata of a PDF blob. | | reorderPages | Reorder pages of a PDF blob. | | mergePDFs | Merge multiple PDF files in a single PDF. | | convertPDFToPng | Convert PDF pages to PNG images. | | getValuesFromPDFForm | Get values from PDF Form. | | setValuesToPDFForm | Set values to PDF Form. | | createPDFFormBySlideTemplate | Create PDF Form By Google Slide template. | | embedObjects | Embed objects into PDF blob. | | insertHeaderFooter | Add custom header and footer to PDF blob. | | splitPDF | Split each page of a PDF to an individual PDF file. | | addPageNumbers | Add page numbers to PDF. |

<a name="setpdfblob"></a>

setPDFBlob

Give the source PDF blob. This method is used with other methods.

In this sample script, the PDF metadata is retrieved.

const blob = DriveApp.getFileById("###fileId of PDF file###").getBlob();

PDFApp.setPDFBlob(blob).getMetadata()
  .then(res => console.log(res))
  .catch(err => console.log(err));

<a name="usestandardfont"></a>

useStandardFont

When you want to use the standard font, please use this method. This method is used with other methods.

In this sample script, a value is put into a field of PDF Form with "TimesRoman".

const blob = DriveApp.getFileById("###fileId of PDF file###").getBlob();
const object = {
  values: [
    { "name": "textbox.sample1.sample1.page1", "value": "sample update text" }
  ],
};

PDFApp
.setPDFBlob(blob)
.useStandardFont("TimesRoman")
.setValuesToPDFForm(object)
  .then(newBlob => DriveApp.createFile(newBlob))
  .catch(err => console.log(err));

<a name="usecustomfont"></a>

useCustomFont

When you want to use the custom font, please use this method. This method is used with other methods.

In this sample script, a value is put into a field of PDF Form with a custom font.

const blob = DriveApp.getFileById("###fileId of PDF file###").getBlob();
const object = {
  values: [
    { "name": "textbox.sample1.sample1.page1", "value": "sample update text" }
  ],
};

PDFApp
.setPDFBlob(blob)
.useCustomFont(DriveApp.getFileById("###fileId of font file###).getBlob())
.setValuesToPDFForm(object)
  .then(newBlob => DriveApp.createFile(newBlob))
  .catch(err => console.log(err));

<a name="exportpages"></a>

exportPages

Export specific pages from a PDF blob.

const blob = DriveApp.getFileById("###fileId of PDF file###").getBlob();
const pageNumbers = [2, 4, 6, 8];

PDFApp.setPDFBlob(blob).exportPages(pageNumbers)
  .then(blob => DriveApp.createFile(blob))
  .catch(err => console.log(err));

<a name="getmetadata"></a>

getMetadata

Get PDF metadata from a PDF blob.

const blob = DriveApp.getFileById("###fileId of PDF file###").getBlob();

PDFApp.setPDFBlob(blob).getMetadata()
  .then(res => console.log(res))
  .catch(err => console.log(err));
  • When this script is run, the metadata is retrieved from the inputted PDF blob.

  • When I tested this script, I noticed that the values of modificationDate and producer might be a bug in pdf-lib. modificationDate returns the execution time. producer always returns pdf-lib (https://github.com/Hopding/pdf-lib). I guessed that this might be a bug in pdf-lib. And, I would like to believe that this will be resolved in the future update.

  • This is from my post "Management of PDF Metadata using Google Apps Script".

<a name="udpatemetadata"></a>

udpateMetadata

Update PDF metadata of a PDF blob.

const blob = DriveApp.getFileById("###fileId of PDF file###").getBlob();

const object = {
  title: ["sample title", { showInWindowTitleBar: true }], // This property is an array.
  subject: "sample subject",
  author: "sample author",
  creator: "sample creator",
  creationDate: new Date("2023-08-01T00:00:00"), // This value is date object.
  modificationDate: new Date("2023-08-01T10:00:00"), // This value is date object.
  keywords: ["sample keyword 1", "sample keyword 2", "sample keyword 3"], // This property is an array.
  producer: "sample producer",
};

PDFApp.setPDFBlob(blob).udpateMetadata(object)
  .then(newBlob => DriveApp.createFile(newBlob))
  .catch(err => console.log(err));
  • When this script is run, the metadata of "title", "subject", "author", "creator", "creationDate", "modificationDate", "keywords", and "producer" is updated. And, after the update is finished, a new PDF file is created in the root folder.

  • This is from my post "[Management of PDF Metadata using Google Apps Script](https://medium.com/google-cloud/manage

Related Skills

View on GitHub
GitHub Stars82
CategoryDevelopment
Updated15d ago
Forks15

Languages

JavaScript

Security Score

100/100

Audited on Mar 19, 2026

No findings