-
Notifications
You must be signed in to change notification settings - Fork 0
qprinter
holzkohlengrill edited this page Dec 15, 2023
·
2 revisions
Converts a html into a pdf using Qt5's QPrinter
class
- Use markdown (
pip3 install Markdown
) for markdown to html conversion - and pygments (
pip3 install Pygments
) for code synthax highlighting in markdown files
from PyQt5.QtGui import QTextDocument
from PyQt5.QtPrintSupport import QPrinter
from PyQt5.QtWidgets import QApplication
import sys
def markdown():
app = QApplication(sys.argv)
path = r"""markdown\markdown.html"""
outputFile = r"""markdown\markdownOut.pdf"""
with open(path, "r") as fp:
html = fp.read()
doc = QTextDocument()
doc.setHtml(html)
printer = QPrinter()
printer.setOutputFileName(outputFile)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setDocName("MAdZ Test")
printer.setCreator("MSc")
printer.setPageSize(QPrinter.A4)
printer.setPageMargins(15, 15, 15, 15, QPrinter.Millimeter)
doc.print(printer)
print("Done printing..")
if __name__ == "__main__":
markdown()
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License *.
Code (snippets) are licensed under a MIT License *.
* Unless stated otherwise