Gists
Abstract
This study proposes a workaround to address the Gemini API’s current inability to directly process web content from URLs. By utilizing Google Apps Script, the method extracts relevant information from a specified URL and feeds it into the API for summarization. This approach offers a solution for generating comprehensive summaries from web-based content until the API’s limitations are resolved.
Introduction
While Gemini API offers powerful text generation capabilities, it currently faces limitations in directly accessing and processing web content from URLs. When prompted to summarize an article at a specific URL like Summarize the article at the following URL. https://###, the API often returns an error message indicating its inability to retrieve the necessary information. This limitation arises from the API’s current design, which may not be equipped to handle web requests and parse HTML content.
PDFApp was updated to v1.0.7.
-
v1.0.7 (May 15, 2024)
- The method of “addPageNumbers” was updated. Ref When a number is used to the property
x instead of “left”, “center”, and “right”, the inputted number is directly used.
You can see the detail information here https://github.com/tanaikech/PDFApp
PDFApp was updated to v1.0.6.
-
v1.0.6 (May 15, 2024)
- A new method of “addPageNumbers” was added. Ref This method adds the page numbers to each page of the PDF.
You can see the detail information here https://github.com/tanaikech/PDFApp
Gists

Description
This is a simple sample script for adding the page numbers to PDF data using Google Apps Script.
When you use this script, please copy and paste the following script to the script editor of Google Apps Script. And, please set the file ID of the PDF file.
Sample script
In this script, pdf-lib is used.
/**
* ### Description
* Add page numbers to PDF.
*
* @param {Object} blob PDF blob.
* @param {Object} pageFormat Format of page number.
* @returns {Blob} Updated PDF blob.
*/
async function addPageNumbers_(blob, pageFormat) {
if (blob.getContentType() != MimeType.PDF) {
throw new Error("Blob is not PDF.");
}
// Load pdf-lib
const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";
eval(
UrlFetchApp.fetch(cdnjs)
.getContentText()
.replace(
/setTimeout\(.*?,.*?(\d*?)\)/g,
"Utilities.sleep($1);return t();"
)
);
const data = new Uint8Array(blob.getBytes());
const pdfData = await PDFLib.PDFDocument.load(data);
const pdfDoc = await PDFLib.PDFDocument.create();
(await pdfDoc.copyPages(pdfData, pdfData.getPageIndices())).forEach(
(page, i) => {
const { width } = page.getSize();
const obj = { center: width / 2, left: 20, right: width - 20 };
const pageFormatObj = { ...pageFormat };
pageFormatObj.x = obj[pageFormat.x];
page.drawText(`${i + 1}`, pageFormatObj);
pdfDoc.addPage(page);
}
);
const bytes = await pdfDoc.save();
return Utilities.newBlob(
[...new Int8Array(bytes)],
MimeType.PDF,
`new_${blob.getName()}`
);
}
// Please run this function.
function sample1() {
const fileId = "###"; // Please set the file ID of the PDF file.
const pdfBlob = DriveApp.getFileById(fileId).getBlob(); // Of course, you can directly give the PDF blob.
const pageFormat = { size: 10, x: "center", y: 10 };
addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =>
DriveApp.createFile(newBlob)
);
}
// This function is a simple demonstration script.
function sample2() {
// Create a sample Google Document.
const tempDoc = DocumentApp.create("tempDoc");
const body = tempDoc.getBody();
for (let p = 0; p < 5; p++) {
body.appendParagraph(`sample text ${p + 1}`).appendPageBreak();
}
tempDoc.saveAndClose();
const pdfBlob = tempDoc.getBlob();
// Add page numbers.
const pageFormat = { size: 10, x: "center", y: 10 };
addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =>
DriveApp.createFile(newBlob)
);
}
- When you run the function
sample1, the page numbers are added to the center of each page.
- When you run the function
sample2, a new Google Document is created including 5 pages. And, the page numbers are added to the center of each page.
- In this sample, a simple format like
{ size: 10, x: "center", y: 10 } is used for the page numbers. Here, the page numbers are put to only “left”, “center”, and “right” of the bottom of the page. But, there are various parameters in DrawTextOptions. Ref So, when you want to customize more, please modify the script.
PDFApp was updated to v1.0.5.
You can see the detail information here https://github.com/tanaikech/PDFApp
PDFApp was updated to v1.0.4.
You can see the detail information here https://github.com/tanaikech/PDFApp
PDFApp was updated to v1.0.3.
You can see the detail information here https://github.com/tanaikech/PDFApp
PDFApp was updated to v1.0.2.
-
v1.0.2 (August 21, 2023)
- A new method of
insertHeaderFooter was added. Ref When this method is used, the custom header and footer can be added when a Google Spreadsheet is exported as PDF.
You can see the detail information here https://github.com/tanaikech/PDFApp
Gists

