Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 7, 2023 04:28
Show Gist options
  • Save code-boxx/bf8538a33095ec10c37ed159569502a5 to your computer and use it in GitHub Desktop.
Save code-boxx/bf8538a33095ec10c37ed159569502a5 to your computer and use it in GitHub Desktop.
Python Convert HTML To PDF

PYTHON CONVERT HTML TO PDF

https://code-boxx.com/python-convert-html-to-pdf/

NOTES

  1. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will:
    • Download the below dummy image.
    • Create a virtual environment.
    • Install pdfkit, PyQt5, PyQtWebEngine.
  2. Run the respective 1-pdfkit.py, 2-qt.py, 3-chrome.py for the different conversion methods.
  3. For 1-pdfkit.py to work, you need to install wkhtmltopdf - https://wkhtmltopdf.org/downloads.html

IMAGE

celebrate

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# (A) LOAD PDFKIT
import pdfkit
# (B) HTML TO PDF
pdfkit.from_string(
"<h1>It Works!</h1><p>This is an HTML string</p>",
"demo1.pdf"
)
pdfkit.from_file(
"x-dummy.html",
"demo2.pdf",
options = {"enable-local-file-access": ""}
)
pdfkit.from_url(
"https://en.wikipedia.org/wiki/Koto_(instrument)",
"demo3.pdf"
)
# (A) LOAD MODULES
import sys
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
# (B) CREATE QAPP
app = QApplication(sys.argv)
# (C) WEB ENGINE VIEW - PRINT URL TO PDF
webview = QWebEngineView()
webview.setZoomFactor(1)
webview.page().pdfPrintingFinished.connect(webview.close)
webview.load(QUrl("https://en.wikipedia.org/wiki/Koto_(instrument)"))
def to_pdf(finished):
webview.page().printToPdf("demo4.pdf")
webview.loadFinished.connect(to_pdf)
# (D) GO!
sys.exit(app.exec_())
# (A) CHANGE TO YOUR OWN!
chrome = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
output = "D:\\http\\demo5.pdf"
url = "https://en.wikipedia.org/wiki/Koto_(instrument)"
cmd = f'"{chrome}" --headless --disable-gpu --print-to-pdf="{output}" {url}'
# (B) RUN COMMAND
import subprocess
subprocess.run(cmd)
curl https://user-images.githubusercontent.com/11156244/280919934-c3443d1f-a1ea-4e3c-bb4b-a4e47715f5da.png --ssl-no-revoke --output celebrate.png
virtualenv venv
call venv\Scripts\activate
pip install pdfkit PyQt5 PyQtWebEngine
curl https://user-images.githubusercontent.com/11156244/280919934-c3443d1f-a1ea-4e3c-bb4b-a4e47715f5da.png --ssl-no-revoke --output ./celebrate.png
virtualenv venv
source "venv/bin/activate"
pip install pdfkit PyQt5 PyQtWebEngine
<!DOCTYPE html>
<html>
<head>
<title>Dummy HTML page</title>
<meta charset="utf-8">
</head>
<body>
<h1>It Works!</h1>
<img src="celebrate.png">
<p>This is from an HTML file.</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment