All Projects → twtrubiks → python-pdfkit-example

twtrubiks / python-pdfkit-example

Licence: MIT License
python-pdfkit HTML TO PDF Example

Programming Languages

c
50402 projects - #5 most used programming language
python
139335 projects - #7 most used programming language
Pawn
127 projects
HTML
75241 projects

Projects that are alternatives of or similar to python-pdfkit-example

pydf
PDF generation in python using wkhtmltopdf for heroku and docker
Stars: ✭ 68 (+277.78%)
Mutual labels:  wkhtmltopdf, html-to-pdf
api2pdf.php
PHP client library for the Api2Pdf.com REST API - Convert HTML to PDF, URL to PDF, Office Docs to PDF, Merge PDFs, HTML to Image, URL to Image, HTML to Docx, HTML to Xlsx, PDF to HTML, Thumbnail preview of office files
Stars: ✭ 42 (+133.33%)
Mutual labels:  wkhtmltopdf, html-to-pdf
Pdfkit
A Ruby gem to transform HTML + CSS into PDFs using the command-line utility wkhtmltopdf
Stars: ✭ 2,799 (+15450%)
Mutual labels:  wkhtmltopdf, html-to-pdf
bookjs-eazy
web print / html to pdf so eazy ,HTML自动分页插件。用于生成PDF,前端WEB打印生成PDF或后端wkhtmltopdf、chrome headless生成
Stars: ✭ 99 (+450%)
Mutual labels:  wkhtmltopdf
shuttlepdf
📃 Smashingly simple, and scalable ("serverless") HTML to PDF conversions using Google Cloud Functions, and Puppeteer.
Stars: ✭ 15 (-16.67%)
Mutual labels:  html-to-pdf
html-to-pdf-convertor
HTML to PDF convertor for Android
Stars: ✭ 45 (+150%)
Mutual labels:  html-to-pdf
wkhtmltopdf-binary-edge
this is forked from tolgap/wkhtmltopdf-binary .
Stars: ✭ 70 (+288.89%)
Mutual labels:  wkhtmltopdf
docraptor-ruby
A native Ruby client for the DocRaptor HTML to PDF generation API
Stars: ✭ 20 (+11.11%)
Mutual labels:  html-to-pdf
dhtml2pdf
Simple, free and very easy to use PHP API that allows you to see, download or get the binary of the PDF generated from the HTML of an URL.
Stars: ✭ 27 (+50%)
Mutual labels:  wkhtmltopdf
fpdf-easytable
fpdf-easyTable is a PHP class that makes it easy to build tables for PDF documents generated with the FPDF library
Stars: ✭ 74 (+311.11%)
Mutual labels:  html-to-pdf
wkhtmltopdf-flask-aas
Wkhtmltopdf Flask As a Service
Stars: ✭ 17 (-5.56%)
Mutual labels:  wkhtmltopdf
mdconv
A CLI markdown converter written in Go, that does not depend on LaTeX.
Stars: ✭ 42 (+133.33%)
Mutual labels:  wkhtmltopdf
imprenta
An AWS lambda in python 3 that generates PDF files from HTML using jinja, pdfkit and wkhtmltopdf.
Stars: ✭ 18 (+0%)
Mutual labels:  wkhtmltopdf
odoo-11.0-windows-installation
Install odoo-11.0 from source on Windows
Stars: ✭ 34 (+88.89%)
Mutual labels:  wkhtmltopdf
wkhtmltopdf-aws-lambda-layer
AWS Lambda Layer for wkhtmltopdf
Stars: ✭ 62 (+244.44%)
Mutual labels:  wkhtmltopdf
puppeteer-report
Convert HTML to PDF by Puppeteer with support of adding a custom header, footer, and page number
Stars: ✭ 90 (+400%)
Mutual labels:  html-to-pdf
Docotic.Pdf.Samples
C# and VB.NET samples for Docotic.Pdf library
Stars: ✭ 52 (+188.89%)
Mutual labels:  html-to-pdf
automation-report
Automation report是一款可以解决很多行业领域中设涉及到报告生成的需求,本项目最开始的初衷是为公司内部简化人工流程的一个环节,主要实现目的是将实验室检测得出的下机数据结果与对应的报告模版批量结合生成报告(.pdf)。
Stars: ✭ 13 (-27.78%)
Mutual labels:  wkhtmltopdf
magento2-module-pdf
Magento 2 Module for creating PDF's based on wkhtmltopdf
Stars: ✭ 55 (+205.56%)
Mutual labels:  wkhtmltopdf
pdf-generator-service
A simple express service that generates a pdf based on the submitted HTML
Stars: ✭ 28 (+55.56%)
Mutual labels:  html-to-pdf

