Save xkcd to a pdf with captions

curl -sL xkcd.com | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd.pdf
Saves to a PDF with title and alt text of comic. As asked for on http://bbs.archlinux.org/viewtopic.php?id=91100 Change xkcd.com to dynamic.xkcd.com/comics/random for a random comic.

6
2010-03-03 03:41:31

What Others Think

Error: convert: Non-conforming drawing primitive definition `s' @ draw.c/DrawImage/3123. Result: Alt text garbled
oshazard · 450 weeks and 2 days ago
convert: unable to read font `/usr/share/fonts/default/TrueType/arial.ttf'. convert: Non-conforming drawing primitive definition `m'.
SuperFly · 450 weeks and 2 days ago
convert: unable to read font `/usr/share/fonts/default/TrueType/arial.ttf'. if you got this, try this: add -font "helvetica" to the convert command e.g.: ...| convert -font "helvetica" png:- ... this: convert: Non-conforming drawing primitive definition `m'. is still coming up, but you'll see the text in the pdf
nbk1971 · 450 weeks and 1 day ago
nice, but didn't work - same problem as comments above
leovailati · 450 weeks and 1 day ago
I think I've fixed those problems please try it again. Remember it saves to a pdf file so you should have an "xkcd.pdf" in your local directory.
matthewbauer · 450 weeks and 1 day ago
It worked aces for me; I was the first to give it a thumbs up. :P Something I've considered giving a go, but have yet to - converting it to vector and doubling (or tripling) in size before saving to pdf. Also it would be pretty sweet to name each pdf "comicNumber.pdf" rather than just xkcd.pdf. If I could do that, and successfully achieve the vector "zoom" effect, i'd totally make this a daily cron. :)
trench · 450 weeks ago
There is no need for the temp file. You can use convert on a URL. So the major thing might be IMG=`curl -sL xkcd.com| grep '<img [^>]*/><br/>'|awk -F\" '{print $2}'`;convert $IMG -gravity south -draw "text 0,0 \"$IMG\"" pdf:- > xkcd.pdf
houghi · 450 weeks ago
@trench Try this: number=100; for a in $(seq 1 $number); do curl -sL xkcd.com/$a | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > /tmp/$a.pdf; done; gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=xkcd.pdf $(seq 1 $number | sed -r 's/(.*)/\/tmp\/\1.pdf/'); rm /tmp/a /tmp/*.pdf
matthewbauer · 450 weeks ago
@houghi The problem is it gives the URL of the xkcd comic not the alt text @trench Actually this is more what you want: number=100; mkdir xkcd; for a in $(seq 1 $number); do curl -sL xkcd.com/$a | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd/$a.pdf; done
matthewbauer · 450 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: