ブラウザでブログとか見てると, PDFにしてKindleで読みたい! ってことがあるわけです。
手作業でチマチマ印刷するのもタルいなー, と思っていたら, 面白いのを見付けました。Firefoxで, コマンドラインから印刷できるようにする拡張「commandlineprint2」
そこで, Webで公開されてる「Real World Haskell」をKindle向けPDFにできないものかと挑戦してみました。(※Ubuntu環境です。Windows/Macでできるか不明)
手順の流れはこんな感じです。
- 普段のFirefox環境と設定を別にするために, Firefoxのプロファイルマネージャーを起動して新しいプロファイルを作ります。
- Firefoxの「Stylish」拡張で, ページの余計な余白は装飾を削ぎ落します。
- user.jsを編集して, 余白や紙のサイズを調整します。
- 「commandlineprint2」インストールしてバッチ処理的にコマンドラインから印刷します。
firefox -P CmdlinePrint -printprinter kindle -print http://xxx
1. 新しいプロファイルの作成
普段のFirefox環境と設定を別にするためにFirefoxのプロファイルマネージャーを起動して新しいプロファイルを作ります。
firefox -ProfileManager
ここでは "CmdlinePrint" という名前のプロファイルを作ることにしましょう。
2. スタイルシートの調整
Firefoxの「Stylish」拡張で, ページの余計な余白は装飾を削ぎ落します。
「commandlineprint2」インストールします。印刷したいページ/サイトに合せて作る必要があるので多少面倒かも知れません。
「commandlineprint2」インストールします。印刷したいページ/サイトに合せて作る必要があるので多少面倒かも知れません。
例えばこんな感じ:
@namespace url(http://www.w3.org/1999/xhtml);@-moz-document domain("book.realworldhaskell.org") {body {margin: 0em !important;padding: 0em !important;}.navheader, .book, .preface, .chapter, .appendix, .bibliography,.navfooter, .basetemplate, div.preface {margin: 0em!important;padding: 0em!important;width: 95%!important;}div.toc, div.navheader, div.rwhfooter, div.navfooter,.comment, .commenttoggle, .comment_header, .comment_help{display: none;}div {margin: 0em;padding: 0em;}}
3. user.jsを編集して, 余白や紙のサイズを調整します。
直接 .mozilla/firefox/xxxxxx.CmdlinePrint/user.js を編集します。
print.printer_kindle.xxxx という設定が "kindle" という名前のプリンタに対応するようです。
/* To omit typing "-printmode PDF" etc */user_pref("extensions.cmdlnprint.mode", 1); //PDF//user_pref("extensions.cmdlnprint.mode", 2); //PNG//user_pref("extensions.cmdlnprint.mode", 3); //PostScript/* fake printer "drupal pdf dummy" */user_pref("print.printer_kindle.print_downloadfonts", true);user_pref("print.printer_kindle.print_edge_bottom", 0);user_pref("print.printer_kindle.print_edge_left", 0);user_pref("print.printer_kindle.print_edge_right", 0);user_pref("print.printer_kindle.print_edge_top", 0);user_pref("print.printer_kindle.print_in_color", true);user_pref("print.printer_kindle.print_bgcolor", false);user_pref("print.printer_kindle.print_bgimages", false);/* Note that margins are string, while unwritable margins are integer. */user_pref("print.printer_kindle.print_margin_top", "0");user_pref("print.printer_kindle.print_margin_bottom", "0");user_pref("print.printer_kindle.print_margin_left", "0");user_pref("print.printer_kindle.print_margin_right", "0");user_pref("print.printer_kindle.print_unwritable_margin_top", 0);user_pref("print.printer_kindle.print_unwritable_margin_bottom", 0);user_pref("print.printer_kindle.print_unwritable_margin_left", 0);user_pref("print.printer_kindle.print_unwritable_margin_right", 0);user_pref("print.printer_kindle.print_headercenter", "");user_pref("print.printer_kindle.print_headerleft", "");user_pref("print.printer_kindle.print_headerright", "");user_pref("print.printer_kindle.print_footercenter", "");user_pref("print.printer_kindle.print_footerleft", "");user_pref("print.printer_kindle.print_footerright", "");/* paperWidth is also string, as it stands for float (or double). */user_pref("print.printer_kindle.print_paper_width", "180");user_pref("print.printer_kindle.print_paper_height", "240");user_pref("browser.sessionstore.resume_from_crash", false);
「commandlineprint2」をインストールします。
すると以下のようなコマンドラインで URL の内容を印刷できるようになります。
firefox -P CmdlinePrint -printprinter kindle -print http://xxx
Real World Haskellのページを全部印刷したいなら, チマチマURLをコピペしてコマンド実行してもいいし, wget とか sed とか awk とか perl とか駆使するのも良いでしょう。
for url in `cat rwh-url-list.txt`dofirefox -P CmdlinePrint -printprinter kindle -print $urldone
CreativeCommonsだから, PDF化したのを公開しても良いんだよね??
気が向いたら。
...あ, Kindle版が売ってるじゃん! 買えよ!! っていう突っ込みもありですね。
今後の課題とか
- 出力ファイル名をもっと柔軟に指定したい。
- 日本語もいけるのかなぁ...?(未挑戦)
- kindlegen 使うともっと素敵にできる?? (試した限りだと, コマンドラインのレイアウトがメタメタ...)
- なんでLinux版Firefoxの印刷回りってこんなヘボなのよ!!?? (設定が保存されないとか, GUIで余白設定できないとか)
- PDFがバラバラのファイルだと, Kindleでメンドクサイ。ひとつに纏めて目次を付けたいな。
0 件のコメント:
コメントを投稿