python-pdfkit-example

python-pdfkit HTML TO PDF Example

使用 python-pdfkit 將 HTML 轉成 PDF

常常需要將一些東西轉換成 PDF,今天教你使用 python-pdfkit 快速完成這個功能。

使用 Python Flask 搭配 python-pdfkit 實現轉換成 PDF 功能。

特色

  • 搭配 python-pdfkit 實現轉換成 PDF 功能 ( HTML 轉成 PDF)。

安裝套件

請先確定電腦有安裝 Python

因為轉出 PDF 是用到 python-pdfkit 以及 wkhtmltopdf

所以必須安裝這兩個套件,以下用 Windows 環境介紹

python-pdfkit

pip install pdfkit

更多 python-pdfkit ,可參考 python-pdfkit

wkhtmltopdf

最後 python 還是會去使用 wkhtmltopdf.exe ,所以請務必安裝 wkhtmltopdf

安裝方法很簡單,基本上就是無腦安裝 exe 檔(請選擇符合自己電腦的版本),請記好自己安裝的路徑

alt tag

alt tag

安裝好 wkhtmltopdf 之後,建議大家可以先做測試,測試方法如下

先使用 cmd (命令提示字元) 切換到 wkhtmltopdf 路徑下

接著請在 cmd (命令提示字元) 輸入以下指令

wkhtmltopdf "https://www.google.com.tw/" out.pdf

alt tag

順利的話,路徑下會多出 out.pdf 的檔案,打開它可以看到我們將 google 的首頁轉成 pdf 文件了

alt tag

更多 wkhtmltopdf ,可參考 wkhtmltopdf

開始使用 python-pdfkit

設定路徑

前面有提到 python-pdfkit 還是會去使用 wkhtmltopdf.exe,所以我們必須告訴系統自己的 wkhtmltopdf.exe 路徑

config = pdfkit.configuration(wkhtmltopdf='wkhtmltopdf.exe 存在路徑')
pdfkit.from_url("目標網址", "輸出檔案", configuration=config)
config = pdfkit.configuration(wkhtmltopdf='C:/Program Files (x86)/wkhtmltopdf/bin/wkhtmltopdf.exe')
pdfkit.from_url("https://www.google.com.tw/", "out.pdf", configuration=config)

當然,你也可以直接設定電腦的環境變數。

一些 python-pdfkit 基本的使用

import pdfkit

# 指定目標網址 轉成 pdf 
pdfkit.from_url('https://www.google.com.tw/', 'out.pdf')

# 指定目標檔案(html) 轉成 pdf 
pdfkit.from_file('test.html', 'out.pdf')

# 指定目標字串 轉成 pdf 
pdfkit.from_string('Hello!', 'out.pdf')

指定目標字串也可以寫 html

body = """
    <html>
      <head>
        <meta name="pdfkit-page-size" content="Legal"/>
        <meta name="pdfkit-orientation" content="Landscape"/>
      </head>
      Hello World!
      </html>
    """

pdfkit.from_string(body, 'out.pdf')

合併多的網址、檔案(html) 轉成 pdf

pdfkit.from_url(['https://www.google.com.tw/', 'https://tw.yahoo.com/', 'https://shopping.pchome.com.tw/'], 'out.pdf')
pdfkit.from_file(['file1.html', 'file2.html'], 'out.pdf')

額外多的設定,像是邊距、邊碼、大小

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'),
    ],
    'no-outline': None
}

pdfkit.from_url('https://www.google.com.tw/', 'out.pdf', options=options)

更多的額外設定的參數,可參考這份說明 wkhtmltopdf.txt

P.S
如果出現如下圖(很多黑色框框),代表你的電腦可能沒安裝字型檔 alt tag

如果出現亂碼(如下圖),代表是編碼問題

alt tag

指定編碼即可解決這個問題

options = {
        'encoding': 'UTF-8'
    }
pdfkit.from_string(body, filename, configuration=config, options=options)

alt tag

如果出現下方的錯誤

IOError: 'No wkhtmltopdf executable found'

通常是你沒安裝 wkhtmltopdf 或是 你忘記指定 wkhtmltopdf.exe 的路徑

執行畫面 (簡單範例)

執行程式,請用 系統管理員 執行 (不然有時候會無法存取)

請將記得安裝套件

pip install -r requirements.txt

首頁 alt tag 可以輸入網址 (將目標網址轉成 PDF )

PDF template (範例) alt tag

執行環境

  • Python 3.4.3

Reference

License

MIT license

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].