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

6

18

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.

Bubbler answered 18/2, 2014 at 20:16 Comment(0)
O
36

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.

Ornas answered 19/2, 2014 at 8:18 Comment(4)
Thanks, @Nenotlep, for your attention. Taking it from your example, (and sorry for branching off into php), is it possible to place this kind of pipe command AS IS into a php function such as shell_exec or system? You understand that I am not actually programming directly for the command line ;)Bubbler
I would think it is possible, but I have never done it. What the pipe does is capture the the output stream of the first command and feed it to the input stream of the second command, so you might have to control the streams yourself. Now I've never done this with PHP, but there's a nice relating commend on the PHP website: fi2.php.net/shell_exec#67183 - although it is very old. Sorry, but I don't have an environment to test it myself at the moment :(.Ornas
Even I was looking out for same thing, very helpful answer. But do you know why it inserts "magical ponies" (with double quotes) in pdf ? How to fix that ? I executed echo "<h3>magical ponies</h3>" | wkhtmltopdf.exe - test.pdfThornburg
To remove the quotes you can do echo | set /p="<h3>magical ponies</h3>" | wkhtmltopdf.exe - test.pdfDisentangle
M
3

In addition to the answer provided by pp. If you prefer not to escape the < > characters, you can also do the following:

echo | set /p="<h3>Magical ponies</h3>" | wkhtmltopdf - test.pdf
Moua answered 6/3, 2017 at 0:26 Comment(0)
N
2

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.)

Nellanellda answered 2/11, 2016 at 13:41 Comment(0)
B
2

Using PowerShell, you can do it like this:

$html = "<h1>Magical Ponies</h1><p>Once upon a time in Horseland, there was a band of miniat
ure creatures..."
$html | .\wkhtmltopdf.exe - C:\temp\test.pdf

Just make sure you're running the code from within the \bin\ directory of wkhtmltopdf, otherwise, you'd have to provide a full path to the executable.

Bubbler answered 9/3, 2017 at 8:7 Comment(0)
A
0

I couldn't get wkhtmltopdf to work on my end converting raw html to PDF, but I did find a simple microservice that was able to do it with a couple minutes work on bantam.io.

Here's how it works:

bantam
 .run('@images/html', {
   html: `
   <h1 style='width: 400px; text-align: center'>TEST</h1>
   <br/>
   <img src='https://images.unsplash.com/photo-1507146426996-ef05306b995a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1950&q=80' />
   `,
   imageType: 'pdf',
 })
 .then(pdfUrl => {
    // pdf is temporarily hosted on AWS S3 via a secure link at `pdfUrl`
});

They also have options for taking in a URL rather than raw HTML and you can produce images as well as PDFs. Here are the docs: https://bantam.io/functions/@images/html?link=docs&subLink=0

Athelstan answered 10/5, 2019 at 23:24 Comment(0)
C
0

The question has been answered, but I'm leaving this here since it solved my problem too:

echo "<p>hello world</p>" | wkhtmltopdf - - > [output here, could be a file (like "c.pdf"), or something else]

This command exemplifies how to use wkhtmltopdf by just piping content in and out, without needing to save the input or the output to a file (you can also pipe it to a file if you want).

It was useful to me because I was making a web server that takes html text as input and returns the pdf's content, without saving it to a file.

The complete example command (should successfully create a file called c.pdf, with "hello world" written on it):

echo "<p>hello world</p>" | wkhtmltopdf - - > c.pdf

Calore answered 10/4 at 1:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.