pdfkit html轉pdf


pdfkit的通用option選項

參考:https://cloud.tencent.com/developer/ask/202116
https://www.cnblogs.com/taceywong/p/5643978.html(cover)

options = {
  'page-size': 'A4',
  'header-html':'http://localhost:8080/static/data/pdfHeader.html', #設置頁眉數據,作為頁眉的html頁面必須有<!DOCTYPE html>
  'header-spacing':'3', #設置頁眉與正文之間的距離,單位是毫米
  'header-right':'Quality Report', #設置頁眉右側數據
  'header-font-size':10, #設置頁眉字體大小
  'footer-font-size':10, #設置頁腳字體大小
  'footer-right':'[page]/[topage]',
  'margin-top': '0.75in',
  'margin-right': '0.75in',
  'margin-bottom': '0.5in',
  'margin-left': '0.75in',
  'encoding': "UTF-8",
  # 'no-outline': None, #為None時表示確定,則不生成目錄
  'header-line':None, #為None時表示確定,生成頁眉下的線
}

 生成pdf:pdfkit.from_file('report.html', 'report.pdf',options=options,cover='http://localhost:8080/static/data/pdfHeader.html') ,可以使用參數cover來設置pdf封面

pdfkit可以使用wkhtmltopdf 選項,移除選項名字前面的 '--' ,如果選項沒有值, 使用 None,False或者 *作為字典值

1.使用html標簽控制pdf分頁

參考:https://my.oschina.net/yifanxiang/blog/1797678

https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1551

1.使用css來控制分頁,這個也是瀏覽器支持的打印時自動換頁的class(寫個html,用瀏覽器打印看下就明白),感覺很有用。

1 <p>Text page 1</p>
2 <div style="page-break-after:always; border:1px solid blue;"></div> 
3 <div style="page-break-before:always; border:1px solid red;"></div>
4 <p>Text page 2</p>

2.Ubuntu安裝Python pdfkit

安裝pdfkit sudo pip install pdfkit ,使用python -c 'import pdfkit'檢查是否安裝成功

下載對應版本的wkhtmltopdf deb,並安裝 https://github.com/wkhtmltopdf/wkhtmltopdf/releases/tag/0.12.5

1 sudo dpkg -i *.deb
2 #如果報依賴錯誤執行下面語句再試
3 sudo apt-get -f --fix-missing install

使用which wkhtmltopdf檢查是否安裝成功,輸出結果為:/usr/local/bin/wkhtmltopdf

使用pdfkit將html轉化為pdf

1 import pdfkit 
2 ul1 = "http://10.10.162.60/qaweb/Vpu/index"
3 pdf_file = "url.pdf" pdfkit.from_url(ul1, 'test_600.pdf')
4 pdfkit.from_file('./report_verisilicon_fpga.rdoL3.html', 'test_text.pdf')

3.wkhtmltopdf 生成帶封面、頁眉、頁腳、目錄的pdf

參考:https://my.oschina.net/bobbob/blog/360946

https://blog.csdn.net/qq_24331363/article/details/84993321

wkhtmltopdf -T 15mm --header-spacing 5 --outline cover http://image.baidu.com --header-html file:///C:/Program%20Files/wkhtmltopdf/bin/a.html --header-line --footer-center "[page]/[topage]" --footer-line http://www.yaolan.com/zhishi/tinglishaicha/ t2.pdf 

--header-html file:///C:/Program%20Files/wkhtmltopdf/bin/a.html或者 http://localhost:8080/static/data/pdfHeader.html在每一個pdf頁面設置頁眉數據,作為頁眉的html頁面必須有<!DOCTYPE html>,否則會導致生成的pdf為空白的

--outline顯示目錄

4.wkhtmltopdf 術語定義

參考: https://blog.csdn.net/chuoliang2040/article/details/100902718

文檔對象 “文檔對象”是指PDF文檔中的文檔對象,共有三種類型的“文檔對象”,他們分別是“頁面對象”,“封面對象”和“目錄對象”。

頁面對象 “頁面對象”是指以頁面的形式在PDF文檔中呈現的對象,這個是相對於“封面對象”和“目錄對象”來講的。此類對象會成為PDF文檔中內容。

