Skip to content

Commit

Permalink
Merge pull request #76 from datalogics-erics/main
Browse files Browse the repository at this point in the history
Add Google Apps Script Integration Sample
  • Loading branch information
datalogics-tsmith authored Aug 20, 2024
2 parents 931d87b + b49afe8 commit 8a81025
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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);
1 change: 1 addition & 0 deletions Google Apps Script/Advanced Integrations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
In this directory you will find advanced integrations between pdfRest and Google Apps Script with sample code and instructions.
7 changes: 7 additions & 0 deletions Google Apps Script/README.md
Original file line number Diff line number Diff line change
@@ -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.

<br/>

# Is pdfRest compatible with Google Apps Script?
Yes, pdfRest easily integrates with Google Apps Script to automate PDF processing tasks within the Google Workspace.

0 comments on commit 8a81025

Please sign in to comment.