Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 7, 2023 04:28
Show Gist options
  • Select an option

  • Save code-boxx/bf8538a33095ec10c37ed159569502a5 to your computer and use it in GitHub Desktop.

Select an option

Save code-boxx/bf8538a33095ec10c37ed159569502a5 to your computer and use it in GitHub Desktop.

Revisions

  1. code-boxx revised this gist Nov 7, 2023. 3 changed files with 5 additions and 4 deletions.
    5 changes: 3 additions & 2 deletions 0-PY-HTML-PDF.MD
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,11 @@ https://code-boxx.com/python-convert-html-to-pdf/
    - Download the below dummy image.
    - Create a virtual environment.
    - Install `pdfkit`, `PyQt5`, `PyQtWebEngine`.
    2) For `pdfkit` to work, you need to install wkhtmltopdf - https://wkhtmltopdf.org/downloads.html
    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](https://user-images.githubusercontent.com/11156244/275173590-9179aa24-70df-408d-8eb4-40eda2446bd2.png)
    ![celebrate](https://user-images.githubusercontent.com/11156244/280919934-c3443d1f-a1ea-4e3c-bb4b-a4e47715f5da.png)

    ## LICENSE
    Copyright by Code Boxx
    2 changes: 1 addition & 1 deletion unpack.bat
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    curl https://user-images.githubusercontent.com/11156244/275173590-9179aa24-70df-408d-8eb4-40eda2446bd2.png --ssl-no-revoke --output celebrate.png
    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
    2 changes: 1 addition & 1 deletion unpack.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    curl https://user-images.githubusercontent.com/11156244/275173590-9179aa24-70df-408d-8eb4-40eda2446bd2.png --ssl-no-revoke --output ./celebrate.png
    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
  2. code-boxx created this gist Oct 14, 2023.
    33 changes: 33 additions & 0 deletions 0-PY-HTML-PDF.MD
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    ## 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) For `pdfkit` to work, you need to install wkhtmltopdf - https://wkhtmltopdf.org/downloads.html

    ## IMAGE
    ![celebrate](https://user-images.githubusercontent.com/11156244/275173590-9179aa24-70df-408d-8eb4-40eda2446bd2.png)

    ## 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.
    19 changes: 19 additions & 0 deletions 1-pdfkit.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    # (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"
    )
    20 changes: 20 additions & 0 deletions 2-qt.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    # (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_())
    9 changes: 9 additions & 0 deletions 3-chrome.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # (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)
    4 changes: 4 additions & 0 deletions unpack.bat
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    curl https://user-images.githubusercontent.com/11156244/275173590-9179aa24-70df-408d-8eb4-40eda2446bd2.png --ssl-no-revoke --output celebrate.png
    virtualenv venv
    call venv\Scripts\activate
    pip install pdfkit PyQt5 PyQtWebEngine
    4 changes: 4 additions & 0 deletions unpack.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    curl https://user-images.githubusercontent.com/11156244/275173590-9179aa24-70df-408d-8eb4-40eda2446bd2.png --ssl-no-revoke --output ./celebrate.png
    virtualenv venv
    source "venv/bin/activate"
    pip install pdfkit PyQt5 PyQtWebEngine
    12 changes: 12 additions & 0 deletions x-dummy.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!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>