python-qt5
python-qt5 copied to clipboard
pyqt print to pdf page breaks
I have a python code like this which converts url to pdf
import sys
from PyQt5 import QtWidgets, QtWebEngineWidgets
from PyQt5.QtCore import QUrl, QTimer, QSizeF
from PyQt5.QtGui import QPageLayout, QPageSize
from PyQt5.QtWidgets import QApplication
import argparse
def _fullScreenRequested(request):
request.accept()
loader.showFullScreen()
def main():
parser = argparse.ArgumentParser(description="Just an example", formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--url", help="Type url")
args = parser.parse_args()
config = vars(args)
url = config['url']
app = QtWidgets.QApplication(sys.argv)
loader = QtWebEngineWidgets.QWebEngineView()
loader.setZoomFactor(1)
layout = QPageLayout()
layout.setPageSize(QPageSize(QSizeF(13, 20), QPageSize.Unit.Inch, "4x6 in page", QPageSize.SizeMatchPolicy.ExactMatch))
layout.setOrientation(QPageLayout.Portrait)
loader.load(QUrl(url))
loader.page().pdfPrintingFinished.connect(lambda *args: QApplication.exit())
def emit_pdf(finished):
QTimer.singleShot(2000, lambda: loader.page().printToPdf("test.pdf", pageLayout=layout))
loader.loadFinished.connect(emit_pdf)
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I want to add page breaks to that pdf using my python code
mainly these two
https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after
https://developer.mozilla.org/en-US/docs/Web/CSS/break-after
is it possible to add page breaks in my python code to the output pdf?
hello whene i change this line
loader.load(QUrl(url))
to
loader.load(QUrl('https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after'))
It kinda do what you want to do But I suggest using the print function that you can see what are you converting to pdf before you doing it
hello whene i change this line
loader.load(QUrl(url))
to
loader.load(QUrl('https://developer.mozilla.org/en-US/docs/Web/CSS/page-break-after'))
It kinda do what you want to do But I suggest using the print function that you can see what are you converting to pdf before you doing it
I think you are mistaken. I want to add extra page breaks for the output pdf
I'm not sure if I understand what you want exactly, but if you want to make it look like a pdf file with multiple page maybe try this
layout.setPageSize(QPageSize(getattr(QPageSize, "A4")))
layout.setOrientation(getattr(QPageLayout.Orientation, "Portrait"))
#layout.setPageSize(QPageSize(QRectF(13, 20), QPageSize.Unit.Inch, "4x6 in page", QPageSize.SizeMatchPolicy.ExactMatch))
#layout.setOrientation(QPageLayout.Portrait)