在nodejs画布中将png图像转换为pdf [英] Convert png image to pdf in nodejs canvas
本文介绍了在nodejs画布中将png图像转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从画布创建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; //创建一个新的图像
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 ('帆布');
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; //创建一个新的图像
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();
});
}
$ b b})。listen(8124);
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);
解决方案
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);
这篇关于在nodejs画布中将png图像转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文