I am using Google Apps script to create a PDF file. This is stored in my Google Drive.
Yesterday I added to the script that I need a copy of new PDF files send to dropbox.
Below is the script I am using.
Yesterday everything worked well, a copy was sent to the dropbox.
Today when I run the script, it get this error:
function createPDF(info) {
// Get the folders in Google Drive where the PDFs should be saved
var pdfFolder = DriveApp.getFolderById("186bAn5kZu5Mq5FaVkQCD-2I5L3t9RWsO");
// Access token for Dropbox API
var accessToken = "MY_ACCES_TOKEN";
//Dropbox Folder Path to place files in
var dropboxPath = "/AutoPrintPDF/";
// Get the folder in Google Drive where the temporary files should be saved
var tempFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxx");
// Get the template document that will be used to create the PDF
var templateDoc = DriveApp.getFileById("xxxxxxxxxxxxxxxxxxxxxxxxxx");
// Make a copy of the template document and save it in the tempFolder
var newTempFile = templateDoc.makeCopy(tempFolder);
// Open the copied document
var openDoc = DocumentApp.openById(newTempFile.getId());
// Get the body of the document
var body = openDoc.getBody();
// Replace placeholders in the document with the relevant data
body.replaceText("{truck}", info['Truck'].toUpperCase());
body.replaceText("{trailer}", info['Trailer'].toUpperCase());
body.replaceText("{valid}", info['Valid Until']);
body.replaceText("{pausestart}", info['Pause Start']);
body.replaceText("{pauseend}", info['Pause End']);
// Save and close the document
openDoc.saveAndClose();
// Convert document to PDF
var blobPDF = newTempFile.getAs(MimeType.PDF);
// Create a new file in pdfFolder with the name of the parking permit
var pdfFile = pdfFolder.createFile(blobPDF).setName(info['Created at'] + " - Truck: " + info['Truck']);
// Create a new file for Dropbox without special characters in the filename (Sync to windows fails when filename contains special characters)
var pdfName = info['Created at'] + " - Truck: " + info['Truck'];
var sanitizedPdfName = pdfName.replace(/[^\w\s]/gi, '');
var pdfFile = pdfFolder.createFile(blobPDF).setName(pdfName);
sendToDropbox(sanitizedPdfName, blobPDF);
// Get the URL of the PDF
var pdfUrl = pdfFile.getUrl();
// Insert link to PDF in column 8
var sheet = SpreadsheetApp.getActive().getSheetByName("Filter");
var lastRow = sheet.getLastRow();
var linkCell = sheet.getRange(lastRow, 8);
linkCell.setValue(pdfUrl);
// Delete the tempFile after PDF is created. Files in trash folder will be deleted after 30 days.
DriveApp.getFileById(newTempFile.getId()).setTrashed(true)
function sendToDropbox(pdfName, blobPDF) {
var parameters = {
"path": dropboxPath + pdfName + ".pdf",
};
var headers = {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/octet-stream',
'Dropbox-API-Arg': JSON.stringify(parameters)
}
var options = {
"method": "POST",
"headers": headers,
"payload": blobPDF
};
var respons = JSON.parse(UrlFetchApp.fetch(apiurl, options).getContentText());
}