♊️ GemiNews 🗞️ (dev)

Demo 1: Embeddings + Recommendation Demo 2: Bella RAGa Demo 3: NewRetriever Demo 4: Assistant function calling

🗞️Adding Page Numbers to PDF using Google Apps Script

🗿Semantically Similar Articles (by :title_embedding)

Adding Page Numbers to PDF using Google Apps Script

2024-05-15 - Kanshi Tanaike (from Google Cloud - Medium)

DescriptionThis 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 scriptIn 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.Adding Page Numbers to PDF using Google Apps Script was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.

[Blogs] 🌎 https://medium.com/google-cloud/adding-page-numbers-to-pdf-using-google-apps-script-ae964fb07655?source=rss----e52cf94d98af---4 [🧠] [v2] article_embedding_description:
[🧠] [v1/3] title_embedding_description:
[🧠] [v1/3] summary_embedding_description:
[🧠] As per bug https://github.com/palladius/gemini-news-crawler/issues/4 we can state this article belongs to titile/summary version: v1 (like 99% of articles)

🗿article.to_s

------------------------------
Title: Adding Page Numbers to PDF using Google Apps Script
[content]
DescriptionThis 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 scriptIn 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.Adding Page Numbers to PDF using Google Apps Script was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.
[/content]

Author: Kanshi Tanaike
PublishedDate: 2024-05-15
Category: Blogs
NewsPaper: Google Cloud - Medium
Tags: javascript, pdf, google-cloud-platform, google-apps-script, gcp-app-dev
{"id"=>10320,
"title"=>"Adding Page Numbers to PDF using Google Apps Script",
"summary"=>nil,
"content"=>"
\"\"

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.
\"\"

Adding Page Numbers to PDF using Google Apps Script was originally published in Google Cloud - Community on Medium, where people are continuing the conversation by highlighting and responding to this story.

",
"author"=>"Kanshi Tanaike",
"link"=>"https://medium.com/google-cloud/adding-page-numbers-to-pdf-using-google-apps-script-ae964fb07655?source=rss----e52cf94d98af---4",
"published_date"=>Wed, 15 May 2024 00:49:19.000000000 UTC +00:00,
"image_url"=>nil,
"feed_url"=>"https://medium.com/google-cloud/adding-page-numbers-to-pdf-using-google-apps-script-ae964fb07655?source=rss----e52cf94d98af---4",
"language"=>nil,
"active"=>true,
"ricc_source"=>"feedjira::v1",
"created_at"=>Wed, 15 May 2024 01:45:04.677864000 UTC +00:00,
"updated_at"=>Wed, 15 May 2024 01:45:04.677864000 UTC +00:00,
"newspaper"=>"Google Cloud - Medium",
"macro_region"=>"Blogs"}
Edit this article
Back to articles