Problem with the pdfMake?

Asked

Viewed 703 times

0

I’m using the pdfMake library for generating billets, and it was working fine, until I went to follow some examples of the documentation on website and returned me an error, I searched the solution in several issues opened on Github and other sites but none helped me, remembering that I am making the generation of PDF’s on my server Nodejs.

/* O método abaixo é outro jeito de conseguir gerar o documento, e 
   esse método achei nos exemplos no site do pdfMake, e esse método 
   funciona, porém não consigo atribuir as funções de 'download()',
   'open' e 'print()'*/
 pdfMake = printer.createPdfKitDocument(conteudoPDF); //Método funcional

    /* O código abaixo é o exemplo que está na documentação e que está
       retornando o erro "TypeError: pdfMake.createPdf is not a function" */
    pdfMake.createPdf(conteudoPDF).open(); // Método não funcional

    pdfMake.pipe(fs.createWriteStream('../pdfs/testeJson.pdf'));
    pdfMake.end();

2 answers

0

const PdfPrinter = require('pdfmake');
const fs = require('fs');

module.exports = class HelloPdf {

    async generatePdf(body, res) {

        var fonts = {
            Roboto: {
                normal: 'app/assets/fonts/Roboto-Regular.ttf',
                bold: 'app/assets/fonts/Roboto-Medium.ttf',
                italics: 'app/assets/fonts/Roboto-Italic.ttf',
                bolditalics: 'app/assets/fonts/Roboto-MediumItalic.ttf'
            }
        };

        var printer = new PdfPrinter(fonts);

        const title = `${body.title}\n`;

        const documentDefinition = {
            content: [
                { text: title, fontSize: 15, alignment: 'center', bold: true },
            ]
        };

        var pdfDoc = printer.createPdfKitDocument(documentDefinition);
        const pdfName = 'basic.pdf';

        pdfDoc.pipe(fs.createWriteStream(pdfName))
            .on('finish', function () {
                res.setHeader('Content-Type', 'application/pdf');
                res.download(pdfName, pdfName, (err) => { });
            });

        pdfDoc.end();
    }
}

You can check usage examples in the documentation: https://gist.github.com/w33ble/38c5e0220d491148de1c

0


Try to use pdfMake.createPdfKitDocument(conteudoPDF).open(); and check for any errors!

  • Unfortunately returns the same error, I went to look at the documentation and Github of the developer who created pdfMake and it does not commit any of his projects from github since May 18 last year, that is, he abandoned the library and still made it available for use and with bugs, but it is very easy to use and actually creates pdfs through that other method I showed in my code snippet, but does not have the options to print, open and download, I’ll open a new question to see if you can do these 3 options manually, no library use, thank you so much for the help.

  • There is also a cool alternative which is jsPDF, but it only saves the pdf page. You can use it through CDN: <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.4/jspdf.debug.js"></script> to instantiate the jsPDF object. The following is documentation on github: https://github.com/MrRio/jsPDF

  • Already researched about it, really is an interesting tool, but I’m taking data from Mongodb and putting straight into the PDF, I believe that in this case it would not meet my need. Thanks again for your willingness to help, vlw msm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.