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); 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. 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.