封面對象 “封面對象”是指以封面的形式在PDF文檔中呈現的對象。這類對象會成為PDF文檔中的封面。

目錄對象 “目錄對象”是以目錄的形式在PDF文檔中呈現的對象,又叫“TOC對象”。這類對象會成為PDF文檔中的目錄。

大綱 “大綱”是指閱讀PDF文檔時的書簽導航。

外鏈 “外鏈”是指所有在這個頁面中且不指向它自身頁面中錨點的超鏈接。

內鏈 “內鏈”是指在這個頁面中且指向的目標頁面是這個頁面本身中的一個錨點的超鏈接。

5. wkhtmltopdf 中文參數詳解

 參考:https://blog.csdn.net/zhangkezhi_471885889/java/article/details/52184744

常規選項

–allow 允許加載從指定的文件夾中的文件或文件(可重復)

–book* 設置一會打印一本書的時候,通常設置的選項

–collate 打印多份副本時整理

–cookie 設置一個額外的cookie(可重復)

–cookie-jar 讀取和寫入的Cookie,並在提供的cookie jar文件

–copies 復印打印成pdf文件數(默認為1)

–cover* 使用HTML文件作為封面。它會帶頁眉和頁腳的TOC之前插入

–custom-header 設置一個附加的HTTP頭(可重復)

–debug-javascript 顯示的javascript調試輸出

–default-header* 添加一個缺省的頭部,與頁面的左邊的名稱,頁面數到右邊,例如: –header-left ‘[webpage]’ –header-right ‘[page]/[toPage]’ –header-line

–disable-external-links* 禁止生成鏈接到遠程網頁

–disable-internal-links* 禁止使用本地鏈接

–disable-javascript 禁止讓網頁執行JavaScript

–disable-pdf-compression* 禁止在PDF對象使用無損壓縮

–disable-smart-shrinking* 禁止使用WebKit的智能戰略收縮,使像素/ DPI比沒有不變

–disallow-local-file-access 禁止允許轉換的本地文件讀取其他本地文件,除非explecitily允許用 –allow

–dpi 顯式更改DPI(這對基於X11的系統沒有任何影響)

–enable-plugins 啟用已安裝的插件(如Flash

–encoding 設置默認的文字編碼

–extended-help 顯示更廣泛的幫助,詳細介紹了不常見的命令開關

–forms* 打開HTML表單字段轉換為PDF表單域

–grayscale PDF格式將在灰階產生

–help Display help

–htmldoc 輸出程序HTML幫助

–ignore-load-errors 忽略claimes加載過程中已經遇到了一個錯誤頁面

–lowquality 產生低品質的PDF/ PS。有用縮小結果文檔的空間

–manpage 輸出程序手冊頁

–margin-bottom 設置頁面下邊距 (default 10mm)

–margin-left 將左邊頁邊距 (default 10mm)

–margin-right 設置頁面右邊距 (default 10mm)

–margin-top 設置頁面上邊距 (default 10mm)

–minimum-font-size 最小字體大小 (default 5)

–no-background 不打印背景

–orientation 設置方向為橫向或縱向

–page-height 頁面高度 (default unit millimeter)

–page-offset* 設置起始頁碼 (default 1)

–page-size 設置紙張大小: A4, Letter, etc.

–page-width 頁面寬度 (default unit millimeter)

–password HTTP驗證密碼

–post Add an additional post field (repeatable)

–post-file Post an aditional file (repeatable)

–print-media-type* 使用的打印介質類型,而不是屏幕

–proxy 使用代理

–quiet Be less verbose

–read-args-from-stdin 讀取標准輸入的命令行參數

–readme 輸出程序自述

–redirect-delay 等待幾毫秒為JS-重定向(default 200)

–replace* 替換名稱,值的頁眉和頁腳(可重復)

–stop-slow-scripts 停止運行緩慢的JavaScripts

–title 生成的PDF文件的標題(第一個文檔的標題使用,如果沒有指定)

–toc* 插入的內容的表中的文件的開頭

–use-xserver* 使用X服務器(一些插件和其他的東西沒有X11可能無法正常工作)

–user-style-sheet 指定用戶的樣式表,加載在每一頁中

–username HTTP認證的用戶名

–version 輸出版本信息退出

–zoom 使用這個縮放因子 (default 1)

頁眉和頁腳選項

–header-center* (設置在中心位置的頁眉內容)

–header-font-name* (default Arial) (設置頁眉的字體名稱)

–header-font-size* (設置頁眉的字體大小)

–header-html* (添加一個HTML頁眉,后面是網址)

–header-left* (左對齊的頁眉文本)

–header-line* (顯示一條線在頁眉下)

–header-right* (右對齊頁眉文本)

–header-spacing* (設置頁眉和內容的距離,默認0mm)

–footer-center* (設置在中心位置的頁腳內容)

–footer-font-name* (設置頁腳的字體名稱)

–footer-font-size* (設置頁腳的字體大小default 11)

–footer-html* (添加一個HTML頁腳,后面是網址)

–footer-left* (左對齊的頁腳文本)

–footer-line* 顯示一條線在頁腳內容上)

