上篇说到用pandoc转换为reST为pdf是使用LaTeX作为中间格式的,而今天要说的rst2pdf貌似是直接转换为pdf的。

安装和调用

rst2pdf目前只支持Python2.7,因此在创建virtualenv时应使用-p选项指定2.7的python,即

mkvirtualenv -p /path/to/python2. rst
workon rst
pip install rst2pdf

最简单的调用方式是

rst2pdf xxx.rst

然后默认就会生成xxx.pdf文件,当然中文这要是不好使的,因为默认样式是不支持中文字体的,生成的pdf里,中文都是黑块。

中文样式

rst2pdf有一些默认的样式,base、bodytext等等,我们还可以自己新增样式,也可以修改默认的一些样式的选项。对样式的选项修改类似于css,后面的样式选项可以覆盖前面的样式选项,所以我们一般不会去直接修改默认样式,而是在我们自己的样式文件中,覆盖默认的选项,在生成pdf的时候,可以一次性指定多个样式,后面的会覆盖前面的。

对于中文来说,我们就差一个字体的样式了,具体来说就是把下面的代码存储成 chinese.style

{
"embeddedFonts": [
[
"simfang.ttf",
"simhei.ttf",
"simkai.ttf",
"simsun.ttc"
]
],
"fontsAlias": {
"stdFont": "simfang",
"stdBold": "simhei",
"stdItalic": "simkai"
}
}

注:有关字体的配置参见官方文档,这个配置对Linux和Windows应该都是好使的,前提是这些字体文件是真实存在的。Linux字体文件在 /usr/share/fonts 和 ~/.local/share/fonts 文件夹中。

此时,运行以下命令, 中文就可以显示出来了

rst2pdf -s chinese.style xxx.rst

配置文件

如果样式需要每次都指定,对于中文来说就有点不方便,rst2pdf支持一个配置文件,可以使用 --config 的方式传递配置文件路径,默认的配置文件路径是 ~/.rst2pdf/config

以下是官方方式给的配置文件示例+我改了stylesheets,stylesheet_path,language

# This is an example config file. Modify and place in ~/.rst2pdf/config

[general]
# A comma-separated list of custom stylesheets. Example:
# stylesheets="fruity.json,a4paper.json,verasans.json" stylesheets="chinese" # Create a compressed PDF
# Use true/false (lower case) or 1/0
compressed=false # A colon-separated list of folders to search for fonts. Example:
# font_path="/usr/share/fonts:/usr/share/texmf-dist/fonts/" font_path="" # A colon-separated list of folders to search for stylesheets. Example:
# stylesheet_path="~/styles:/usr/share/styles"
stylesheet_path="~/.rst2pdf/styles" # Language to be used for hyphenation support language="zh_CN" # Default page header and footer
header=null
footer=null # What to do if a literal block is too large. Can be
# shrink/truncate/overflow fit_mode="shrink" # How to adjust the background image to the page.
# Can be: "scale" and "center" fit_background_mode="center" # What is the maximum level of heading that starts in a new page.
# 0 means no level starts in a new page. break_level=0 # How section breaks work. Can be "even", and sections start in an
# even page, "odd", and sections start in odd pages, or "any" and
# sections start in the next page, be it even or odd. break_side="any" # Add a blank page at the beginning of the document blank_first_page=false # Treat the first page as even (default false, treat it as odd) first_page_even=false # Smart quotes.
# 0: Suppress all transformations. (Do nothing.)
# 1: Performs default SmartyPants transformations: quotes (including ‘‘backticks''
# -style), em-dashes, and ellipses. "--" (dash dash) is used to signify an em-dash;
# there is no support for en-dashes.
# 2: Same as 1, except that it uses the old-school typewriter shorthand for
# dashes: "--" (dash dash) for en-dashes, "---" (dash dash dash) for em-dashes.
# 3: Same as 2, but inverts the shorthand for dashes: "--" (dash dash) for
# em-dashes, and "---" (dash dash dash) for en-dashes. smartquotes=0 # Footnote backlinks enabled or not (default: enabled) footnote_backlinks=true # Show footnotes inline instead of at the end of the document inline_footnotes=false # Cover page template.
# It will be searched in the document's folder, in ~/.rst2pdf/templates and
# in the templates subfolder of the package folder # custom_cover = cover.tmpl # Use floating images.
# Makes the behaviour of images with the :align: attribute more like rst2html's floating_images = false # Support the ..raw:: html directive
raw_html = false

使用以上的配置文件,我们需要将刚刚的 chinese.style 放到 ~/.rst2pdf/styles/ 文件夹中,以后再生成pdf的时候,就可以简单的使用和英文相同的方式了

rst2pdf xxx.rst

