上篇说到用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. .NET泛型编程 性能提升工具 List<T>

    原文发布时间为:2009-10-27 -- 来源于本人的百度文章 [由搬家工具导入] 结论  .NET 2.0中的泛型是强有力的,你写的代码不必限定于一特定类型,然而你的代码却能具有类型安全性。泛型的 ...

  2. eclipse在linux下无故闪退解决

    A fatal error has been detected by the Java Runtime Environment: SIGSEGV (0xb) at pc=0x00007f25d0b92 ...

  3. 转 C++拷贝构造函数详解

    C++拷贝构造函数详解 一. 什么是拷贝构造函数 首先对于普通类型的对象来说,它们之间的复制是很简单的,例如: int a = 100; int b = a; 而类对象与普通对象不同,类对象内部结构一 ...

  4. FTK应用程序编程接口(API)手册-1【转】

    转自:http://blog.csdn.net/absurd/article/details/6702047 框架函数 框架函数支撑FTK的主体,它负责初始化应用程序,启动主循环和退出主循环.它对任何 ...

  5. 转载:html+js实现只允许输入两位小数的输入框

    JS代码: <script language="JavaScript" type="text/javascript"> function clear ...

  6. 自动内存管理算法 —— 标记和复制法

    最近阅读了<垃圾回收算法手册>这本经典的书籍,借此机会打算写几篇内存管理算法方面的文章,也算是自己的总结吧.                                         ...

  7. slam学习足迹

    1.slam入门介绍 2.齐次坐标系 3.贝叶斯滤波 均值:平均值 标准差:样本的集中程度/分散度 方差:标准差的平方 协方差:不同维度之间的关系(相关度) 协方差矩阵:多维度之间的关系(相关度) 4 ...

  8. ArcGIS 开发时,解决__类型“XX”同时存在于“”和“”中__的错误

    错误提示:类型“ESRI.ArcGIS.ADF.BaseClasses.BaseCommand”同时存在于“e:\Program Files\ArcGIS\DeveloperKit10.2\DotNe ...

  9. 【mybatis】从一个错误,看mybatis中的#和$的区别

    事情的发展是这样的: 因为一个需求,需要在java中拼接出一个完整的sql语句,然后将整条sql语句传递给mybatis执行. mapper.java是这样的: int insertMaster(Wo ...

  10. DotnetBrowser入门教程-入门

    在.net core时代,web开发基本可以用.net core 2.0取代了.但是在传统领域,桌面开发仍然是不可以抛弃的,譬如: 1.用户需要和串口或者硬件打交道. 2.用户只想简单的安装好就使用, ...