Help

Re: Airtable Automation Script - PDF Generator API

597 0
cancel
Showing results for 
Search instead for 
Did you mean: 
dhruv333
4 - Data Explorer
4 - Data Explorer

I'm new to Airtable and automation script.

All I wanted is to save .pdf file which is generated from PDF Generator API. I don't know about the java script either. I got this script using AI and git hub. I am still getting errors can someone help me with it? the script is attached.

/**
* PDF Generator API configuration (Replace with your actual credentials)
*/
const apiKey = '';
const apiSecret = '';
const apiWorkspace = '';
const templateId = '';
const url = `https://us1.pdfgeneratorapi.com/api/v3/templates/${templateId}/output?output=url`;

/**
* Airtable table and record config.
*/
const partNumberTable = base.getTable('Part Number');
const saveDocToField = 'Print';

// Get the record ID from input variables
const partNumberId = input.config.recordId;

/**
* Find record from "Part Number" table
*/
let records = await loadRecords(partNumberTable, [partNumberId]);

// Check if a valid record was found and handle potential errors
if (records && records.length > 0 && records[0]) {
/**
* Make PDF Generator API request with API credentials and record data as request body
*/
const response = await remoteFetchAsync(url, {
method: 'POST',
redirect: 'follow',
headers: {
'Content-Type': 'application/json',
'X-Auth-Key': apiKey,
'X-Auth-Secret': apiSecret,
'X-Auth-Workspace': apiWorkspace
},
body: JSON.stringify(records),
});

const result = await response.json(); // Directly parse the response as JSON

if (result.error) {
throw new Error(result.error); // Throw an error with a clear message
}

/**
* Save generated PDF document to attachment field
*/
await partNumberTable.updateRecordAsync(partNumberId, {
[saveDocToField]: [{ url: result.response, filename: result.meta.display_name }]
});
} else {
// Handle the case where no valid record was found or an error occurred
console.error('Error fetching record or record not found for recordId:', partNumberId);
throw new Error('Record not found or invalid.');
}

/**
* Load records from table by list of ids
*
* @param {Table} table
* @param {Array} recordIds
*/
async function loadRecords(table, recordIds) {
const queryResult = await table.selectRecordsAsync();

return Promise.all(recordIds.map(function(recordId) {
if (typeof recordId !== 'string' || !recordId) {
console.error('Invalid recordId:', recordId);
return null;
} else {
const record = queryResult.getRecord(recordId);
if (record) { // Check if the record exists
return convertToObject(table, record);
} else {
console.error('Record not found for recordId:', recordId);
return null;
}
}
}));
}

/**
* Converts Record class into plain JavaScript object
*
* @param {Table} table
* @param {Record} record
*/
async function convertToObject(table, record) {
let r = {
id: record.id
};

for (let i = 0; i < table.fields.length; i++) {
const field = table.fields[i];
let value = record.getCellValue(field.name);
r[field.name] = value;
}

return r;
}

 

2 Replies 2
ScottWorld
18 - Pluto
18 - Pluto

I don’t know scripting so I can’t help you there, but if you want to do it without using scripting at all, check out Make’s PDF Generator API integrations and Make’s Airtable integrations

You can also use other PDF tools as well, because Make supports many different PDF generating apps.

SamGluck
6 - Interface Innovator
6 - Interface Innovator

I had the same issue as you just two days ago!

I found the best one to work is "PDF Maker - Automate Documents".

Found it in extensions.