Invalid PDF Structure error message with Nodejs & pdfkit

半腔热情 提交于 2020-01-06 08:09:52

问题


I am trying to generate a pdf using nodejs & pdfkit. This will be a GET webservice which will be hosted in AWS api gateway. I am trying to run it locally first and getting Invalid PDF structure everytime.

Here is my code for it. The pdf file is getting generated properly if I place a local path of my system , but always turns up to be a invalid one if I call from browser or postman. Any help would be highly appreciated

'use strict';

const Actions = require('libs-api').Actions;
const config = require('../libs/config').config();
const PDFDocument = require('pdfkit');
const fs = require("fs");
const blobStream = require('blob-stream');
module.exports = async (event, context, callback) => {

    try {
        let actions = new Actions(event, context, config);


        console.log("resultId::" + event.pathParameters.resultId);

        const params = {
            TableName: config.tableName,
            Key: { resultId: event.pathParameters.resultId }
        };

        console.log("params ::" + JSON.stringify(params));

        let data = await actions.readDynamoDB(params);
        // console.log("data::" + JSON.stringify(data));
        let doc = await generatePDF(data);

        let response = {
            statusCode: 200,
            headers: {
                'Access-Control-Allow-Origin': process.env.CORS_URL,
                'Content-Type': 'application/pdf',
                'Content-Disposition': 'attachment; filename=test.pdf'
            },
            body: doc.toString('base64'),
            isBase64Encoded: true
        }
        return response;
    } catch (error) {
        console.log(JSON.stringify(error.stack));
        return Actions.processError();
    }
};

const generatePDF = (data) => {
    console.log("data in generatePDF");

    let doc = new PDFDocument({ margin: 50 });

    doc.text(`Qualification id: ${data.qualificationId}`, 50, 200)
        .text(`Assesment Method: ${data.methodOfAssessment.method}`, 50, 215)
        .text(`Unit Local Code: ${data.unitLocalCode}`, 50, 130)
        .moveDown();

    doc.end();

    return doc;

}

来源:https://stackoverflow.com/questions/57256887/invalid-pdf-structure-error-message-with-nodejs-pdfkit

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