Convert png image to pdf in nodejs canvas

SHIN picture SHIN · Nov 26, 2013 · Viewed 8.4k times · Source

I need to create a pdf from canvas. can any body help me ?

this is what i tried

var http = require('http'), fs = require('fs'),
Canvas = require('canvas');
var ht = require('https');
http.createServer(function (req, res) {
 dr();
function dr()
{

    fs.readFile(__dirname + '/temp.jpg', function(err, data) {
        if (err) throw err;
        var img = new Canvas.Image; // Create a new Image
        img.src = data;

        var canvas = new Canvas(img.width, img.height,'pdf');
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, img.width / 4, img.height / 4);
        res.writeHead(200, {'content-type' : 'application/pdf'}); 
        canvas.toDataURL('application/pdf', function(err, str){

        res.write( str );

        res.end();

        });

    });

}

}).listen(8124);

Answer

SHIN picture SHIN · Nov 26, 2013

This code working for me.

var http = require('http'), fs = require('fs'),
Canvas = require('canvas');
var ht = require('https');
http.createServer(function (req, res) {
 dr();
function dr()
{

    fs.readFile(__dirname + '/temp.jpg', function(err, data) {
        if (err) throw err;
        var img = new Canvas.Image; // Create a new Image
        img.src = data;

        var canvas = new Canvas(img.width, img.height, 'pdf');
        var ctx = canvas.getContext('2d');
        ctx.drawImage(img, 0, 0, img.width / 4, img.height / 4);

        res.writeHead(200, {'content-type' : 'application/pdf'});
    res.write( canvas.toBuffer() );
    res.end();  
    });

}

}).listen(8124);