当前位置 : 主页 > 网页制作 > Nodejs >

node.js – 在nodejs canvas中将png图像转换为pdf

来源:互联网 收集:自由互联 发布时间:2021-06-16
我需要从画布创建一个pdf.有谁能够帮我 ? 这是我试过的 var http = require('http'), fs = require('fs'),Canvas = require('canvas');var ht = require('https');http.createServer(function (req, res) { dr();function dr(){ fs.r
我需要从画布创建一个pdf.有谁能够帮我 ?

这是我试过的

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);
这段代码对我有用.

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);
网友评论