This is a sample script for adding header and footer to PDF using Google Apps Script.
In the current stage, when Google Spreadsheet is manually exported as a PDF file at “Print settings” on the UI of Spreadsheet, the custom header and footer can be added as shown in the following image.

But, unfortunately, in the current stage, this cannot be directly achieved by Google Apps Script. So, I created this sample script. This sample script uses pdf-lib of Javasscript library.
PDFApp was updated to v1.0.1.
-
v1.0.1 (August 18, 2023)
- About the method of “getMetadata”,
pageInfo is added to the retrieved metadata. By this, each page size can be obtained.
You can see the detail information here https://github.com/tanaikech/PDFApp
Overview
This is a Google Apps Script library for managing PDFs.

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.
Gists

Abstract
When PDF file can be managed with Google Apps Script, that will lead to the automation process on cloud. In this report, the method for cooking PDF over Google Apps Script.
Introduction
Google Apps Script is one of the strong tools for achieving the automation process. When Google Apps Script can be used for the situation, it can be processed with cloud computing. By this, the users are not required to stay on the desks with the PC. One day, there might be a case where you are required to manage PDF files using Google Apps Script. When PDF files can be managed with Google Apps Script, that will be very important for achieving the automation process. Unfortunately, there are no built-in methods for directly managing PDF data using Google Apps Script. Fortunately, after the V8 runtime has been released, several raw Javascript libraries could be used with Google Apps Script. pdf-lib is also one of them. When this is used, PDF data can be cooked over Google Apps Script. In this report, I would like to introduce achieving this using a Google Apps Script library.
Gists

This is a sample script for embedding the objects in PDF using Google Apps Script.
Recently, I had a situation where it is required to manage PDFs using Google Apps Script. At that time, I had a situation where it is required to embed objects of texts and images in PDF using Google Apps Script. So, I created the following Class with Google Apps Script. When this Class is used, the objects of texts and images can embed in PDF.
Gists

This is a sample script for creating PDF forms from a Google Slide template using Google Apps Script.
Recently, I had a situation where it is required to create a custom PDF form. In that case, I thought that when a PDF form can be created from a template, it might be useful. So, I created the following Class with Google Apps Script. When this Class is used, a custom PDF form can be easily created from a Google Slide as a template.
Gists

This is a sample script for retrieving and putting values for PDF Forms using Google Apps Script.
PDF can have the PDF Form for inputting the values in the PDF by the user. Ref Recently, I had a situation that required me to retrieve and put the values to the PDF Form using Google Apps Script. In order to achieve this, I created a Class object with Google Apps Script. That is as follows.
Gists

