n8n-nodes-hwp-to-pdf
Version:
n8n node to convert HWP files to PDF using huhsame.com API
122 lines (121 loc) • 4.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HwpToPdf = void 0;
class HwpToPdf {
constructor() {
this.description = {
displayName: 'HWP to PDF',
name: 'hwpToPdf',
icon: 'file:hwp.svg',
group: ['transform'],
version: 1,
description: 'Upload HWP file and receive PDF',
defaults: {
name: 'HWP to PDF',
},
inputs: ["main" /* NodeConnectionType.Main */],
outputs: ["main" /* NodeConnectionType.Main */],
properties: [
{
displayName: 'Binary Property',
name: 'binaryPropertyName',
type: 'string',
default: 'data',
description: 'Name of the binary property containing the HWP file',
},
{
displayName: 'PDF Filename',
name: 'pdfFilename',
type: 'string',
default: 'converted',
description: 'Name for the resulting PDF file (without .pdf)',
},
{
displayName: 'API Endpoint',
name: 'apiEndpoint',
type: 'string',
default: 'https://api.huhsame.com/convert',
description: 'API endpoint for HWP to PDF conversion',
},
{
displayName: 'Timeout (seconds)',
name: 'timeout',
type: 'number',
default: 60,
description: 'Timeout in seconds for the API request',
},
],
};
}
async execute() {
var _a;
const items = this.getInputData();
const returnItems = [];
for (let i = 0; i < items.length; i++) {
try {
const binaryPropertyName = this.getNodeParameter('binaryPropertyName', i);
const pdfFilename = this.getNodeParameter('pdfFilename', i);
const apiEndpoint = this.getNodeParameter('apiEndpoint', i);
const timeout = this.getNodeParameter('timeout', i) * 1000; // Convert to milliseconds
const binaryData = (_a = items[i].binary) === null || _a === void 0 ? void 0 : _a[binaryPropertyName];
if (!binaryData) {
throw new Error(`No binary data found for property "${binaryPropertyName}"`);
}
const buffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
const formData = {
file: {
value: buffer,
options: {
filename: binaryData.fileName || 'input.hwp',
contentType: binaryData.mimeType || 'application/octet-stream',
},
},
filename: pdfFilename,
};
try {
const response = await this.helpers.request({
method: 'POST',
url: apiEndpoint,
formData,
encoding: null,
timeout,
});
if (!response || !Buffer.isBuffer(response)) {
throw new Error('Invalid response from API');
}
const newItem = {
json: {},
binary: {
[binaryPropertyName]: await this.helpers.prepareBinaryData(response, `${pdfFilename}.pdf`, 'application/pdf'),
},
};
returnItems.push([newItem]);
}
catch (error) {
if (error.response) {
throw new Error(`API Error: ${error.response.status} - ${error.response.statusText}`);
}
else if (error.code === 'ETIMEDOUT') {
throw new Error(`Request timed out after ${timeout / 1000} seconds`);
}
else {
throw new Error(`Request failed: ${error.message}`);
}
}
}
catch (error) {
if (this.continueOnFail()) {
returnItems.push([{
json: {
error: error.message,
},
}]);
continue;
}
throw error;
}
}
return returnItems;
}
}
exports.HwpToPdf = HwpToPdf;