7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MacでQiita記事(Markdown)をPDFに変換する方法

Last updated at Posted at 2018-12-09

MacでQiita記事(Markdown)をPDFに変換する方法を紹介します。

前提条件

  • Homebrewがインストール済みであること
  • OSバージョンが「macOS High Sierra バージョン 10.13.16」以上であること

手順

Homebrew最新化

$ brew update
$ brew upgrade

PandocとBasicTexのインストール

$ brew install pandoc
$ brew cask install basictex

環境変数PATHへの設定

$ vim ~/.bash_profile

以下を「.bash_profile」に追記

export PATH=/Library/TeX/texbin:$PATH

tlmgrのアップデートとモジュールの追加

$ sudo tlmgr update --self --all
$ sudo tlmgr install collection-langjapanese collection-luatex collection-latexextra
$ sudo tlmgr install lm-math

Qiita記事をMarkdownに変換

URLの語尾に「.md」を追加
https://qiita.com/hiesiea/items/58c1c9e14938f301689c.md

すると、記事がMarkdownで表示されるので、ファイルとして保存する。

MarkdownをPDFに変換

$ pandoc 58c1c9e14938f301689c.md -o output.pdf -V documentclass=ltjarticle --pdf-engine=lualatex

変換結果

result.png

ね、簡単でしょ?

参考サイト

Qiitaの投稿を手っ取り早くPDF化
MacでPandocを使ってMarkdownをpdfに変換 | note.nkmk.me
MacでPandocを使ってマークダウンをPDFに変換 | ヤマムギ

追記:HTMLからPDFに変換

Qiitaの記事執筆中、画像をアップロードした時に画像部分が以下のようにimgタグで表記されていることがあります。
(imgだったりそうでなかったりで、なぜ表記揺らぎがあるのか不明。。Qiitaさんなんとかしてください泣)

<img width="1552" alt="2.png" src="https://qiita-image-store.s3.amazonaws.com/xxx.png">

この場合、MarkdownからPDFに変換しようすると変換できません。
そこで、HTMLからPDFに変換することを思いつき、ツールを探したところ、「wkhtmltopdf」が一番簡単に実現できました。

wkhtmltopdf https://qiita.com/hiesiea/items/58c1c9e14938f301689c result2.pdf
7
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?