Convert PDF to Text - GoogleAppScript
PDF To TEXT sample in GoogleAppScript demonstrating ‘Convert PDF to Text’
program.gs
// Visit Knowledgebase for adding Text Macros to PDF
// https://apidocs.pdf.co/kb/Fill%20PDF%20and%20Add%20Text%20or%20Images%20(pdf-edit-add)/macros
// Handle PDF.co Menu on Google Sheet Open Event
function onOpen() {
var spreadsheet = SpreadsheetApp.getActive();
var menuItems = [
{name: 'CreatePDF', functionName: 'fillPDFForm'}
];
spreadsheet.addMenu('PDF.co', menuItems);
}
/**
* A function that adds headers and some initial data to the spreadsheet.
*/
function fillPDFForm() {
var spreadsheet = SpreadsheetApp.getActive();
// Get PDF.co API Key Cell
let pdfCoAPIKeyCell = spreadsheet.getRange("A1");
// Get Cells for Input/Output
let pdfUrlCell = spreadsheet.getRange("A2");
let resultUrlCell = spreadsheet.getRange("A3");
let pdfCoAPIKey = pdfCoAPIKeyCell.getValue();
let pdfUrl = pdfUrlCell.getValue();
// Prepare Payload
var data = {
"async": false,
"encrypt": false,
"inline": false,
"name": "sample_result",
"url": pdfUrl
};
// Prepare Request Options
var options = {
'method' : 'post',
'contentType': 'application/json',
'headers': {
"x-api-key": pdfCoAPIKey
},
// Convert the JavaScript object to a JSON string.
'payload' : JSON.stringify(data)
};
// Get Response
// https://developers.google.com/apps-script/reference/url-fetch
var pdfCoResponse = UrlFetchApp.fetch('https://api.pdf.co/v1/pdf/convert/to/text', options);
var pdfCoRespContent = pdfCoResponse.getContentText();
var pdfCoRespJson = JSON.parse(pdfCoRespContent);
// Display Result
if(!pdfCoRespJson.error){
resultUrlCell.setValue(pdfCoRespJson.url);
}
else{
resultUrlCell.setValue(pdfCoRespJson.message);
}
}
PDF.co Web API: the Web API with a set of tools for documents manipulation, data conversion, data extraction, splitting and merging of documents. Includes image recognition, built-in OCR, barcode generation and barcode decoders to decode bar codes from scans, pictures and pdf.
Download Source Code (.zip)
return to the previous page explore PDF To TEXT endpoint
Copyright © 2016 - 2023 PDF.co