UNPKG

lbx-invoice

Version:

Provides functionality around generating invoices.

89 lines 3.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PdfUtilities = void 0; const tslib_1 = require("tslib"); const promises_1 = require("fs/promises"); const pdf_lib_1 = require("pdf-lib"); const pdfmake_min_1 = tslib_1.__importDefault(require("pdfmake/build/pdfmake.min")); const vfs_fonts_1 = tslib_1.__importDefault(require("pdfmake/build/vfs_fonts")); pdfmake_min_1.default.vfs = vfs_fonts_1.default.pdfMake.vfs; /** * Utility class for handling pdf files. */ class PdfUtilities { /** * Creates a pdf from the provided document definition. * @param documentDefinition - The definition to create the pdf from. * @returns The finished pdfMake pdf as a base64 string. */ static async createPdf(documentDefinition) { return new Promise((resolve, reject) => { try { pdfmake_min_1.default.createPdf(documentDefinition).getBase64(res => resolve(res)); } catch (error) { reject(error); } }); } /** * Attaches a file to the pdf at the given path. * @param pdfFilePath - The path of the pdf to attach to. * @param attachment - The raw attachment data. * @param name - The name of the file to attach. * @param options - Other options for the attachment, eg. The mime type. */ static async attach(pdfFilePath, attachment, name, options) { const existingPdfBytes = await (0, promises_1.readFile)(pdfFilePath); const pdfDoc = await pdf_lib_1.PDFDocument.load(existingPdfBytes); await pdfDoc.attach(attachment, name, options); const newPdfBytes = await pdfDoc.save(); await (0, promises_1.writeFile)(pdfFilePath, newPdfBytes); } // TODO: enable /** * Sets the XMP Metadata from the given xml string. * @param metadataXML - The xmp metadata xml as a string. * @param pdfFilePath - The path of the pdf to set the metadata for. */ static async setXmpMetadata(metadataXML, pdfFilePath) { const existingPdfBytes = await (0, promises_1.readFile)(pdfFilePath); const pdfDoc = await pdf_lib_1.PDFDocument.load(existingPdfBytes); const metadataStream = pdfDoc.context.stream(metadataXML, { Type: 'Metadata', Subtype: 'XML', Length: metadataXML.length }); const metadataStreamRef = pdfDoc.context.register(metadataStream); pdfDoc.catalog.set(pdf_lib_1.PDFName.of('Metadata'), metadataStreamRef); const newPdfBytes = await pdfDoc.save(); await (0, promises_1.writeFile)(pdfFilePath, newPdfBytes); } // TODO: enable /** * Gets the XMP Metadata from the pdf under the provided path. * @param pdfFilePath - The path of the pdf to get the xmp metadata from. * @returns An PDFObject or undefined, when no metadata was set. */ static async getXmpMetadata(pdfFilePath) { const existingPdfBytes = await (0, promises_1.readFile)(pdfFilePath); const pdfDoc = await pdf_lib_1.PDFDocument.load(existingPdfBytes); // eslint-disable-next-line typescript/no-unsafe-call const infoDict = pdfDoc['getInfoDict'](); const metadata = infoDict.lookup(pdf_lib_1.PDFName.of('Metadata')); if (!metadata) { return undefined; } this.assertIsLiteralOrHexString(metadata); return metadata.decodeText(); // return (pdfDoc['getInfoDict']() as PDFDict).get(PDFName.of('Metadata')); } static assertIsLiteralOrHexString(pdfObject) { if (!(pdfObject instanceof pdf_lib_1.PDFHexString) && !(pdfObject instanceof pdf_lib_1.PDFString)) { throw new Error('values is neither '); } } } exports.PdfUtilities = PdfUtilities; //# sourceMappingURL=pdf.utilities.js.map