pdf-lib is a JavaScript library that allows dynamic creation and modification of PDFs. In NetSuite, you can leverage pdf-lib within a SuiteScript to customize PDF documents dynamically.
Steps to Use pdf-lib in NetSuite:
- Include the pdf-lib library: Since NetSuite does not support direct NPM installations, you need to upload
pdf-libas a script file to the NetSuite File Cabinet and reference it in your script. - Load the library in your SuiteScript using
require. - Modify PDF content dynamically.
Key Features of pdf-lib:
- Create PDFs from scratch.
- Modify existing PDFs by adding text, images, and annotations.
- Merge or split PDF pages.
This approach helps in dynamically generating or customizing PDFs in NetSuite without relying on external services.
Sample Script for PDF Modification:
/**
* @NApiVersion 2.x
* @NScriptType Suitelet
*/
defined(['N/ui/serverWidget', 'N/file', 'N/log', 'pdf-lib'], function (serverWidget, file, log, PDFLib) {
function onRequest(context) {
try {
// Load an existing PDF from the File Cabinet
var pdfFile = file.load({ id: '12345' });
var pdfBytes = pdfFile.getContents();
// Load the document using pdf-lib
var pdfDoc = PDFLib.PDFDocument.load(pdfBytes);
var pages = pdfDoc.getPages();
var firstPage = pages[0];
// Draw text on the first page
firstPage.drawText('Custom Text', {
x: 50,
y: 700,
size: 12
});
// Save the modified PDF
var modifiedPdfBytes = pdfDoc.save();
var modifiedPdfFile = file.create({
name: 'modified_document.pdf',
fileType: file.Type.PDF,
contents: modifiedPdfBytes,
folder: 123 // Target folder ID
});
modifiedPdfFile.save();
log.debug('Success', 'PDF modified successfully.');
} catch (e) {
log.error('Error modifying PDF', e);
}
}
return { onRequest: onRequest };
});