Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Google Apps Script Integration Sample #76

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Loading