How to render image as pdf (canvas and pdfkit)

jtomasrl picture jtomasrl · Jan 6, 2014 · Viewed 10k times · Source

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/oAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAICjAWckAAHF4EUcAAAAAElFTkSuQmCC'
    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

Answer

Pascoual picture Pascoual · Mar 11, 2014

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.