Creating PDF from WIX content using Velo

@davebogan98 Hey Dave! Sure. This was the code I used below in a file called pdf.js. The function createBuffer was exported to be used in another backend js file as **import** PDF **from** 'backend/document-tools/format/pdf.js' ;

Then for usage, it was used using await PDF . get ( jobId );

Note btw, this is not a generic example, so you’ll need to refer to the Pdfmake documentation to tweak it for your use case.

// backend/document-tools/format/pdf.js
import PdfPrinter from 'pdfmake';

const fonts = {
    Courier: {
        normal: 'Courier',
        bold: 'Courier-Bold',
        italics: 'Courier-Oblique',
        bolditalics: 'Courier-BoldOblique'
    },
    Helvetica: {
        normal: 'Helvetica',
        bold: 'Helvetica-Bold',
        italics: 'Helvetica-Oblique',
        bolditalics: 'Helvetica-BoldOblique'
    },
    Times: {
        normal: 'Times-Roman',
        bold: 'Times-Bold',
        italics: 'Times-Italic',
        bolditalics: 'Times-BoldItalic'
    },
    Symbol: {
        normal: 'Symbol'
    },
    ZapfDingbats: {
        normal: 'ZapfDingbats'
    }
};

const printer = new PdfPrinter(fonts);
// var fs = require('fs');

function createBuffer(id, filename, array = []) {
    let docDefinition = {
        defaultStyle: {
            font: 'Helvetica'
        },
        footer: {
            columns: [
                'Generated on myexamplewebsite.com'
            ]
        },
        content: [
            { text: filename, fontSize: 18, bold: true, lineHeight: 2 },
            ""
        ]
    };

    if (array) {
        // docDefinition.content = docDefinition.content.concat(array)
        docDefinition.content = docDefinition.content.concat(array.map(item => ({ text: item.text })));
    }

    let pdfDoc = printer.createPdfKitDocument(docDefinition);
    return new Promise((resolve, reject) => {
        try {
            var chunks = [];
            pdfDoc.on('data', chunk => chunks.push(chunk));
            pdfDoc.on('end', () => resolve(Buffer.concat(chunks)));
            pdfDoc.end();
        } catch (err) {
            reject(err);
        }
    });
}


export default {
    get: async ({ id, name, captions_array }) => {
        return await createBuffer(id, name, captions_array);
    }
}