我试图将一个Markdown文件转换成PDF。我只想要以下两点:
- 一种可以轻松更改pdf样式的方法(例如使用CSS文件)
- 代码块的语法高亮
我可以使用哪些工具来实现这个目标?我尝试过Pandoc,但它使用Latex格式,不易于使用。
我试图将一个Markdown文件转换成PDF。我只想要以下两点:
我可以使用哪些工具来实现这个目标?我尝试过Pandoc,但它使用Latex格式,不易于使用。
Pandoc可以将您的Markdown转换为HTML,但样式/布局是一个不同的主题。如果您想生成PDF但使用CSS进行样式设置,则需要使用能够解释CSS的工具。这要么使用浏览器并打印到PDF,支付Prince或尝试wkhtmltopdf(另请参见print-css.rocks)。顺便说一句,现在Pandoc也可以使用wkhtmltopdf
:
pandoc -t html --css mystyles.css input.md -o output.pdf
pandoc
和CSS样式设置在命令行中将markdown .md文档转换为PDF通过正确的设置,pandoc
可以做得很好,但是它仍然缺少我真正希望它具有的代码块下面的灰色背景:(。在@mb21's answer的启发下,这是我为GitHub Flavored Markdown(gfm)想出的相当不错的pandoc
命令。
在Ubuntu 20.04上测试通过:
# Install pandoc and dependencies
sudo apt update
sudo apt install pandoc
sudo apt install wkhtmltopdf # a dependency to convert HTML To pdf
# Download the github.css CSS style theme
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 插件 的一部分,我在 我的另一个答案中 对此进行了介绍。
上述命令的 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
示例的 markdown 文件,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://istack.dev59.com/RJj4x.webp">
</p>
**Align center:**
<p align="center" width="100%">
<img width="33%" src="https://istack.dev59.com/RJj4x.webp">
</p>
**Align right:**
<p align="right" width="100%">
<img width="33%" src="https://istack.dev59.com/RJj4x.webp">
</p>
```
Produces this:
**Align left:**
<p align="left" width="100%">
<img width="33%" src="https://istack.dev59.com/RJj4x.webp">
</p>
**Align center:**
<p align="center" width="100%">
<img width="33%" src="https://istack.dev59.com/RJj4x.webp">
</p>
**Align right:**
<p align="right" width="100%">
<img width="33%" src="https://istack.dev59.com/RJj4x.webp">
</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://istack.dev59.com/RJj4x.webp">
</p>
```
上面的 Pandoc 转换命令:
pandoc -f gfm -t html5 --metadata pagetitle="test.md" --css github.css \
test.md -o test.pdf
输出PDF截图:
与Markdown Viewer相比还不够好,因为它仍然缺少代码块下方的灰色背景(看看我的另一个答案中的效果),但它看起来相当不错!
有一个非常好用且简单的工具可以浏览Markdown文档,并支持导出PDF功能:
GFMS - Github Flavored Markdown Server
它是一个简单而轻巧(无需配置)的HTTP服务器,您可以在任何包含Markdown文件的目录中启动它来进行浏览。
特点如下:
gfms -p 8888
wget "http://localhost:8888/file.md?pdf" -O file.pdf
wkhtmltopdf
并使用它来安装 gh-md-to-html。pip3 install gh-md-to-html[pdf_export]
然后使用
gh-md-to-html path_to_your_file.md -p <name>.pdf -c path_to_your_css.html
-p
选项声明要将生成的pdf文件保存为哪个文件名;"<name>
"会自动替换为您输入文件的名称。
- -c
选项是指向包含在<style>
标签内的css的html
文件的路径,该css将被嵌入到生成的html文件中,然后再将该文件转换为pdf。gh-md-to-html
将文件转换为html,然后再使用wkhtmltopdf
将其转换为pdf,正如其名称所示。-s false
给该命令,这将禁用默认样式。但是,代码块在两种情况下都会正确地进行语法高亮。pip3 install gh-md-to-html[offline_conversion]
,然后使用-o OFFLINE
选项运行gh-md-to-html
。