Detect if page is running through wkhtmltopdf

688 views
Skip to first unread message

Joshua Tan

unread,
May 5, 2014, 7:35:36 PM5/5/14
to wkhtmltop...@googlegroups.com
Hello,

I have a script that runs customization on a page. It targets 2 scenarios: for regular browsing and during pdf creation. I solved this problem by checking on the window.location.href property.

...
mode: /^http/.test(window.location.href) ? 'normal' : 'wkhtmltopdf'
...

During pdf creation, an HTML file is generated on the server side, passed to wkhtmltopdf, then deleted. And since wkhtmltopdf reads it locally, it will have file:///xxxxx as window.location.href

However, I'm not really satisfied with this. I prefer a more robust solution. So I discovered --custom-header but for the life of me I can't seem to get it working.

I logged the results with this script

for (var prop in navigator) {
    console.log(prop + ":" + navigator[prop]);
}

ran the wkhtmltopdf (0.12.0) with these parameters:

wkhtmltopdf --debug-javascript --custom-header 'User-Agent' 'meow' --custom-header-propagation meow.html meow.pdf


and none of the object properties shows 'meow' in it.

Is this a bug or am I looking into the wrong object?

Diego Medina

unread,
Jun 10, 2014, 11:50:53 PM6/10/14
to wkhtmltop...@googlegroups.com
are you making the request to an actual url or fetching the page from the filesystem?

I use custom headers and they work fine, my full syntax is:

/opt/wkhtmltopdf/bin/wkhtmltopdf     --custom-header-propagation --custom-header Cookie "EXTSESSID=some-session-id; JSESSIONID=j-session-id-here" https://server.com/page report.pdf

and the cookie value and jsessionid is sent to the server

hope that helps
Reply all
Reply to author
Forward
0 new messages