7.4.2. rst2pdf的使用

Note

pip3只能安装最高版本到0.93.x(不建议) pip2能安装到最高版本0.94.1(建议)

中文问题解决 [3]

1. 首先导出rst2pdf默认的stylesheet
  $> rst2pdf --print-stylesheet > ~/.rst2pdf/styles/chinese.style

2. 打开这个文件,指定汉字字体(如仿宋Fangsong.ttf)
  embeddedFonts: [Fangsong.ttf]
  fontsAlias:
    stdFont: Fangsong
    ...
    字体都改成simhei
3. 把字体拷贝到指定目录(如字体在/Library/Fonts等目录下,可以不用拷贝)
  $ cp /Library/Fonts/Microsoft/Fangsong.ttf ~/.rst2pdf/styles/
4. 执行:
   $> rst2pdf -s ~/.rst2pdf/styles/chinese.style --font-path=~/.rst2pdf/styles/ file.rst
5. 设定rst2pdf默认配置以便可以直接执行下面语句:
  $> rst2pdf file.rst

chiness.style [6]

rst2pdf默认配置 [7],参考 [4]

颜色 [5]

注: 这个只可用于rst2pdf生成pdf加颜色
  另外, sphinx生成html加颜色链接也有说明

# 在配置文件chinese.style文件中增加
bluetext:
  parent: bodytext
  textColor: blue

# 在使用的地方调用
.. role:: bluetext
:redtext:`blue`

.. role:: redtext
:bluetext:`red`
[1]sublime的rst插件自动生成pdf文件
[2]https://github.com/rst2pdf
[3]https://blog.csdn.net/yuyan21/article/details/9297629
[4]https://www.cnblogs.com/windtail/p/8260317.html
[5]https://stackoverflow.com/questions/3702865/sphinx-restructuredtext-set-color-for-a-single-word
[6]

  # List any fonts you would like to embed in the PDF here
  embeddedFonts: [Fangsong.ttf]

  # Default page setup.  Can be overridden by including other
  # stylesheets with -s

  pageSetup:
    size: A4
    width: null
    height: null
    margin-top: 2cm
    margin-bottom: 2cm
    margin-left: 2cm
    margin-right: 2cm
    margin-gutter: 0cm
    spacing-header: 5mm
    spacing-footer: 5mm

    # The first template is one of the 'pageTemplates"
    # (See next section)

    firstTemplate: oneColumn

  # pageTemplates can be accessed with the .. raw:: pdf PageBreak command

  pageTemplates:
    coverPage:
        frames: []
            [0cm, 0cm, 100%, 100%]
        showHeader : false
        showFooter : false

    emptyPage:
        frames: []
            [0cm, 0cm, 100%, 100%]
        showHeader : false
        showFooter : false

    oneColumn:
        frames: []
            [0cm, 0cm, 100%, 100%]
        showHeader : true
        showFooter : true

    twoColumn:
        frames: []
            [0cm, 0cm, 49%, 100%]
            [51%, 0cm, 49%, 100%]
        showHeader : true
        showFooter : true

    threeColumn:
        frames: []
            [2%, 0cm, 29.333%, 100%]
            [35.333%, 0cm, 29.333%, 100%]
            [68.666%, 0cm, 29.333%, 100%]
        showHeader : true
        showFooter : true

    cutePage:
        frames: []
            [0%, 0%, 100%, 100%]
        showHeader : true
        showFooter : true
        defaultFooter : ###Page###
        defaultHeader : ###Section###

  fontsAlias:
    stdFont: Fangsong
    stdBold: Fangsong
    stdItalic: Fangsong
    stdBoldItalic: Fangsong
    stdSans: Fangsong
    stdSansBold: Fangsong
    stdSansItalic: Fangsong
    stdSansBoldItalic: Fangsong
    stdMono: Fangsong
    stdMonoItalic: Fangsong
    stdMonoBold: Fangsong
    stdMonoBoldItalic: Fangsong
    stdSerif: Fangsong

  linkColor: navy

  styles:
    base:
      parent: null
      fontName: stdFont
      fontSize: 10
      leading: 12
      leftIndent: 0
      rightIndent: 0
      firstLineIndent: 0
      alignment: TA_LEFT
      spaceBefore: 0
      spaceAfter: 0
      bulletFontName: stdFont
      bulletFontSize: 10
      bulletIndent: 0
      textColor: black
      backColor: null
      wordWrap: null
      borderWidth: 0
      borderPadding: 0
      borderColor: null
      borderRadius: null
      allowWidows: false
      allowOrphans: false
      hyphenation: false
      kerning: false
      underline: false
      strike: false
      commands: []

    normal:
      parent: base

    title-reference:
      parent: normal
      fontName: stdItalic

    bodytext:
      parent: normal
      spaceBefore: 6
      alignment: TA_JUSTIFY
      hyphenation: true

    # Added by Gordon
    bluetext:
      parent: bodytext
      textColor: blue
    redtext:
      parent: bodytext
      textColor: red


    toc:
      parent: normal

    blockquote:
      parent: bodytext
      leftIndent: 20

    lineblock:
      parent: bodytext

    line:
      parent: lineblock
      spaceBefore: 0

    toc1:
      parent: toc
      fontName: stdBold

    toc2:
      parent: toc
      leftIndent: 20

    toc3:
      parent: toc
      leftIndent: 40

    toc4:
      parent: toc
      leftIndent: 60

    toc5:
      parent: toc
      leftIndent: 80

    toc6:
      parent: toc
      leftIndent: 100

    toc7:
      parent: toc
      leftIndent: 100

    toc8:
      parent: toc
      leftIndent: 100

    toc9:
      parent: toc
      leftIndent: 100

    toc10:
      parent: toc
      leftIndent: 100

    toc11:
      parent: toc
      leftIndent: 100

    toc12:
      parent: toc
      leftIndent: 100

    toc13:
      parent: toc
      leftIndent: 100

    toc14:
      parent: toc
      leftIndent: 100

    toc15:
      parent: toc
      leftIndent: 100

    footer:
      parent: normal
      alignment: TA_CENTER

    header:
      parent: normal
      alignment: TA_CENTER

    attribution:
      parent: bodytext
      alignment: TA_RIGHT

    image:
      parent: bodytext
      alignment: TA_CENTER

    figure:
      parent: bodytext
      alignment: TA_CENTER
      commands: []
             [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
             [ALIGN, [ 0, 0 ], [ -1, -1 ], CENTER ]
      colWidths: [100%]

    figure-caption:
      parent: bodytext
      fontName: stdItalic
      alignment: TA_CENTER

    figure-legend:
      parent: bodytext

    bullet-list:
      parent: bodytext
      commands: []
             [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
             [RIGHTPADDING, [ 0, 0 ], [ 1, -1 ], 0 ]
      colWidths: ["20", null]

    bullet-list-item:
      parent: bodytext

    item-list:
      parent: bodytext
      commands: []
             [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
             [RIGHTPADDING, [ 0, 0 ], [ 1, -1 ], 0 ]
      colWidths: [20pt,null]

    item-list-item:
      parent: bodytext

    definition-list-term:
      parent: normal
      fontName: stdBold
      spaceBefore: 4
      spaceAfter: 0
      keepWithNext: false

    definition-list-classifier:
      parent: normal
      fontName: stdItalic

    definition:
      parent: bodytext
      firstLineIndent: 0
      bulletIndent: 0
      spaceBefore: 0
      colWidths: [20pt,null]
      commands: []
                    [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
                    [LEFTPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]
                    [BOTTOMPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]
                    [RIGHTPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]

    fieldname:
      parent: bodytext
      alignment: TA_RIGHT
      fontName: stdBold

    fieldvalue:
      parent: bodytext

    rubric:
      parent: bodytext
      textColor: darkred
      alignment: TA_CENTER

    italic:
      parent: bodytext
      fontName: stdItalic

    heading:
      parent: normal
      keepWithNext: true
      spaceBefore: 12
      spaceAfter: 6

    title:
      parent: heading
      fontName: stdBold
      fontSize: 200%
      alignment: TA_CENTER
      keepWithNext: false
      spaceAfter: 10

    subtitle:
      parent: title
      spaceBefore: 12
      fontSize: 75%

    heading1:
      parent: heading
      fontName: stdBold
      fontSize: 175%

    heading2:
      parent: heading
      fontName: stdBold
      fontSize: 150%

    heading3:
      parent: heading
      fontName: stdBoldItalic
      fontSize: 125%

    heading4:
      parent: heading
      fontName: stdBoldItalic

    heading5:
      parent: heading
      fontName: stdBoldItalic

    heading6:
      parent: heading
      fontName: stdBoldItalic

    topic-title:
      parent: heading3

    sidebar-title:
      parent: heading3

    sidebar-subtitle:
      parent: heading4

    sidebar:
      float: none
      width: 100%
      parent: normal
      backColor: beige
      borderColor: darkgray
      borderPadding: 8
      borderWidth: 0.5

    admonition:
      parent: normal
      spaceBefore: 12
      spaceAfter: 6
      borderPadding: [16,16,16,16]
      backColor: beige
      borderColor: darkgray
      borderWidth: 0.5
      commands:[]
             [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]

    attention:
      parent: admonition

    caution:
      parent: admonition

    danger:
      parent: admonition

    error:
      parent: admonition

    hint:
      parent: admonition

    important:
      parent: admonition

    note:
      parent: admonition

    tip:
      parent: admonition

    warning:
      parent: admonition

    admonition-title:
      parent: heading3

    admonition-heading:
      parent: heading3

    attention-heading:
      parent: admonition-heading

    caution-heading:
      parent: admonition-heading

    danger-heading:
      parent: admonition-heading

    error-heading:
      parent: admonition-heading

    hint-heading:
      parent: admonition-heading

    important-heading:
      parent: admonition-heading

    note-heading:
      parent: admonition-heading

    tip-heading:
      parent: admonition-heading

    warning-heading:
      parent: admonition-heading

    literal:
      parent: normal
      fontName: stdMono
      firstLineIndent: 0
      hyphenation: false
      wordWrap: null

    aafigure:
      parent: literal

    table:
      spaceBefore:6
      spaceAfter:0
      alignment: TA_CENTER
      commands: []
             [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
             [INNERGRID, [ 0, 0 ], [ -1, -1 ], 0.25, black ]
             [ROWBACKGROUNDS, [0, 0], [-1, -1], [white,#E0E0E0]]
             [BOX, [ 0, 0 ], [ -1, -1 ], 0.25, black ]

    table-title:
      parent : heading4
      keepWithNext: false
      alignment : TA_CENTER

    table-heading:
      parent : heading
      backColor : beige
      alignment : TA_CENTER
      valign : BOTTOM
      borderPadding : 0

    table-body:
      parent : normal

    dedication:
      parent : normal

    abstract:
      parent : normal

    contents:
      parent : normal

    tableofcontents:
      parent : normal

    code:
      parent: literal
      leftIndent: 0
      spaceBefore: 8
      spaceAfter: 8
      backColor: beige
      borderColor: darkgray
      borderWidth: 0.5
      borderPadding: 6

    linenumber:
      parent: code

    right:
      parent: bodytext
      alignment: right

    center:
      parent: bodytext
      alignment: center

    pygments-diml: {parent: code, textColor: #aaaaaa}
    pygments-n: parent: code
    pygments-nx: parent: code
    pygments-p: parent: code
    pygments-hll: {parent: code, backColor: #ffffcc}
    pygments-c: {textColor: #008800, parent: code}
    pygments-err: {parent: code}
    pygments-k: {textColor: #AA22FF, parent: code}
    pygments-o: {textColor: #666666, parent: code}
    pygments-cm: {textColor: #008800, parent: code}
    pygments-cp: {textColor: #008800, parent: code}
    pygments-c1: {textColor: #008800, parent: code}
    pygments-cs: {textColor: #008800, parent: code}
    pygments-gd: {textColor: #A00000, parent: code}
    pygments-ge: {parent: code}
    pygments-gr: {textColor: #FF0000, parent: code}
    pygments-gh: {textColor: #000080, parent: code}
    pygments-gi: {textColor: #00A000, parent: code}
    pygments-go: {textColor: #808080, parent: code}
    pygments-gp: {textColor: #000080, parent: code}
    pygments-gs: {parent: code}
    pygments-gu: {textColor: #800080, parent: code}
    pygments-gt: {textColor: #0040D0, parent: code}
    pygments-kc: {textColor: #AA22FF, parent: code}
    pygments-kd: {textColor: #AA22FF, parent: code}
    pygments-kn: {textColor: #AA22FF, parent: code}
    pygments-kp: {textColor: #AA22FF, parent: code}
    pygments-kr: {textColor: #AA22FF, parent: code}
    pygments-kt: {textColor: #00BB00, parent: code}
    pygments-m: {textColor: #666666, parent: code}
    pygments-s: {textColor: #BB4444, parent: code}
    pygments-na: {textColor: #BB4444, parent: code}
    pygments-nb: {textColor: #AA22FF, parent: code}
    pygments-nc: {textColor: #0000FF, parent: code}
    pygments-no: {textColor: #880000, parent: code}
    pygments-nd: {textColor: #AA22FF, parent: code}
    pygments-ni: {textColor: #999999, parent: code}
    pygments-ne: {textColor: #D2413A, parent: code}
    pygments-nf: {textColor: #00A000, parent: code}
    pygments-nl: {textColor: #A0A000, parent: code}
    pygments-nn: {textColor: #0000FF, parent: code}
    pygments-nt: {textColor: #008000, parent: code}
    pygments-nv: {textColor: #B8860B, parent: code}
    pygments-ow: {textColor: #AA22FF, parent: code}
    pygments-w: {textColor: #bbbbbb, parent: code}
    pygments-mf: {textColor: #666666, parent: code}
    pygments-mh: {textColor: #666666, parent: code}
    pygments-mi: {textColor: #666666, parent: code}
    pygments-mo: {textColor: #666666, parent: code}
    pygments-sb: {textColor: #BB4444, parent: code}
    pygments-sc: {textColor: #BB4444, parent: code}
    pygments-sd: {textColor: #BB4444, parent: code}
    pygments-s2: {textColor: #BB4444, parent: code}
    pygments-se: {textColor: #BB6622, parent: code}
    pygments-sh: {textColor: #BB4444, parent: code}
    pygments-si: {textColor: #BB6688, parent: code}
    pygments-sx: {textColor: #008000, parent: code}
    pygments-sr: {textColor: #BB6688, parent: code}
    pygments-s1: {textColor: #BB4444, parent: code}
    pygments-ss: {textColor: #B8860B, parent: code}
    pygments-bp: {textColor: #AA22FF, parent: code}
    pygments-vc: {textColor: #B8860B, parent: code}
    pygments-vg: {textColor: #B8860B, parent: code}
    pygments-vi: {textColor: #B8860B, parent: code}
    pygments-il: {textColor: #666666, parent: code}

    endnote:
        parent: bodytext
        commands: []
            [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
            [TOPPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]
            [BOTTOMPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]
            [RIGHTPADDING, [ 0, 0 ], [ 1, -1 ], 0 ]
        colWidths: [3cm, null]

    field-list:
        parent: bodytext
        commands: []
            [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
            [TOPPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]
        colWidths: [3cm, null]
        spaceBefore: 6

    option-list:
        commands: []
            [VALIGN, [ 0, 0 ], [ -1, -1 ], TOP ]
            [TOPPADDING, [ 0, 0 ], [ -1, -1 ], 0 ]
        colWidths: [null,null]

[7]
# 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