This is a sample script for changing the order of pages in a PDF file using Google Apps Script.
Sample script
Before you run this script, please set the variables in the function main.
/**
* ### Description
* Changing order of pages in a PDF file.
*
* @param {Object} fileId is file ID of PDF file. newOrderOfpages is new order of pages. About "ignoreSkippedPages", if this is false, when the PDF has 5 pages and "newOrderOfpages" is "[3, 2]", the exported PDF file has 5 pages of 3, 2, 1, 4, 5. If this is true, when the PDF has 5 pages and "newOrderOfpages" is "[3, 2]", the exported PDF file has only 2 pages of 3 and 2.
* @return {void}
*/
async function changeOrderOfPDFPages_({
fileId,
newOrderOfpages,
ignoreSkippedPages,
}) {
// Load pdf-lib
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) {
Utilities.sleep(t);
return f();
};
const blob = DriveApp.getFileById(fileId).getBlob();
const pdfData = await PDFLib.PDFDocument.load(
new Uint8Array(blob.getBytes())
);
const numberOfPages = pdfData.getPageCount();
const maxPage = Math.max(...newOrderOfpages);
if (numberOfPages < maxPage || numberOfPages < newOrderOfpages.length) {
throw new Error(
"Maximum page in the order of pages is over than the maximum page of the original PDF file."
);
}
let skippedPages = [];
if (!ignoreSkippedPages && numberOfPages > newOrderOfpages.length) {
skippedPages = [...Array(numberOfPages)]
.map((_, i) => i + 1)
.filter((e) => !newOrderOfpages.includes(e));
}
const pdfDoc = await PDFLib.PDFDocument.create();
const pages = await pdfDoc.copyPages(
pdfData,
[...Array(numberOfPages)].map((_, i) => i)
);
[...newOrderOfpages, ...skippedPages].forEach((e) =>
pdfDoc.addPage(pages[e - 1])
);
const bytes = await pdfDoc.save();
return Utilities.newBlob(
[...new Int8Array(bytes)],
MimeType.PDF,
"sample.pdf"
);
}
function main() {
const fileId = "###"; // Please set a file ID of your a PDF file or a file ID of Google Docs files (Document, Spreadsheet, Slide).
const newOrderOfpages = [3, 1, 2, 5, 4]; // Please set new order of the pages in a PDF file. In this sample, the order of pages of the original PDF file is changed to 3, 1, 2, 5, 4.
const ignoreSkippedPages = false; // If this is false, when the PDF has 5 pages and "newOrderOfpages" is "[3, 2]", the exported PDF file has 5 pages of 3, 2, 1, 4, 5. If this is true, when the PDF has 5 pages and "newOrderOfpages" is "[3, 2]", the exported PDF file has only 2 pages of 3 and 2.
changeOrderOfPDFPages_({ fileId, newOrderOfpages, ignoreSkippedPages }).then(
(blob) => {
DriveApp.createFile(blob.setName("sample.pdf"));
}
);
}
When this script is run, a new PDF file is created with the new order of pages.
Gists

This is a sample script for managing the metadata of PDF data using Google Apps Script.
There might be a case in that you want to retrieve and update the metadata of PDF data using Google Apps Script. In this post, I would like to introduce achieving this.
This is a Class ManagePdfMetadata. This Class is used for managing the metadata of PDF files using Google Apps Script. And, in this Class, a Javascript library of pdf-lib is used for managing the PDF metadata. This Javascript library is loaded in this Class.
Gists

This is a sample script for exporting the specific pages from a PDF as a new PDF using Google Apps Script.
In this sample script, pdf-lib is used. In the current stage, it seems that this Javascript can be directly used with Google Apps Script.
Sample script
async function myFunction() {
// Retrieve PDF data.
const fileId = "###"; // Please set a file ID of your a PDF file or a file ID of Google Docs files (Document, Spreadsheet, Slide).
const pageNumbers = [2, 4]; // In this sample, 2 and 4 pages are exported as a PDF.
const blob = DriveApp.getFileById(fileId).getBlob();
// Merge PDFs.
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) {
Utilities.sleep(t);
return f();
};
const pdfDoc = await PDFLib.PDFDocument.create();
const pdfData = await PDFLib.PDFDocument.load(
new Uint8Array(blob.getBytes())
);
const pages = await pdfDoc.copyPages(
pdfData,
[...Array(pdfData.getPageCount())].map((_, i) => i)
);
pages.forEach((page, i) => {
if (pageNumbers.includes(i + 1)) {
pdfDoc.addPage(page);
}
});
const bytes = await pdfDoc.save();
// Create a PDF file.
DriveApp.createFile(
Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, "sample.pdf")
);
}
Gists

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.
Gists

This is a sample script for merging multiple PDF files as a single PDF file using Google Apps Script.
In this sample script, pdf-lib is used. In the current stage, it seems that this Javascript can be directly used with Google Apps Script.
Sample script 1
As a sample situation, please put multiple PDF files in your Google Drive. This sample merges those PDF files as a single PDF file.