♊️ GemiNews 🗞️
(dev)
🏡
📰 Articles
🏷️ Tags
🧠 Queries
📈 Graphs
☁️ Stats
💁🏻 Assistant
💬
🎙️
Demo 1: Embeddings + Recommendation
Demo 2: Bella RAGa
Demo 3: NewRetriever
Demo 4: Assistant function calling
Editing article
Title
Summary
Content
<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-snkrdxfwDYFSNkr.png" /></figure><h3>Description</h3><p>This is a simple sample script for adding the page numbers to PDF data using Google Apps Script.</p><p>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.</p><h3>Sample script</h3><p>In this script, <a href="https://pdf-lib.js.org/">pdf-lib</a> is used.</p><pre>/**<br>* ### Description<br>* Add page numbers to PDF.<br>*<br>* @param {Object} blob PDF blob.<br>* @param {Object} pageFormat Format of page number.<br>* @returns {Blob} Updated PDF blob.<br>*/<br>async function addPageNumbers_(blob, pageFormat) {<br> if (blob.getContentType() != MimeType.PDF) {<br> throw new Error("Blob is not PDF.");<br> }<br><br> // Load pdf-lib<br> const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";<br> eval(UrlFetchApp.fetch(cdnjs).getContentText().replace(/setTimeout\(.*?,.*?(\d*?)\)/g, "Utilities.sleep($1);return t();"));<br><br> const data = new Uint8Array(blob.getBytes());<br> const pdfData = await PDFLib.PDFDocument.load(data);<br> const pdfDoc = await PDFLib.PDFDocument.create();<br> (await pdfDoc.copyPages(pdfData, pdfData.getPageIndices()))<br> .forEach((page, i) => {<br> const { width } = page.getSize();<br> const obj = { center: width / 2, left: 20, right: width - 20 };<br> const pageFormatObj = { ...pageFormat };<br> pageFormatObj.x = obj[pageFormat.x];<br> page.drawText(`${i + 1}`, pageFormatObj);<br> pdfDoc.addPage(page);<br> });<br> const bytes = await pdfDoc.save();<br> return Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, `new_${blob.getName()}`);<br>}<br><br>// Please run this function.<br>function sample1() {<br> const fileId = "###"; // Please set the file ID of the PDF file.<br> const pdfBlob = DriveApp.getFileById(fileId).getBlob(); // Of course, you can directly give the PDF blob.<br><br> const pageFormat = { size: 10, x: "center", y: 10 };<br> addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =><br> DriveApp.createFile(newBlob)<br> );<br>}<br><br>// This function is a simple demonstration script.<br>function sample2() {<br> // Create a sample Google Document.<br> const tempDoc = DocumentApp.create("tempDoc");<br> const body = tempDoc.getBody();<br> for (let p = 0; p < 5; p++) {<br> body.appendParagraph(`sample text ${p + 1}`).appendPageBreak();<br> }<br> tempDoc.saveAndClose();<br> const pdfBlob = tempDoc.getBlob();<br><br> // Add page numbers.<br> const pageFormat = { size: 10, x: "center", y: 10 };<br> addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =><br> DriveApp.createFile(newBlob)<br> );<br>}</pre><ul><li>When you run the function sample1, the page numbers are added to the center of each page.</li><li>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.</li><li>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. <a href="https://pdf-lib.js.org/docs/api/interfaces/drawtextoptions">Ref</a> So, when you want to customize more, please modify the script.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae964fb07655" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/adding-page-numbers-to-pdf-using-google-apps-script-ae964fb07655">Adding Page Numbers to PDF using Google Apps Script</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>
Author
Link
Published date
Image url
Feed url
Guid
Hidden blurb
--- !ruby/object:Feedjira::Parser::RSSEntry title: Adding Page Numbers to PDF using Google Apps Script url: https://medium.com/google-cloud/adding-page-numbers-to-pdf-using-google-apps-script-ae964fb07655?source=rss----e52cf94d98af---4 author: Kanshi Tanaike categories: - javascript - pdf - google-cloud-platform - google-apps-script - gcp-app-dev published: 2024-05-15 00:49:19.000000000 Z entry_id: !ruby/object:Feedjira::Parser::GloballyUniqueIdentifier is_perma_link: 'false' guid: https://medium.com/p/ae964fb07655 carlessian_info: news_filer_version: 2 newspaper: Google Cloud - Medium macro_region: Blogs rss_fields: - title - url - author - categories - published - entry_id - content content: '<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*-snkrdxfwDYFSNkr.png" /></figure><h3>Description</h3><p>This is a simple sample script for adding the page numbers to PDF data using Google Apps Script.</p><p>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.</p><h3>Sample script</h3><p>In this script, <a href="https://pdf-lib.js.org/">pdf-lib</a> is used.</p><pre>/**<br>* ### Description<br>* Add page numbers to PDF.<br>*<br>* @param {Object} blob PDF blob.<br>* @param {Object} pageFormat Format of page number.<br>* @returns {Blob} Updated PDF blob.<br>*/<br>async function addPageNumbers_(blob, pageFormat) {<br> if (blob.getContentType() != MimeType.PDF) {<br> throw new Error("Blob is not PDF.");<br> }<br><br> // Load pdf-lib<br> const cdnjs = "https://cdn.jsdelivr.net/npm/pdf-lib/dist/pdf-lib.min.js";<br> eval(UrlFetchApp.fetch(cdnjs).getContentText().replace(/setTimeout\(.*?,.*?(\d*?)\)/g, "Utilities.sleep($1);return t();"));<br><br> const data = new Uint8Array(blob.getBytes());<br> const pdfData = await PDFLib.PDFDocument.load(data);<br> const pdfDoc = await PDFLib.PDFDocument.create();<br> (await pdfDoc.copyPages(pdfData, pdfData.getPageIndices()))<br> .forEach((page, i) => {<br> const { width } = page.getSize();<br> const obj = { center: width / 2, left: 20, right: width - 20 };<br> const pageFormatObj = { ...pageFormat };<br> pageFormatObj.x = obj[pageFormat.x];<br> page.drawText(`${i + 1}`, pageFormatObj);<br> pdfDoc.addPage(page);<br> });<br> const bytes = await pdfDoc.save();<br> return Utilities.newBlob([...new Int8Array(bytes)], MimeType.PDF, `new_${blob.getName()}`);<br>}<br><br>// Please run this function.<br>function sample1() {<br> const fileId = "###"; // Please set the file ID of the PDF file.<br> const pdfBlob = DriveApp.getFileById(fileId).getBlob(); // Of course, you can directly give the PDF blob.<br><br> const pageFormat = { size: 10, x: "center", y: 10 };<br> addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =><br> DriveApp.createFile(newBlob)<br> );<br>}<br><br>// This function is a simple demonstration script.<br>function sample2() {<br> // Create a sample Google Document.<br> const tempDoc = DocumentApp.create("tempDoc");<br> const body = tempDoc.getBody();<br> for (let p = 0; p < 5; p++) {<br> body.appendParagraph(`sample text ${p + 1}`).appendPageBreak();<br> }<br> tempDoc.saveAndClose();<br> const pdfBlob = tempDoc.getBlob();<br><br> // Add page numbers.<br> const pageFormat = { size: 10, x: "center", y: 10 };<br> addPageNumbers_(pdfBlob, pageFormat).then((newBlob) =><br> DriveApp.createFile(newBlob)<br> );<br>}</pre><ul><li>When you run the function sample1, the page numbers are added to the center of each page.</li><li>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.</li><li>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. <a href="https://pdf-lib.js.org/docs/api/interfaces/drawtextoptions">Ref</a> So, when you want to customize more, please modify the script.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ae964fb07655" width="1" height="1" alt=""><hr><p><a href="https://medium.com/google-cloud/adding-page-numbers-to-pdf-using-google-apps-script-ae964fb07655">Adding Page Numbers to PDF using Google Apps Script</a> was originally published in <a href="https://medium.com/google-cloud">Google Cloud - Community</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>'
Language
Active
Ricc internal notes
Imported via /Users/ricc/git/gemini-news-crawler/webapp/db/seeds.d/import-feedjira.rb on 2024-05-15 03:45:01 +0200. Content is EMPTY here. Entried: title,url,author,categories,published,entry_id,content. TODO add Newspaper: filename = /Users/ricc/git/gemini-news-crawler/webapp/db/seeds.d/../../../crawler/out/feedjira/Blogs/Google Cloud - Medium/2024-05-15-Adding_Page_Numbers_to_PDF_using_Google_Apps_Script-v2.yaml
Ricc source
Show this article
Back to articles