traviscooper / node-wkhtml Goto Github PK
View Code? Open in Web Editor NEWConvert html to PDF or PNG format using the webkit rendering engine, and qt.
Convert html to PDF or PNG format using the webkit rendering engine, and qt.
Hi, I'm trying to get the following sample code running
var http = require('http'),
wkhtml = require('node-wkhtml');
http.createServer(function (request, response) {
var pdf = wkhtml.spawn('pdf');
pdf.stdout.pipe(response);
pdf.stdin.end('<h1>Hello World</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>', 'utf-8');
}).listen(8000);
But I keep getting the following error:
Uncaught exception : Error: write EPIPE
at errnoException (net.js:769:11)
at Object.afterWrite (net.js:593:19)
I'm running it on Node v0.8.11.
Anyone else having troubles with piping to the response?
Tim.
Specify a license to release this code under, otherwise it's copyrighted by default. I recommend the MIT License:
https://tldrlegal.com/license/mit-license
or Apache 2
https://tldrlegal.com/license/apache-license-2.0-(apache-2.0)
I am new to node, can you tell me how I can generate an image using same code.
As seen at this URL:
http://nodejs.org/docs/v0.4.9/api/child_processes.html#child_process.exec
There are options that can be passed in to the exec() command to increase the maximum buffer size, and to force binary encoding. I was getting this error:
"exec error: Error: maxBuffer exceeded."
with this command:
var Image = require("node-wkhtml").image();
var url = "http://jqueryui.com/";
new Image({ url: url }).convert(function(error, stdout){
console.log("done");
});
To solve the error I edited the PDF.prototype.convert function found in "node-wkhtml/lib/image.js" to pass in a larger maxBuffer argument like this:
PDF.prototype.convert = function(callback) {
exec( util.buildCommand(this),
{maxBuffer: 10*1024*1024},
callback);
}
Although this got rid of the error message, when I saved the resultant data into a database and tried to read it out, it was not parsable as an image file (although there was definitely the correct amount of data saved). To solve this final problem, I told exec to use binary encoding like this:
PDF.prototype.convert = function(callback) {
exec( util.buildCommand(this),
{encoding: 'binary', maxBuffer: 10*1024*1024},
callback);
}
I think it makes sense that the encoding should always be set to binary in this case. As for the maxBuffer, I'm not sure if setting a large number has any effect beyond the potential for an extremely large image to hurt the system. It might be good if this was an argument a user could pass in through the convert() function since they should have a better idea of their size requirements.
Hi Mike, I'd be happy to take this repo, so that we can have it around for legacy purposes.
currently , PDF.add works only on url and file type, but it doesn't work on html type:
pdf = new PDF({
html:"html content 1"
});
pdf.add({
html:"html content 2"
});
it will generate:
echo 'html content 2' | wkhtmltopdf - -
then throw an error: Command failed: Loading pages (1/6).
I'm not very familiar on stdin :(
in util.js at line 23
it should be :
command = command.concat(object.file || object.url);
but not
command = command.concat(object.filename || object.url);
I tried to create a pfd vis a html string such as
hell world!
but it doesn't work.
the command line generated is
echoHello World
| wkhtmltopdf - -
Hi. I'm attempting to write converted pdf from stdout to browser window with the following code. Should you be able to call
response.write(stdout) as I'm doing below, or could I be missing something? At the moment, this opens a blank pdf or occasionally the browser crashes when trying to load it
var url = require("url");
function pdfExport(response, request) {
var urlObj = url.parse(request.url, true);
var fileName = urlObj.query["filename"];
var gotoUrl = urlObj.query["url"];
console.log("Request handler 'pdfExport' was called.");
var PDF = require("node-wkhtml").pdf();
new PDF({url:'www.google.com'}).convert(function(err, stdout) {
if (err) {
console.log(err);
} else {
response.writeHead(200, {"Content-Type": "application/pdf"});
response.write(stdout);
console.log("PDF Complete!");
}
});
}
exports.pdfExport = pdfExport;
It would be nice to get only the generated pdf back in a buffer. Currently you return the stdin which contains also the executed command which leads to unusable pdfs when storing the data direkt in a buffer.
In wkhtmltopdf 0.12 the command is "wkhtmltoimage"
In the code index.js line 33
executable = 'wkhtmltoimg'
Could you provide an example that passes in some command-line options/arguments to wkhtml. Maybe I'm just dense, but I'm trying to set the page size (among other options) and this seems the obvious way to do so... but the result is nothing gets returned from wkhtml:
var pdf = wkhtml.spawn('pdf', undefined, {"page-size: "Letter"});
pdf.stdout.pipe(createWriteStream(__dirname + "/pdfFromString.pdf"));
pdf.stdin.end('<h1>Hello World</h1><p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>', 'utf-8');
(and yes, I'm using Streams)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.