From 9890bf42d28d687db6a5c3b3a15f6f901e528ca7 Mon Sep 17 00:00:00 2001 From: Eric Shore Date: Tue, 20 Aug 2024 10:52:33 -0500 Subject: [PATCH 1/3] Create README.md Add README for Google Apps Script --- Google Apps Script/README.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 Google Apps Script/README.md diff --git a/Google Apps Script/README.md b/Google Apps Script/README.md new file mode 100644 index 0000000..924235b --- /dev/null +++ b/Google Apps Script/README.md @@ -0,0 +1,7 @@ +# What is Google Apps Script? +[Apps Script](https://www.google.com/script/start/) is a rapid application development platform based on JavaScript that makes it fast and easy to create business applications that integrate with Google Workspace. It can be used to build web apps and automate tasks. + +
+ +# Is pdfRest compatible with Google Apps Script? +Yes, pdfRest easily integrates with Google Apps Script to automate PDF processing tasks within the Google Workspace. From b22dde70a4f014a2989d2f16d97d712738c40643 Mon Sep 17 00:00:00 2001 From: Eric Shore Date: Tue, 20 Aug 2024 14:04:11 -0500 Subject: [PATCH 2/3] Create uploadAndMergePDFs.gs Add Google Apps Script sample code for merging PDFs --- .../Merge PDFs/uploadAndMergePDFs.gs | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 Google Apps Script/Advanced Integrations/Merge PDFs/uploadAndMergePDFs.gs diff --git a/Google Apps Script/Advanced Integrations/Merge PDFs/uploadAndMergePDFs.gs b/Google Apps Script/Advanced Integrations/Merge PDFs/uploadAndMergePDFs.gs new file mode 100644 index 0000000..bfd7624 --- /dev/null +++ b/Google Apps Script/Advanced Integrations/Merge PDFs/uploadAndMergePDFs.gs @@ -0,0 +1,111 @@ +function uploadAndMergePDFs(pdfFiles) { + // Replace with your actual API key + const apiKey = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; + + const uploadUrl = "https://api.pdfrest.com/upload"; + const mergeUrl = "https://api.pdfrest.com/merged-pdf"; + + const uploadedFiles = []; + + const pagesArr = []; + const typeArr = []; + + // Upload each PDF file + for (const pdfFile of pdfFiles) { + const uploadData = pdfFile.getBlob().getBytes(); + const uploadOptions = { + "method" : "post", + "payload" : uploadData, + "headers" : { + "Api-Key": apiKey, + "Content-Filename": pdfFile.getName(), + "Content-Type": "application/octet-stream" + } + }; + + const uploadResponse = UrlFetchApp.fetch(uploadUrl, uploadOptions); + if (uploadResponse.getResponseCode() !== 200) { + throw new Error(`Failed to upload file: ${uploadResponse.getContentText()}`); + } + + uploadedFiles.push(JSON.parse(uploadResponse.getContentText()).files[0].id); + pagesArr.push("1-last"); + typeArr.push("id"); + } + + // Wait for all uploads to complete + uploadedFiles.forEach(fileId => waitForUploadCompletion(fileId)); + + + // Prepare merge request data + const mergeData = { + "id": uploadedFiles, + "type": typeArr, + "pages": pagesArr, + }; + + const mergeOptions = { + "method" : "post", + "payload" : JSON.stringify(mergeData), + "headers" : { + "Api-Key": apiKey, + "Content-Type": "application/json" + } + }; + + const mergeResponse = UrlFetchApp.fetch(mergeUrl, mergeOptions); + if (mergeResponse.getResponseCode() !== 200) { + throw new Error(`Failed to merge PDFs: ${mergeResponse.getContentText()}`); + } + + console.log("PDFs merged successfully!"); + + // You can access the response data here: + const mergedPdfInfo = JSON.parse(mergeResponse.getContentText()); + + return (mergedPdfInfo.outputId); +} + +function checkUploadStatus(fileId) { + const options = { + "method": "get", + }; + const response = UrlFetchApp.fetch(`https://api.pdfrest.com/resource/${fileId}?format=info`, options); + const data = JSON.parse(response.getContentText()); + return data.size > 0; +} + +function waitForUploadCompletion(fileId) { + while (!checkUploadStatus(fileId)) { + Utilities.sleep(1000); // Wait 1 second before checking again + } +} + +function getFile(fileId, folderId) { + const options = { + "method": "get", + "responseType": UrlFetchApp.BLOB, // Specify response type as blob + }; + const response = UrlFetchApp.fetch(`https://api.pdfrest.com/resource/${fileId}?format=file`, options); + const pdfBlob = response.getBlob(); // Directly get the blob from the response + const folder = DriveApp.getFolderById(folderId); + const result = folder.createFile(pdfBlob); + console.log("Merged PDF downloaded!"); +} + +// Example usage - merge all PDFs in Google Drive folder +const folderId = "xxxxx-xxxxxx-xxxxxxxx_xxxxxxxxxxx"; // Replace with the ID of your target folder +const folder = DriveApp.getFolderById(folderId); +const files = folder.getFiles(); + +const pdfFiles = []; +while (files.hasNext()) { + const file = files.next(); + if (file.getMimeType() === MimeType.PDF) { + pdfFiles.push(file); + } +} + +const outputId = uploadAndMergePDFs(pdfFiles); + +getFile(outputId,folderId); From b49afe874e1be848801b81ff6254bad93278f2b8 Mon Sep 17 00:00:00 2001 From: Eric Shore Date: Tue, 20 Aug 2024 14:07:05 -0500 Subject: [PATCH 3/3] Create README.md add README --- Google Apps Script/Advanced Integrations/README.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 Google Apps Script/Advanced Integrations/README.md diff --git a/Google Apps Script/Advanced Integrations/README.md b/Google Apps Script/Advanced Integrations/README.md new file mode 100644 index 0000000..4bfce46 --- /dev/null +++ b/Google Apps Script/Advanced Integrations/README.md @@ -0,0 +1 @@ +In this directory you will find advanced integrations between pdfRest and Google Apps Script with sample code and instructions.