最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用rst2pdf實(shí)現(xiàn)將sphinx生成PDF

 更新時(shí)間:2016年06月07日 09:54:57   作者:hankcs  
這篇文章主要介紹了使用rst2pdf實(shí)現(xiàn)將sphinx生成PDF的相關(guān)資料,以及使用過程用遇到的錯(cuò)誤的處理方法,非常的全面,需要的朋友可以參考下

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

rst2pdf

簡(jiǎn)介

rst2pdf是一個(gè)將 reStructuredText 轉(zhuǎn)換為 PDF 的工具,具有下列特性:

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

安裝

easy_install rst2pdf

配置rst2pdf

注冊(cè)到sphinx項(xiàng)目

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

# 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相關(guān)的配置:

# -- 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'

具體配置項(xiàng)的值請(qǐng)自行調(diào)整,不需要嚴(yán)格按照我的來。

樣式表

在項(xiàng)目根目錄下創(chuàng)建一個(gè)zh_CN.json,寫入:

{
 "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"
   }
  ]
 ]
}

關(guān)于以上樣式的說明:

embeddedFonts用于嵌入字體,經(jīng)試驗(yàn),必須包含至少四個(gè)值才不會(huì)報(bào)錯(cuò)。不過這四個(gè)字體值可以是重復(fù)的。

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

wordWrap用于指定換行規(guī)則,CJK就是適用于中日韓文字的規(guī)則。這是從網(wǎng)上的模板抄來的,但經(jīng)我的測(cè)試發(fā)現(xiàn),如果用CJK規(guī)則的話,中英混排的文檔里面英文部分就沒法正常斷行,這真是個(gè)遺憾。實(shí)際上,fontsAlias的分類很多都只對(duì)英文字體有意義,如嚴(yán)格來講中文是沒有所謂斜體的(不過因?yàn)閃ord的普及,經(jīng)??吹街形谋辉O(shè)置為斜體的情形)。如果是純中文文檔,當(dāng)然隨便用哪些中文字體都行,如宋體?,F(xiàn)實(shí)中,經(jīng)常會(huì)有中英文混排的情形,所以如果全用中文字體的話,英文部分就沒法顯示斜體等字形了。

關(guān)于pdf_stylesheets的說明:這個(gè)參數(shù)中默認(rèn)使用的某些樣式包含了一些字體,而這些字體并非在所有操作系統(tǒng)上都找得到。'sphinx'和'kerning'都是默認(rèn)提供的樣式,要么不用它們,要么直接修改其包含的字體。'a4'指設(shè)置輸出的PDF為A4紙大小。默認(rèn)的樣式文件可以在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:

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

輸出PDF

然后一句:

make pdf

就能輸出PDF了。

解決findfonts.py:249 Unknown font:

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

fontfile = get_nt_fname(fname)

后面加了一句:

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

強(qiáng)行解決問題。

解決rst2pdf輸出PDF為空白文檔

事實(shí)上,在字體正常的情況下,我發(fā)現(xiàn)輸出的PDF依然是空白的:

在使用二分法排除rst文件中的問題后,我發(fā)現(xiàn)這是由于PDF開頭的目錄造成的。當(dāng)目錄超出一頁(yè)時(shí)就會(huì)發(fā)生這種情況,我傾向于認(rèn)為這是rst2pdf的一個(gè)bug。

解決方法是將pdf_toc_depth調(diào)小一點(diǎn),或者干脆不生成目錄,pdf_use_toc = False。

PDF效果

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

相關(guān)鏈接:

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

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

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

相關(guān)文章

  • python多進(jìn)程共享Array問題

    python多進(jìn)程共享Array問題

    multiprocessing庫(kù)提供了Array類,允許在多個(gè)進(jìn)程間共享數(shù)組,Array在共享內(nèi)存中創(chuàng)建,各進(jìn)程可直接訪問和修改其元素,實(shí)現(xiàn)數(shù)據(jù)同步,Array支持多種數(shù)據(jù)類型,可選鎖定參數(shù)以保證數(shù)據(jù)安全
    2024-09-09
  • 使用python畫個(gè)小豬佩奇的示例代碼

    使用python畫個(gè)小豬佩奇的示例代碼

    本文給大家較詳細(xì)的介紹了使用python畫個(gè)小豬佩奇的示例代碼,感興趣的朋友一起看看吧
    2018-06-06
  • python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解

    python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解

    這篇文章主要介紹了python軟件測(cè)試Jmeter性能測(cè)試JDBC Request(結(jié)合數(shù)據(jù)庫(kù))的使用詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • python讀取文件名稱生成list的方法

    python讀取文件名稱生成list的方法

    下面小編就為大家分享一篇python讀取文件名稱生成list的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • windows+vscode安裝paddleOCR運(yùn)行環(huán)境的步驟

    windows+vscode安裝paddleOCR運(yùn)行環(huán)境的步驟

    這篇文章主要介紹了windows+vscode安裝paddleOCR運(yùn)行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-11-11
  • 講清楚fit_transform()和transform()的區(qū)別及說明

    講清楚fit_transform()和transform()的區(qū)別及說明

    這篇文章主要介紹了講清楚fit_transform()和transform()的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python 實(shí)現(xiàn)網(wǎng)課實(shí)時(shí)監(jiān)控自動(dòng)簽到、打卡功能

    Python 實(shí)現(xiàn)網(wǎng)課實(shí)時(shí)監(jiān)控自動(dòng)簽到、打卡功能

    這篇文章主要介紹了Python實(shí)現(xiàn)網(wǎng)課實(shí)時(shí)監(jiān)控自動(dòng)簽到,打卡功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • python如何導(dǎo)出微信公眾號(hào)文章方法詳解

    python如何導(dǎo)出微信公眾號(hào)文章方法詳解

    這篇文章主要介紹了python如何導(dǎo)出微信公眾號(hào)文章方法詳解,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • python監(jiān)控鍵盤輸入實(shí)例代碼

    python監(jiān)控鍵盤輸入實(shí)例代碼

    這篇文章主要介紹了python監(jiān)控鍵盤輸入實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-02-02
  • python使用PyPDF2 和 pdfplumber操作PDF文件

    python使用PyPDF2 和 pdfplumber操作PDF文件

    本文主要介紹了Python中用于操作PDF的兩個(gè)庫(kù):PyPDF2和pdfplumber,下面就來具體介紹一下兩個(gè)庫(kù)的使用方法,具有一定的參考價(jià)值,感興趣的可以了解一下
    2025-01-01

最新評(píng)論

望江县| 达日县| 奉新县| 新密市| 探索| 菏泽市| 肥东县| 东方市| 左权县| 江门市| 三河市| 临邑县| 临夏市| 东辽县| 南雄市| 汾西县| 怀安县| 哈尔滨市| 信宜市| 沙洋县| 通山县| 宣威市| 沙湾县| 宁晋县| 广安市| 陇川县| 宜都市| 兴安县| 静宁县| 榆中县| 荃湾区| 布拖县| 茂名市| 徐汇区| 白银市| 鹤庆县| 南昌县| 昌宁县| 南阳市| 阳原县| 井冈山市|