上篇说到用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. Eat the Trees(hdu 1693)

    题意:在n*m的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少中方法. 第一道真正意义上的插头DP,可参考陈丹琦的<基于连通性状态压缩的动态规划问题> ...

  2. BZOJ 2146 Construct

    Construct [问题描述] 随着改革开放的深入推进…… 小T家要拆迁了…… 当对未来生活充满美好憧憬的小T看到拆迁协议书的时候,小T从一位大好的社会主义青年变成了绝望的钉子户. 由于小T的家位于 ...

  3. onbeforeunload 的使用

    原文发布时间为:2008-10-20 -- 来源于本人的百度文章 [由搬家工具导入] onbeforeunload 可以在页面关闭,刷新,跳转时弹出提示信息,防止意外的跳转使得当前页的表单内容被清空。 ...

  4. Day 20 Object_oriented_programing(摘)

    面向对象变成介绍 面向过程编程 核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西.主要应用在一旦完成很少修改的地方,如linux ...

  5. centos 7 安装git并配置ssh

    一.安装 1.查看是否安装git rpm -qa|grep git 有git加版本号就说明已经安装过了 2.安装git yum install git 3.查看git版本 git version 二. ...

  6. GRDB自定义的纯函数

    GRDB自定义的纯函数   在GRDB中,用户可以自定义SQlite函数.这样,在SQL语句中,可以直接调用这些函数.但是在定义的时候,用户需要指定函数的pure属性,表示该函数是否为纯函数.纯函数是 ...

  7. shell脚本 linux脚本

    linux:shell 脚本 如果判断当前时间 是不是12点之前 用date命令先取得当前的时间(仅取小时数) : date '+%H'     #按24小时制取hour (00..23) 然后与12 ...

  8. [深入浅出iOS库]之数据库 sqlite

    一,sqlite 简介 前面写了一篇博文讲如何在 C# 中使用 ADO 访问各种数据库,在移动开发和嵌入式领域也有一个轻量级的开源关系型数据库-sqlite.它的特点是零配置(无需服务器),单磁盘文件 ...

  9. Ajax方式实现注册验证代码

    经常用到的经典Ajax代码,记录备用: function CreateAjax() { var XMLHttp; try { XMLHttp = new ActiveXObject("Mic ...

  10. dedecms 留言板中引用模板文件方法

    最近在做一个用dedecms搭建的网站,客户提出要有留言板,dedecms带了一个留言板的模块,安装倒是十分简便,但装完后发现界面十分粗糙.装修比较简单,但是发现遇到一个问题:网站通用的导航栏无法显示 ...