Python uses pdfkit to generate pdf [python]

1. Install pdfkit

pip3 install pdfkit

Code:

html = ''
'< body>

%s

' % text

pdfkit.from_string(html,'./result.pdf') or pdfkit.from_file(1.html,'./result.pdf')

2. Test run

Error after running python3 test.py

If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf - https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

Missing: wkhtmltopdf to install yum install wkhtmltopdf

github address: https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf

Official website: https://wkhtmltopdf.org/downloads.html

3. Continue to report error###

OSError: wkhtmltopdf: cannot connect to X server

Install xvfb

yum install xorg-x11-server-Xvfb

Generate the script wkhtmltopdf.sh in the /usr/bin/ directory and write the command

vim /usr/bin/wkhtmltopdf.sh

Write in the .sh file: xvfb-run -a --server-args="-screen 0, 1024x768x24" /usr/bin/wkhtmltopdf -q $*

chmod a+x /usr/bin/wkhtmltopdf.sh

ln -s /usr/bin/wkhtmltopdf.sh /usr/local/bin/wkhtmltopdf

Start Xserver: wkhtmltopdf http://www.baidu.com a.pdf

If the code is garbled at noon or the error is related to format

Reference: https://blog.csdn.net/q490880/article/details/81121246

Recommended Posts

Python uses pdfkit to generate pdf [python]
Use Python to generate Douyin character videos!
Python uses pandas to process Excel data
01. Introduction to Python
Introduction to Python
How to convert web pages to PDF with Python
Python uses the email module to send mail
Python uses PIL to cut and stitch pictures
Centos 6.4 python 2.6 upgrade to 2.7
Centos 6.4 python 2.6 upgrade to 2.7
Python uses the re module to verify dangerous characters
Solution to python alignment error
CentOS upgrade python2 to pyth
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
PyCharm set Python version
Python programming Pycharm fast learning
Magic methods and uses of Python
Pycharm modify python path process diagram
Python uses pdfkit to generate pdf [python]
Python uses pandas to process Excel data
How to learn python quickly
How to uninstall python plugin
Python code to find bugs(6)
Python code to find bugs (1)
Python code to find bugs(8)
3 ways to encrypt Python files
How to understand python objects
How to use python tuples