PDFKit and Node JS; when creating PDF document How to send it from the server to the client?

0

my problem is as follows; I have a web application created with NodeJS that shows a form where users add data that will include a PDF document created with PDFKit, by clicking on the submit button the form is created and the document remains on the side from the server, the code is as follows:

var express = require('express');
var router = express.Router();

router.post('/pagar',function(req,res){
	var nombre = req.body.nombre;
    var apellidoPaterno = req.body.apellidoPaterno;
    var apellidoMaterno = req.body.apellidoMaterno;
    var fs = require('fs');
    var PDFDocument = require('pdfkit');
    
    var pdf = new PDFDocument({
        //size: 'LEGAL', 
        layout: 'landscape',
        size: [210, 210], 
        margin: 5,     
        info: {    
             Title: 'Recibo de agua potable',
             Author: 'Comite de agua potable 2018',
        }  
    });

    // Write stuff into PDF
    pdf.moveDown()
         .fillColor('black')
         .fontSize(7)
         .text('EJEMPLO DE DOCUMENTO PDF', {
           align: 'center',
           indent: 2,
           height: 2,
           ellipsis: true
         });
         

    pdf.moveDown()
         .fillColor('black')
         .fontSize(7)
         .text('NOMBRE DE PERSONAS DESDE FORMULARIO', {
           align: 'center',
           indent: 2,
           height: 2,
           ellipsis: true
         });
          


     pdf.moveDown()
         .fillColor('black')
         .fontSize(8)
         .text('NOMBRE: '+nombre+' '+ apellidoPaterno +' '+ apellidoMaterno , {
           align: 'left',
           indent: 2,
           height: 2,
           ellipsis: true
         });


      // Stream contents to a file
     pdf.pipe(fs.createWriteStream(nombre+"_"+apellidoPaterno+"_"+apellidoMaterno+'.pdf')).on('finish', function () {
        console.log('Archivo creado satisfactoriamente ....');
     });
 
     pdf.end();

});

module.exports = router;

However, I would like the PDF document to be sent to the client so that it can be printed since when entering the web application (from a client machine) and adding the data to the form it is created but on the server side.

I wish I could help.

    
asked by keops 28.08.2018 в 05:49
source

0 answers