默认设置下,rst2pdf的效果貌似要比pandoc的要好,以后不太要求的文档就用这个好了,长篇文档,估计还得使用sphinx

参考

官方文档:http://rst2pdf.ralsina.me/handbook.html

某博客: http://blog.163.com/ar_cn/blog/static/14538308520104102716573/

rst2pdf 中文的更多相关文章

  1. Pandoc PDF 中文

    最近终于又决定(^_^)使用reStructuredText写文档了,输出PDF时的中文问题必须要解决下. 安装环境 sudo apt install texlive texlive-latex-ex ...

  2. win10 环境 gitbash 显示中文乱码问题处理

    gitbash 是 windows 环境下非常好用的命令行终端,可以模拟一下linux下的命令如ls / mkdir 等等,如果使用过程中遇到中文显示不完整或乱码的情况,多半是因为编码问题导致的,修改 ...

  3. 网页提交中文到WEB容器的经历了些什么过程....

    先准备一个网页 <html><meta http-equiv="Content-Type" content="text/html; charset=gb ...

  4. opencv在图像显示中文

    在图像定位和模式识别时,经常需要把结果标注到图片上,标注内容可以是数字字母.矩形框等(opencv支持的)或者是中文汉字(借助freetype). 1.显示数字/矩形框 #include <op ...

  5. solr服务中集成IKAnalyzer中文分词器、集成dataimportHandler插件

    昨天已经在Tomcat容器中成功的部署了solr全文检索引擎系统的服务:今天来分享一下solr服务在海量数据的网站中是如何实现数据的检索. 在solr服务中集成IKAnalyzer中文分词器的步骤: ...

  6. Windows server 2012 添加中文语言包(英文转为中文)(离线)

    Windows server 2012 添加中文语言包(英文转为中文)(离线) 相关资料: 公司环境:亚马孙aws虚拟机 英文版Windows2012 中文SQL Server2012安装包,需要安装 ...

  7. java中文乱码解决之道(一)-----认识字符集

    沉寂了许久(大概有三个多月了吧),LZ"按捺不住"开始写博了! java编码中的中文问题是一个老生常谈的问题了,每次遇到中文乱码LZ要么是按照以前的经验修改,要么则是baidu.c ...

  8. Sublime Text 3中文乱码解决方法以及安装包管理器方法

    一般出现乱码是因为文本采用了GBK编码格式,Sublime Text默认不支持GBK编码. 安装包管理器 简单安装 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令 ...

  9. MAC下 mysql不能插入中文和中文乱码的问题总结

    MAC下 mysql不能插入中文和中文乱码的问题总结 前言 本文中所提到的问题解决方案,都是基于mac环境下的,但其他环境,比如windows应该也适用. 问题描述 本文解决下边两个问题: 往mysq ...

随机推荐

  1. 【CF1029B】Creating the Contest(贪心)

    题意: n<=2e5 思路:可以证明答案一定是极长的一段中取最大值 #include<cstdio> #include<cstring> #include<stri ...

  2. 为IIS添加json扩展类型文件的MiME类型

    IIS6.0 1.打开IIS添加Mime项 关联扩展名:*.json内容类型(MIME):application/x-javascript      2.添加映射: 位置在IIS对应站点右键属性:”主 ...

  3. Select语句执行顺序《转》

    原文发布时间为:2010-10-12 -- 来源于本人的百度文章 [由搬家工具导入] 目的在于理解如何Select 【搜索所得】: 标准的 SQL 的解析顺序为:(1).FROM 子句, 组装来自不同 ...

  4. linux内核情景分析之内核中的互斥操作

    信号量机制: struct sempahore是其结构,定义如下 struct semaphore { atomic_t count;//资源数目 int sleepers;//等待进程数目 wait ...

  5. DCP port

    DCP port: D+ D- short. This doesn't support any data transfer. Without the need ofr enumeration. Can ...

  6. MMU介绍【转】

    转自:http://blog.csdn.net/martree/article/details/3321578 虚拟存储器的基本思想是程序,数据,堆栈的总的大小可以超过物理存储器的大小,操作系统把当前 ...

  7. poj 1061(线性同余)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 104278   Accepted: 20356 Descript ...

  8. 关于Integer的parseInt(String s, int radix)方法的使用

    我们平时用到Integer.parseInt("123");其实默认是调用了int i =Integer.parseInt("123",10); 其中10代表的 ...

  9. DOM-window下的常用子对象-location-刷新页面

    1.刷新当前页面:(通过给location.href赋值的方式) window.location.href="" eg:window.location.href="htt ...

  10. saltstack安装+基本命令

    环境: node1:172.16.1.60 OS:centos 7.3 master hostname:centos7u3-1 node2:172.16.1.61 OS:centos 7.3 mini ...