2

I would like to convert several webpages into a pdf file, by:

wkhtmltopdf http://think-like-a-git.net/sections/about-this-site.html http://think-like-a-git.net/sections/about-this-site/who-this-site-is-for.html all.pdf

If I put the two input arguments into a text file called "links":

http://think-like-a-git.net/sections/about-this-site.html 
http://think-like-a-git.net/sections/about-this-site/who-this-site-is-for.html

how shall I specify the input arguments in terms of the file? The following doesn't work:

$ wkhtmltopdf "$(cat links)" all.pdf
Loading pages (1/6)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
Exit with code 1 due to network error: ContentNotFoundError

2 Answers 2

4

When you used double quotes in "$(cat links)", the shell treated the whole content of file as one string, not separated fields (with each field is one line in file).

You can do something like this:

set -f  # Turn off globbing
IFS='   # Split on newline only
'
wkhtmltopdf $(cat links) all.pdf
6
  • Thanks. Why "Turn off globbing"? Commented Dec 16, 2015 at 2:59
  • Not download all the webpages, when I include all the links on the right column in think-like-a-git.net in the links file. Commented Dec 16, 2015 at 3:02
  • @Tim: An url string can contain [, ], * or ?, which are shell metacharacters to perform globbing. Commented Dec 16, 2015 at 3:09
  • I put the links in a text file like this paste.ubuntu.com/14045177. the command only convert the first or two webpages, and not the other webpages. Commented Dec 16, 2015 at 3:15
  • @Tim: I'm not familiar with wkhtmltopdf, maybe you should run with some links first to make sure it works, wkhtmltopdf link1 link2 link3 link4 all.pdf. Commented Dec 16, 2015 at 3:21
2

You could:

readarray -t a < file
wkhtmltopdf "${a[@]}" all.pdf

  • readarray reads the file into an array line by line, -t removes the trailing newline.
  • "${a[@]}" refers to all array elements. This generates a command in the form:
wkhtmltopdf "${a[0]}" "${a[1]}" "${a[2]}" "..." all.pdf

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.