最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - How to run custom Javascript on Wkhtmltopdfwkhtmltoimage? - Stack Overflow

programmeradmin2浏览0评论

I want to run a javascript on a website, but when I do it, nothing changes, I get the pdf as if I run no result of the javascript on it, this is an example of what I do:

xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltoimage --run-script "javascript:(\$(function(){ \$("div").hide()   ;}))" google google.png

I have also scripts without jquery like:

xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf --run-script "javascript:(function(){ document.write("AHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"); ;})" google p2.pdf

But as I said, no effect on the pdf or the png (because I have tried both wkhtmltoimage and wkhtmltopdf).

I want to run a javascript on a website, but when I do it, nothing changes, I get the pdf as if I run no result of the javascript on it, this is an example of what I do:

xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltoimage --run-script "javascript:(\$(function(){ \$("div").hide()   ;}))" google.com google.png

I have also scripts without jquery like:

xvfb-run --server-args="-screen 0, 1024x768x24" wkhtmltopdf --run-script "javascript:(function(){ document.write("AHHHHHHHHHHHHHHHHHHHHHHHHHHHHH"); ;})" google.com p2.pdf

But as I said, no effect on the pdf or the png (because I have tried both wkhtmltoimage and wkhtmltopdf).

Share Improve this question edited Apr 10, 2014 at 9:31 Farid Nouri Neshat 30.4k6 gold badges77 silver badges125 bronze badges asked Apr 9, 2014 at 12:48 aDoNaDoN 1,9514 gold badges41 silver badges62 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 10

Your command uses quotes incorrectly; try this:

--run-script "javascript:(\$(function(){ \$('div').hide()   ;}))"

Also, try increasing the JavaScript delay:

--javascript-delay 1000

http://wkhtmltopdf.org/usage/wkhtmltopdf.txt

About the question

"do you know any way of generate a .png with less size? cause every png I generate weights 9MB"

I had the same problem with large PNG files generated with wkhtmltoimage and managed to fix this with adding the flag --quality 0, that shrunk the image from 3.5MB to just 38KB. Note that PNG is doing lossless compression so the image quality won't change only will be shrunk in size. Be careful when using --quality flag when you output to jpg format as this can blur the image is you set it too low.

发布评论

评论列表(0)

  1. 暂无评论