How to render image as pdf (canvas and pdfkit)

15.4k views Asked by At

I have a function on nodejs that generates an image from many images and then generate a pdf file from that. Im trying with just one image but i need to add more, but this doesnt seems to work

function HelperHandler() {
  this.pdf = function(req, res, next) {
    var doc = new PDFDocument;
    mergeImages(function(err, image) {
      if (err)
        return res.json(err);

      doc.image(image, 100, 100);
      doc.output(function(string) {
        res.contentType = "application/pdf";
        res.send(string);
      });
    })
  }
}

var mergeImages = function(callback) {
  var Canvas = require("canvas")
    , fs     = require("fs");

  fs.readFile(global.root_path + "/images/bg.jpg", function(err, data) {
    if (err)
      callback("error loading image");
    else {
      var canvas = new Canvas(408, 939)
        , img    = new Canvas.Image(data);
      ctx = canvas.getContext("2d");
      img.onload = function() {
        ctx.drawImage(img, 0, 0, 408, 939);
      }
      canvas.toDataURL('image/png', function(err, str) {
        callback(null, str);
      });
    }
  });
}

Error

Error: ENAMETOOLONG, name too long 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZgAAAOrCAYAAABqSpB/AAAABmJLR0QA/wD/AP+gvaeTAAAF5UlEQVR4nO3BMQEAAADCoPVPbQo/oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICjAWckAAHF4EUcAAAAAElFTkSuQmCC'
    at Object.openSync (fs.js:427:18)
    at Object.readFileSync (fs.js:284:15)
    at Function.open (/Users/jtomasrl/code/app/server/node_modules/pdfkit/js/image.js:27:28)
    at PDFDocument.image (/Users/jtomasrl/code/app/server/node_modules/pdfkit/js/mixins/images.js:27:26)
    at /Users/jtomasrl/code/app/server/lib/handler/current/helper.js:15:11
    at /Users/jtomasrl/code/app/server/lib/handler/current/helper.js:41:9
    at /Users/jtomasrl/code/app/server/node_modules/canvas/lib/canvas.js:217:7
2

There are 2 answers

0
Nobody On

Here's the complete code:

const PDFDocument = require('pdfkit');
const fs = require('fs');

// Create a new PDF document
const doc = new PDFDocument();

// Pipe its output to a file
doc.pipe(fs.createWriteStream('output.pdf'));

// Add an image to the PDF. The image data is base64, so it needs to be converted to a Buffer first.
const base64Image = '...'; // your base64 image data here
const imageBuffer = Buffer.from(base64Image, 'base64');
doc.image(imageBuffer, 0, 0, {fit: [250, 300]});

// Finalize the PDF and end the stream
doc.end();

More about images here: https://pdfkit.org/docs/images.html

Hope it helps.

0
Pascoual On

You can use a buffer or a path with PDFKit image.

But you can't use a base64 URL, you need to decode this string to a buffer.

To use base64 data:

doc.image(new Buffer(image.replace('data:image/png;base64,',''), 'base64'), 100, 100); // this will decode your base64 to a new buffer

More information on base64 de/encode with Node Buffer here.