pdfmake returns blank page for 'fs.createWriteStream'

浪尽此生 提交于 2020-01-05 04:37:27

问题


using node 8.11 and pdfmake 0.1.53 and pm2. pdfmake gives a mixed response, but almost fails(blank page) and success(with content) randomly. Is it temp storage bug? tried with a different name too but getting blank page, after close and open system getting first time proper, next onwards getting empty pages.

note: the direct download does not fail always. attached the sample at last.

sample code:

module.exports = {
    generateReport: function(req, res){ 

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

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

        var docDefinition = {
            content: [
                'First paragraph',
                'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
            ]
        };
        var PdfPrinter = printer.createPdfKitDocument(docDefinition);
        PdfPrinter.pipe(fs.createWriteStream('public/pdfs/document.pdf'));
        PdfPrinter.end();

    }
}

app.js

const express = require('express');
var path = require('path');
var app = express();
app.use('/', express.static(path.join(__dirname, 'public')));
app.use('/api', require('./routes/api'));
app.listen('3003', 'localhost');
console.log('Server started at port 3003');

api.js

const express = require('express');

const ctrl = require('../controllers/ctrl');

var router = express.Router();

router.route('/generate-report').get(ctrl.generateReport);

module.exports = router;

Direct download always getting with content.

sample code for direct download :

module.exports = {
        generateReportAndDownload: function(req, res){

            var fonts = {
                Roboto: {
                    normal: 'node_modules/fonts/Roboto-Regular.ttf',
                    bold: 'node_modules/fonts/Roboto-Medium.ttf',
                    italics: 'node_modules/fonts/Roboto-Italic.ttf',
                    bolditalics: 'node_modeules/fonts/Roboto-MediumItalic.ttf'
                }
            };
            var PdfPrinter = require('pdfmake');
            var printer = new PdfPrinter(fonts);
            var doc = printer.createPdfKitDocument({
                info: {
                    title: 'PDF with External Image',
                    author: 'Matt Hagemann',
                    subject: 'PDF with External Image',
                },
                content: ['First paragraph',
                'Another paragraph, this time a little bit longer to make sure'],
                defaultStyle: {
                    fontSize: 11,
                    font: 'Roboto', // The font name was defined above.
                    lineHeight: 1.2,
                }
            })
            doc.end()
            res.setHeader('Content-type', 'application/pdf')
            res.setHeader('Content-disposition', 'inline; filename="Example.pdf"')
            doc.pipe(res)
        }
}

来源:https://stackoverflow.com/questions/54930985/pdfmake-returns-blank-page-for-fs-createwritestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!