–footer-right* (右對齊頁腳文本)

–footer-spacing* (設置頁腳和內容的距離)

./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf

 

* [page] 由當前正在打印的頁的數目代替

* [frompage] 由要打印的第一頁的數量取代

* [topage] 由最后一頁要打印的數量取代

* [webpage] 通過正在打印的頁面的URL替換

* [section] 由當前節的名稱替換

* [subsection] 由當前小節的名稱替換

* [date] 由當前日期系統的本地格式取代

* [time] 由當前時間,系統的本地格式取代

./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf

./wkhtmltopdf –header-center ‘報表’ –outline –header-line –margin-top 2cm –header-line http://www.hao123.com/ hao123.pdf

./wkhtmltopdf –header-left ‘[webpage]’ –footer-center ‘測試([page]/[toPage])’ http://www.baidu.com baidu.pdf

表內容選項中

–toc-depth* Set the depth of the toc (default 3)

–toc-disable-back-links* Do not link from section header to toc

–toc-disable-links* Do not link from toc to sections

–toc-font-name* Set the font used for the toc (default Arial)

–toc-header-font-name* The font of the toc header (if unset use –toc-font-name)

–toc-header-font-size* The font size of the toc header (default 15)

–toc-header-text* The header text of the toc (default Table Of Contents)

–toc-l1-font-size* Set the font size on level 1 of the toc (default 12)

–toc-l1-indentation* Set indentation on level 1 of the toc (default 0)

–toc-l2-font-size* Set the font size on level 2 of the toc (default 10)

–toc-l2-indentation* Set indentation on level 2 of the toc (default 20)

–toc-l3-font-size* Set the font size on level 3 of the toc (default 8)

–toc-l3-indentation* Set indentation on level 3 of the toc (default 40)

–toc-l4-font-size* Set the font size on level 4 of the toc (default 6)

–toc-l4-indentation* Set indentation on level 4 of the toc (default 60)

–toc-l5-font-size* Set the font size on level 5 of the toc (default 4)

–toc-l5-indentation* Set indentation on level 5 of the toc (default 80)

–toc-l6-font-size* Set the font size on level 6 of the toc (default 2)

–toc-l6-indentation* Set indentation on level 6 of the toc (default 100)

–toc-l7-font-size* Set the font size on level 7 of the toc (default 0)

–toc-l7-indentation* Set indentation on level 7 of the toc (default 120)

–toc-no-dots* Do not use dots, in the toc

輪廓選項

–dump-outline 轉儲目錄到一個文件

–outline 顯示目錄(文章中h1,h2來定)

–outline-depth 設置目錄的深度(默認為4)

 

6.其他

1.PDF文件對應的HTML頁面的寬度為【820px】。

2.一頁PDF文件對應的HTML頁面的高度為【1160px】,多頁類推。

3.記得要將【body】元素的外邊距【margin】清零。

如:

 1 <!DOCTYPE html>
 2 <html>
 3     <head>
 4         <meta charset="UTF-8">
 5         <title></title>
 6         <style type="text/css">
 7             body {margin: 0;}
 8             .content {width: 820px;height: 1160px;margin: 0 auto;background-color: #006DCC;}
 9         </style>
10     </head>
11     <body>
12         <div class="content"></div>
13     </body>
14 </html>

參考:https://blog.csdn.net/Mr_28/article/details/78508600

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM