HTML is converted into a PDF tool -Wkhtmltopdf, Python generates PDF (PDFKIT library)

tags: Python  Software architecture / technology selection  reptile  linux  

First, HTML is converted into PDF tools - Wkhtmltopdf

Official website: https://wkhtmltopdf.org/

1. What is wkhtmltopdf

wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require a display or display service.

WKHTMLTOPDF and WKHTMLTOIMAGE are open source (LGPLV3) command line tools, you can use the Qt Webkit rendering engine to render HTML to PDF and various image formats using the Qt WebKit rendering engine. These complete "Headless" runs without interface or interface services.

If you like, it is still a C library.

WKHTMLTOPDF is a "command line tool" that supports multiple platforms, which can be run under Win, Linux, OS X.

2. How to use it?

  1. Download pre-compiledbinary fileOr fromSource codeConstruct

  2. Create an HTML document to be converted to PDF (or images)

  3. Run your HTML document through this tool.

3. Frequently Asked Questions

error while loading shared libraries: libjpeg.so.62: cannot open shared object file: No such file or

Problem Description:
Execute WKHTMLTOPDF -V error error While Loading Shared Libraries: libjpeg.so.62: Cannot Open Shared Object File: No Such File or Directory

Solution:

apt-get install libjpeg62-dev

Download and install font files (missing words leading to the Chinese in the generated PDF files)

1Yum Install -Y Fontconfig Mkfontscale or
apt-get -y install fontconfig xfonts-utils

2 download font file (TTF file) put into / usr / share / fonts

3 implement three orders

mkfontscale
mkfontdir
fc-cache

Second, Python generates PDF (PDFKIT library)

  1. Install PDFKIT library using PIP
pip3 install pdfkit
  1. Install WKHTMLTOPDF file

Note: PDFKit is a Python package based on Wkhtmltopdf, so you need to install WKHTMLTOPDF. WKHTMLTOPDF is a lightweight software, which is very easy to install.
Download address: https://wkhtmltopdf.org/downloads.html

  1. Generate a PDF file using the PDFKIT library
    Pdfkit can generate a PDF file with a web page, html file, string!

1. Web generate PDF [pdfkit.from_url () function]

#      
import pdfkit

'' Generates the pdf file '' '
def url_to_pdf(url, to_file):
    # Incorporate the WKHTMLTOPDF.EXE program absolute path to config object
    path_wkthmltopdf = r'D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # Generate a PDF file, TO_FILE is the file path
    pdfkit.from_url(url, to_file, configuration=config)
    print('Finish')

#               URL, convert to PDF
url_to_pdf(r'https://zhuanlan.zhihu.com/p/69869004', 'out_1.pdf')

2. HTML file generation PDF [pdfkit.from_file () function]

#      
import pdfkit

'' 'Generates the HTML file' '' '
def html_to_pdf(html, to_file):
    # Incorporate the WKHTMLTOPDF.EXE program absolute path to config object
    path_wkthmltopdf = r'D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # Generate a PDF file, TO_FILE is the file path
    pdfkit.from_file(html, to_file, configuration=config)
    print('Finish')

html_to_pdf('sample.html','out_2.pdf')

3. String generation PDF [pdfkit.from_string () function]


import pdfkit

def str_to_pdf(string, to_file):
    # Incorporate the WKHTMLTOPDF.EXE program absolute path to config object
    path_wkthmltopdf = r'D:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    # Generate a PDF file, TO_FILE is the file path
    pdfkit.from_string(string, to_file, configuration=config)
    print('Finish')

if __name__ == '__main__':
    str_to_pdf('This is test!', 'out_str_to_pdf.pdf')

Third, reference

Python generates PDF (PDFKIT library)
Reference URL: https://www.jianshu.com/p/489c3aff61bd/

Intelligent Recommendation

Python generates PDF data through pdfkit

wkhtmltopdf download address Remember to install pdfkit pip install pdfkit Pay attention to the garbled characters in the following figure, it must be the following three wrong methods   operatio...

Vue + PDFKIT + WKHTMLTOPDF Exports PDF encountered problems

First, style problem 1. When using Vue + Elementui, it is found that some components cannot export PDF, which can only use traditional HTML and CSS layout, which exported PDF clearer 2, you need to pu...

Tools for converting python html to pdf (pdfcrowd and pdfkit)

Convert html to pdf tool under python, personally think that pdfcrowd and pdfkit are better to use. Here we mainly introduce how to use it under mac, similar to other operating systems. One, pdfcrowd ...

pdfkit | Convert html files to pdf using python

When writing code documents in jupyter notebook, sometimes you need to export the pdf version, but jupyter will report an error. I was thinking that apart from the online debug method, there is no oth...

HTML to PDF Demining wkhtmltopdf Converted PDF has no content

Problem Description Opened normally with a browser, but you can't see anything after converting to a PDF document using the tool wkhtmltopdf. The html code is as follows: The CSS code is as follows: O...

More Recommendation

About wkhtmltopdf: html to pdf command line tool

First, what is wkhtmltopdf? Wkhtmltopdf is an open source, command-line tool that uses Qt WebKit rendering engine to convert html to pdf files. Wkhtmltopdf There is also a twin brother, wkhtmltoimage,...

HTML to PDF tool Wkhtmltopdf installation and use

Water spinach Need to finish reading 5minute Speed ​​reading only takes 2 minutes Here is a recommended tool for converting HTML to PDF, Wkhtmltopdf, which is installed and used as follows. 1   &...

It is best to use HTML to turn PDF tool - Wkhtmltopdf

The company needs to generate some of the part of the HTML page generate a PDF file, and then I will find some classes that use PHP to enclose the HTML page into a PDF file. The method is that it has ...

Wkhtmltopdf export html to pdf

1, use the background Recently, the company needs to generate pdf and download the contents of the html page. I tried many methods and did not have satisfactory results. Later, I found the software wk...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top