--
layout: blog
title: 'Python網頁轉PDF文件'
date: 2017-05-02 12:11:34
categories: blog
tags: code
image: ''
lead-text: 'Python將HTML文件轉換為PDF文件'
使用 pdfkit
上面博文已經將pdfkit的基本使用講述清楚了,這里說的是轉換參數中options中的尺寸說明:
options = {
'page-size': 'Letter',
'margin-top': '0.75in',
'margin-right': '0.75in',
'margin-bottom': '0.75in',
'margin-left': '0.75in',
'encoding': "UTF-8",
'custom-header': [
('Accept-Encoding', 'gzip')
],
'cookie': [
('cookie-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'outline-depth': 10,
}
pdfkit的參數說明這個文檔關于轉換的size只有
page-size <Size> Set paper size to: A4, Letter, etc.(default A4)
默認A4大小,或者可以直接設置為B4,等直接按照紙張大小來設置pdf大小,但是為了匹配kindle的大小(6寸)需要單獨設置width和height
page-width <unitreal>
,根據測試,kindle的大小在width:1.2 height:1.4還是比較合適,這個大小看個人對屏幕字體尺寸的設置
還可一設置字體的大小 minimum-font-size 來設置字體的大小
下面是一個參考轉pdf時的參數設置
options = {
'minimum-font-size':'20'
'page-height': 1.4in'
'page-width': '1.2in'
'margin-top': '0.01in',
'margin-right': '0.01in',
'margin-bottom': '0.01in',
'margin-left': '0.01in',
'encoding': "UTF-8",
'custom-header': [
('Accept-Encoding', 'gzip')
],
'cookie': [
('cookie-name1', 'cookie-value1'),
('cookie-name2', 'cookie-value2'),
],
'outline-depth': 10,
}