Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to convert HTML to PDF with python package pdfkit

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the knowledge about "how to convert HTML to PDF with python package pdfkit". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

python package-pdfkit convert HTML to PDF what is pdfkit

pdfkit, HTML+CSS format files into PDF format documents a tool. It is html to pdf toolkit wkhtmltopdf Python package. Therefore, wkhtmltopdf must be installed manually.

installation

First you need to install pdfkit library, using pip install pdfkit command is good.

You also need to install the wkhtmltopdf tool, essentially using this tool to convert, pdfkit library is used as an interface to call the tool.

Python version 3.x, enter on the command line:

$sudo apt-get install wkhtmltopdf

Ubuntu can be installed directly using the following command:

$sudo yum intsall wkhtmltopdf

CentOS can be installed directly using the following command:

$sudo yum intsall wkhtmltopdf uses url to generate pdf file

If you do not specify wkhtmltopdf, you will find wkhtmltopdf from the default execution path of the system.

import pdfkit'''Generate url to pdf file'''def url_to_pdf(url, to_file): pdfkit.from_url(url, to_file,verbose=True)url_to_pdf('http://www.baidu.com','out_3.pdf')

Specify the location of wkhtmltopdf:

import pdfkit'''Generate url to pdf file'''def url_to_pdf(url, to_file): config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf') pdfkit.from_url(url, to_file,configuration=config,verbose=True)url_to_pdf www.baidu.com'out_3.pdf') String generation pdf [pdfkit.from_string() function]#Import library pdfkit''Generate string pdf file''def str_to_pdf(string, to_file): #Pass the absolute path of the wkhtmltopdf.exe program into the config object path_wkthmltopdf = r'C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe' config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf) #Generate pdf file, to_file is file path pdfkit.from_string(string, to_file, configuration=config) print ('complete ')str_to_pdf ('This is test! ',' out_3.pdf') Error OSError: No wkhtmltopdf executable found

Error when converting string, file or web content to pdf using pdfkit. from_string or pdfkit.from_file or pdfkit.from_url:

OSError: No wkhtmltopdf executable found

The obvious reason is that no executable wkhtmltopdf file was found, that is, no wkhtmltopdf.exe file was found.

Python pdfkit extension package needs to be based on wkhtmltopdf.exe executable file to run, so you need to install wkhtmltopdf first.

For windows system, you can download and install it at https://wkhtmltopdf.org/downloads.html, and then add the execution file path of the program to the environment variable (so that you can directly use pdfkit extension package, otherwise you need to indicate the path of the program when using pdfkit)

Ubuntu can be installed directly using the following command:

$sudo apt-get install wkhtmltopdf

CentOS can be installed directly using the following command:

$sudo yum intsall wkhtmltopdf"how to use python package pdfkit to convert HTML to PDF" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report