激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

腳本之家,腳本語言編程技術及教程分享平臺!
分類導航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務器之家 - 腳本之家 - Python - 使用rst2pdf實現將sphinx生成PDF

使用rst2pdf實現將sphinx生成PDF

2020-08-26 09:27hankcs Python

這篇文章主要介紹了使用rst2pdf實現將sphinx生成PDF的相關資料,以及使用過程用遇到的錯誤的處理方法,非常的全面,需要的朋友可以參考下

當初項目文檔是用sphinx寫的,一套rst下來make html得到一整個漂亮的在線文檔。現在想要將文檔導出為離線的handbook pdf,于是找到了rst2pdf這個項目,作為sphinx的拓展,然后加上少量配置即可輸出中文PDF

rst2pdf

簡介

使用rst2pdf實現將sphinx生成PDF

rst2pdf是一個將 reStructuredText 轉換為 PDF 的工具,具有下列特性:

  1. 自定義頁面布局
  2. 支持層疊樣式表
  3. 支持內嵌TTF和Type1字體
  4. 支持幾乎所有語言的語法高亮
  5. 使用reStructuredText作為源文件
  6. 支持字間距調整

安裝

?
1
easy_install rst2pdf

配置rst2pdf

注冊到sphinx項目

需要告訴sphinx我們安裝了rst2pdf,并且將其作為插件使用。只需在項目根目錄下的conf.py中配置:

?
1
2
3
4
5
6
7
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
  'sphinx.ext.autodoc',
  'rst2pdf.pdfbuilder'
]

即可。然后,在conf.py中拷入PDF相關的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -- Options for PDF output --------------------------------------------------
 
# Grouping the document tree into PDF files. List of tuples
# (source start file, target name, title, author, options).
#
# If there is more than one author, separate them with \\.
# For example: r'Guido van Rossum\\Fred L. Drake, Jr., editor'
#
# The options element is a dictionary that lets you override
# this config per-document.
# For example,
# ('index', u'MyProject', u'My Project', u'Author Name',
# dict(pdf_compressed = True))
# would mean that specific document would be compressed
# regardless of the global pdf_compressed setting.
 
pdf_documents = [
  ('index', u'HanLP Handbook', u'HanLP Handbook', u'hankcs'),
]
 
# A comma-separated list of custom stylesheets. Example:
pdf_stylesheets = ['a3','zh_CN']
 
# Create a compressed PDF
# Use True/False or 1/0
# Example: compressed=True
#pdf_compressed = False
 
# A colon-separated list of folders to search for fonts. Example:
pdf_font_path = ['C:\\Windows\\Fonts']
 
# Language to be used for hyphenation support
pdf_language = "zh_CN"
 
# Mode for literal blocks wider than the frame. Can be
# overflow, shrink or truncate
pdf_fit_mode = "shrink"
 
# Section level that forces a break page.
# For example: 1 means top-level sections start in a new page
# 0 means disabled
#pdf_break_level = 0
 
# When a section starts in a new page, force it to be 'even', 'odd',
# or just use 'any'
#pdf_breakside = 'any'
 
# Insert footnotes where they are defined instead of
# at the end.
#pdf_inline_footnotes = True
 
# verbosity level. 0 1 or 2
#pdf_verbosity = 0
 
# If false, no index is generated.
#pdf_use_index = True
 
# If false, no modindex is generated.
#pdf_use_modindex = True
 
# If false, no coverpage is generated.
#pdf_use_coverpage = True
 
# Documents to append as an appendix to all manuals.
#pdf_appendices = []
 
# Enable experimental feature to split table cells. Use it
# if you get "DelayedTable too big" errors
#pdf_splittables = False
 
# Set the default DPI for images
#pdf_default_dpi = 72
 
# Enable rst2pdf extension modules (default is only vectorpdf)
# you need vectorpdf if you want to use sphinx's graphviz support
#pdf_extensions = ['vectorpdf']
 
# Page template name for "regular" pages
#pdf_page_template = 'cutePage'
 
# Show Table Of Contents at the beginning?
# pdf_use_toc = False
 
# How many levels deep should the table of contents be?
pdf_toc_depth = 2
 
# Add section number to section references
pdf_use_numbered_links = False
 
# Background images fitting mode
pdf_fit_background_mode = 'scale'

具體配置項的值請自行調整,不需要嚴格按照我的來。

樣式表

在項目根目錄下創建一個zh_CN.json,寫入:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
 "embeddedFonts": [
  "simsun.ttc"
 ],
 "fontsAlias": {
  "stdFont": "simsun",
  "stdBold": "simsun",
  "stdItalic": "simsun",
  "stdBoldItalic": "simsun",
  "stdMono": "simsun",
  "stdMonoBold": "simsun",
  "stdMonoItalic": "simsun",
  "stdMonoBoldItalic": "simsun",
  "stdSans": "simsun",
  "stdSansBold": "simsun",
  "stdSansItalic": "simsun",
  "stdSansBoldItalic": "simsun"
 },
 "styles": [
  [
   "base",
   {
    "wordWrap": "CJK"
   }
  ],
  [
   "literal",
   {
    "wordWrap": "None"
   }
  ]
 ]
}

關于以上樣式的說明:

embeddedFonts用于嵌入字體,經試驗,必須包含至少四個值才不會報錯。不過這四個字體值可以是重復的。

fontsAlias用來指定各類字形用什么字體。如stdFont指正文字體,stdBold指粗體,stdItalic指斜體。其他的還有stdBoldItalic粗斜體,stdMono等寬體,等等。確保所用字體已經安裝在你的操作系統上,且字體必須是TTF類型的(Windows環境下限制比較多~)。

wordWrap用于指定換行規則,CJK就是適用于中日韓文字的規則。這是從網上的模板抄來的,但經我的測試發現,如果用CJK規則的話,中英混排的文檔里面英文部分就沒法正常斷行,這真是個遺憾。實際上,fontsAlias的分類很多都只對英文字體有意義,如嚴格來講中文是沒有所謂斜體的(不過因為Word的普及,經常看到中文被設置為斜體的情形)。如果是純中文文檔,當然隨便用哪些中文字體都行,如宋體。現實中,經常會有中英文混排的情形,所以如果全用中文字體的話,英文部分就沒法顯示斜體等字形了。

關于pdf_stylesheets的說明:這個參數中默認使用的某些樣式包含了一些字體,而這些字體并非在所有操作系統上都找得到。'sphinx'和'kerning'都是默認提供的樣式,要么不用它們,要么直接修改其包含的字體。'a4'指設置輸出的PDF為A4紙大小。默認的樣式文件可以在rst2pdf的安裝路徑下找到。

然后配置編譯腳本

Windows用戶,在make.bat中加入:

if "%1" == "pdf" (
  %SPHINXBUILD% -b pdf %ALLSPHINXOPTS% %BUILDDIR%/pdf
  if errorlevel 1 exit /b 1
  echo.
  echo.Build finished. The pdf files are in %BUILDDIR%/pdf.
  goto end
)

類Unix用戶修改Makefile:

?
1
2
3
4
pdf:
  $(SPHINXBUILD) -b pdf $(ALLSPHINXOPTS) $(BUILDDIR)/pdf
  @echo
  @echo "Build finished. The PDF files are in _build/pdf."

輸出PDF

然后一句:

?
1
make pdf

就能輸出PDF了。

解決findfonts.py:249 Unknown font:

這應該是由于pdf_font_path配置有誤造成的,事實上,我確定配置無誤rst2pdf還是找不到字體文件,于是我修改了X:\Program Files (x86)\Python27\Lib\site-packages\rst2pdf\findfonts.py第236行,在

fontfile = get_nt_fname(fname)

后面加了一句:

fontfile = 'C:\\Windows\\Fonts\\simsun.ttc'

強行解決問題。

解決rst2pdf輸出PDF為空白文檔

事實上,在字體正常的情況下,我發現輸出的PDF依然是空白的:

使用rst2pdf實現將sphinx生成PDF

在使用二分法排除rst文件中的問題后,我發現這是由于PDF開頭的目錄造成的。當目錄超出一頁時就會發生這種情況,我傾向于認為這是rst2pdf的一個bug。

解決方法是將pdf_toc_depth調小一點,或者干脆不生成目錄,pdf_use_toc = False。

PDF效果

于是再次重試,可以生成漂亮的PDF了:

使用rst2pdf實現將sphinx生成PDF

相關鏈接:

http://sphinx-users.jp/cookbook/pdf/rst2pdf.html

http://ralsina.me/static/manual.pdf

http://www.typemylife.com/sphinx-restructuredtext-pdf-generation-with-rst2pdf/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品视频2021 | 成人午夜精品 | 日韩在线播放中文字幕 | 欧美女同hd | 久久久久二区 | 一边吃奶一边插下面 | 成年毛片 | 黄色试看视频 | 一级黄色免费观看视频 | 好吊色37pao在线观看 | 久久2019中文字幕 | 亚洲免费视 | 黄色免费不卡视频 | 91午夜视频 | xxxx18韩国护士hd老师 | 久久狠狠高潮亚洲精品 | 黄色片视频观看 | xxx日本视频| 黄色一级片免费观看 | 欧美一级电影网站 | 特黄一级小说 | 曰批全过程120分钟免费69 | 久久久久久久久久久久久久久伊免 | 欧美特黄a | 男女无遮挡羞羞视频 | 久久久av亚洲男天堂 | 国产高潮失禁喷水爽到抽搐视频 | 大学生a级毛片免费视频 | 最近日本电影hd免费观看 | 日韩色视频在线观看 | 欧美成人午夜一区二区三区 | 亚洲成人福利在线 | 美女在线视频一区二区 | 久久亚洲第一 | 国产在线色 | 国产精品成人久久久久a级 av电影在线免费 | 91在线色 | asian超清日本肉体pics | 黄色高清av | 午夜视频在线 | 看免费的毛片 |