2

I tried to export pdf files from webpage with wkhtmltopdf 0.10.0 rc2. However I found a lot of problems in it:

1, When export Bootstrap CSS library based pages, it always shows the link address inside the 'a' tags. For example, "linkname" will rendered with './somepage.html' displayed after 'linkname'. Can I remove it?

2, Some element of the page is missing. Take stackoverflow pages as example, running

wkhtmltopdf http://stackoverflow.com/questions/10307043/rails-3-and-pdfkit-how-to-specify-page-size/ ./test.pdf 

will render most elements in the page. But the 5 buttons after the "stackoverflow" logo is gone. How can I avoid that?

3, How can I export a page that requires login?

Among these 3 questions I'm most curious about the first one. I googled a lot but no result. So I'll accept the first answer to that. Big Thanks in advance.

Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
tuo
  • 586
  • 1
  • 4
  • 20

2 Answers2

6

To answer the first question; the link location is appended to the link text because it's using the following css rule (or some similar variation):

a:link:after, a:visited:after
{    
  content: " (" attr(href) ") ";
}

So to stop this from happening just add the following to your page:

@media print
{

  a:link:after, a:visited:after
  {
    content: "";
  }

}
Michael
  • 11,912
  • 6
  • 49
  • 64
  • For those suffering from the same symptoms, this answer solved it like a charm. it's awkward why the OP didn't mark this. Thank you @MichaelRushton – Amjo Jul 09 '19 at 09:47
  • Thank you! Saved us a lot of time sharing this useful info – mitcheljh Mar 02 '23 at 20:18
0

Since you are really just looking for an answer to question 1, I wanted to add a comment but it got too long. Usually it's 1 question per question here on SO :)

1: Do you have a link to something online where this happens for replication? I just tried http://twitter.github.io/bootstrap/getting-started.html#examples and it worked (horribly ugly, but still).

2: Are you on linux? For me on Win 7 it worked fine. Do you mean the "Questions", "Tags", "Users" etc buttons? I can't be assed to see how they work, but maybe try with --javascript-delay 5000 if they happen to be JS-generated.

3: Depends. You might want to use --cookie-jar for authentication but I personally dislike it. If your event handler for the print command is in your app, you could try to use the currently logged in credentials to vomit a tempfile or a memorystream of the page within the app and then feed that directly to wkhtmltopdf. That is unless your app needs wkhtmltopdf to appear logged in, in which case you need the cookie jar or individual cookies. The cookie(s) would contain the same login data that your logged in user has, so it's kind of like session hijacking which means that if you have any security checks, it might not even work. See http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf_0.10.0_rc2-doc.html for cookie info. More info on cookie jar: https://code.google.com/p/wkhtmltopdf/issues/detail?id=356 Also, there's a related SO question for this that might help you out: wkhtmltopdf Log in to asp.net web app

Community
  • 1
  • 1
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100