Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any wkhtmltopdf option to convert html text rather than file?

I recently stumbled on wkhtmltopdf and have found it to be an excellent tool for on-the-fly conversion from html to pdf in the browser.

A typical usage (in Windows) would go:

wkhtmltopdf.exe --some-option "<div>Some html <b>formatted</b> text</div>" www.host.com/page_to_print.html file.pdf

My question is: Is there an option to use <html><head></head><body><h1>This is a header</h1></body></html> in place of www.host.com/page_to_print.html?

Thanks for any help.

like image 531
Ifedi Okonkwo Avatar asked Feb 18 '14 20:02

Ifedi Okonkwo


People also ask

What is the use of Wkhtmltopdf?

Wkhtmltopdf is an open source simple and much effective command-line shell utility that enables user to convert any given HTML (Web Page) to PDF document or an image (jpg, png, etc).

How do I configure Wkhtmltopdf?

INSTRUCTIONS: Download an appropriate version of wkHTMLtoPDF library from http://wkhtmltopdf.org. If you are on Windows operating system then do install it under C:\ drive (for example c:\wkhtmltopdf). On Linux/UNIX, you can install it under /usr/local/bin and make sure wkhtmltopdf has execute permissions.


2 Answers

You can pipe content into wkhtmltopdf using the command line. For Windows, try this:

echo "<h3>blep</h3>" | wkhtmltopdf.exe - test.pdf

This reads like "echo <h3>blep</h3>, output it's stdout (standard out stream) to wkhtmltopdf stdin (standard in stream)".

The dash - in the wkhtmltopdf command means that it takes it's input from stdin and not a file.

You could also echo HTML into a file, feed that file to wkhtmltopdf and delete that file inside a script.

like image 66
Joel Peltonen Avatar answered Sep 21 '22 08:09

Joel Peltonen


Just a correction to the answer provided by Nenotlep. As Jigar noted (in a comment to Nenotlep's answer), Nenotlep's command results in quotation marks preceding and following the actual text. On my system (Windows 10) this command is the correct solution:

echo ^<h3^>magical ponies^</h3^> | "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" - test.pdf

The echo command needs no quotation marks - but, if you do not put the text between quotation marks, the < and > characters need to be escaped (by ^).

Another way to try out is writing the text into a temporary file, which - on Windows - might even be faster as some sources state:

echo ^<h3^>magical ponies^</h3^> > temp.txt
"C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" - test.pdf < temp.txt

(This can also be written in one line: just put an & between the two commands.)

like image 37
booFar Avatar answered Sep 19 '22 08:09

booFar