3

I have an node.js / express.js app where a route creates a pdf file by using pdfkit. This works and the pdf file is written in my filesystem as a file. However, the flow I need would be without the file being written in the filesystem, but instead a base64 string which can be sent to the client.

The following approach however creates me a valid pdf file but the base64 string is always empty. How can this be done?

const PDFDocument = require('pdfkit');
// Create a document
const doc = new PDFDocument;

// Add an image, constrain it to a given size, and center vertically and horizontally

doc.pipe( fs.createWriteStream('out.pdf') );

// Add another page
 doc.addPage()
 .fontSize(25)
 .text('Here is some vector graphics...', 100, 100);

 // Draw a triangle
 doc.save()
 .moveTo(100, 150)
 .lineTo(100, 250)
 .lineTo(200, 250)
 .fill("#FF3300");

 // Apply some transforms and render an SVG path with the 'even-odd' fill rule
  doc.scale(0.6)
  .translate(470, -380)
  .path('M 250,75 L 323,301 131,161 369,161 177,301 z')
  .fill('red', 'even-odd')
  .restore();

// Add some text with annotations
 doc.addPage()
  .fillColor("blue")
  .text('Here is a link!', 100, 100)
  .underline(100, 100, 160, 27, {color: "#0000FF"})
  .link(100, 100, 160, 27, 'http://google.com/');

  // Finalize PDF file
  doc.end();

// Create base64
 function base64Encode(file) {
   var body = fs.readFileSync(file);
  return body.toString('base64');
}


 var base64String = base64Encode('out.pdf');
 console.log(base64String);

// API result object to be sent to client
res.status(200).json({
 status: "success",
 data: base64String, 
 message: "Congrats! "
 });
codebird456
  • 505
  • 8
  • 19

1 Answers1

1

After some playing around, I found a working solution.

// you pdf settings here.
let options = {
  margins: 50
}

// create doc with settings
const doc = new PDFDocument(options);
// create stream and pipe doc
let out = fs.createWriteStream('output.pdf')
doc.pipe(out);

// create the pdf content
doc.addPage()
   .fontSize(25)
   .text('Here is some vector graphics...', 100, 100);

 // finalize document
 doc.end();

 // Stream on finish event handling after document construction ended
 out.on('finish', function() {

  // read pdf file as base64
  fs.readFile('output.pdf', 'base64', function(err, data) {
  if (err) throw err;
  console.log(data);

  // Send base64 pdf to client
    res.status(200).json({
      status: "success",
      data: data,
      message: "Congrats! "
    });
});
});

I use the answer from here (@CHACO) to construct the pdf file client side from its base64-string:

how to display base64 encoded pdf?

codebird456
  • 505
  • 8
  • 19