上篇说到用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. mysql 排序字段与索引有关系吗?

    mysql 排序字段与索引有关系吗?答案与否需要你explain一下你的sql脚本 另外记住:date_add()方法会影响Index_modify_time索引(即:时间字段索引)  一般遇到这样的 ...

  2. js Regex match, exec, test & jquery plugin, visit the official website!

    http://plugins.jquery.com/?s=bootstrap 1. /reg/.test(strval) 2."strval".match(/reg/g), a r ...

  3. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---31

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:

  4. vs2013载入zlib库,即include "zlib.h"

    转自wo13142yanyouxin原文vs2013载入zlib库,即include "zlib.h" 在程序中,我们经常要用到压缩,解压函数.以压缩函数compress为例进行说 ...

  5. 缠中说禅股票交易系统图解 z

    缠中说禅股票交易系统图解 2010-03-23 10:51 (王纯阳)缠论祖师的经典语录 1. 就在买点买,卖点卖:当然,买点并不一定是一个点,一个价位,级别越大的,可以容忍的区间越大. 2. 你要经 ...

  6. nginx 启动,重启,添加开机启动等相关命令

    nginx -t 测试 配置文件是否正确,同时可以查看配置文件路径 nginx -c /usr/local/nginx/conf/nginx.conf   启动nginx ps -ef|grep ng ...

  7. Ubuntu免安装配置MySQL

    1.下载mysql http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.21-linux-glibc2.5-x86_64.tar.gz 2.解压 ta ...

  8. 测试Apache服务器及httpd: Could not reliably determine the server's fully qualified domain name解决办法

    测试Apache服务器: 重启apache: sudo /usr/local/apache/bin/apachectl restart 若出现错误: httpd: Could not reliably ...

  9. Mysql中delimiter作用

    1. delimiter delimiter是mysql分隔符.在mysqlclient中分隔符默认是分号(:). 假设一次输入的语句较多,而且语句中间有分号,这时须要新指定一个特殊的分隔符. 2. ...

  10. Win2003 IIS 安装方法 图文教程

    最近水一水 质量不高 见谅 一般大家先安装好win2003系统,图文教程 Win2003 服务器系统安装图文教程要通过控制面板来安装.具体做法为: 1. 进入“控制面板”. 2. 双击“添加或删除程序 ...