通过正确的设置,pandoc 做得很好,但仍然缺少我非常希望它拥有的代码块下方的灰色背景 :(。在 @mb21's answer 的引导下,这就是我想出了一个相当不错的pandoc GitHub Flavored Markdown (gfm) 命令。
在 Ubuntu 20.04 上测试:
sudo apt update
sudo apt install pandoc
sudo apt install wkhtmltopdf # a dependency to convert HTML To pdf
wget https://raw.githubusercontent.com/simov/markdown-viewer/master/themes/github.css
# Convert test.md to test.pdf using the github.css CSS style theme
pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css \
test.md -o test.pdf
wget 命令是从这里下载 github.css GitHub CSS 格式化主题文件:https://github.com/simov/markdown-viewer/tree/master/themes。它是Markdown Viewer Chrome plugin here 的一部分,我写过in my other answer here。
从上面分解pandoc 命令:
-f gfm # from format = Github Flavored Markdown
-t html5 # to format = html5
--metadata pagetitle="test.md" # html output format (-t html) requires a
# mandatory html title, so just set it to the input file name:
# "test.md"
--css github.css # use the github.css file as the CSS styling file for
# the html output
test.md # this is the INPUT markdown (Github Flavored Markdown) file
-o test.pdf # save the OUTPUT PDF as test.pdf
示例降价文件,test.md:
Snippet from my project here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world/blob/master/markdown/github_readme_center_and_align_images.md
## 1.1. Align images left, right, or centered, with NO WORD WRAP:
This:
```html
**Align left:**
<p align="left" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align center:**
<p align="center" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align right:**
<p align="right" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
```
Produces this:
**Align left:**
<p align="left" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align center:**
<p align="center" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
**Align right:**
<p align="right" width="100%">
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
If you'd like to set the text itself to left, center, or right, you can include the text inside the `<p>` element as well, as regular HTML, like this:
```html
<p align="right" width="100%">
This text is also aligned to the right.<br>
<img width="33%" src="https://i.stack.imgur.com/RJj4x.png">
</p>
```
上面的 Pandoc 转换命令:
pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css \
test.md -o test.pdf
输出PDF截图:
不如Markdown Viewer,因为它仍然缺少代码块下的灰色背景(看看它看起来像in my other answer here),但看起来还不错!
另见:
- [我的回答]SuperUser: How Can I Convert Github-Flavored Markdown To A PDF