python-qt5 icon indicating copy to clipboard operation
python-qt5 copied to clipboard

pyqt print to pdf page breaks

Open godomainz opened this issue 2 years ago • 3 comments

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?

godomainz avatar May 02 '22 00:05 godomainz

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

zakari1231 avatar May 02 '22 15:05 zakari1231

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

godomainz avatar May 03 '22 00:05 godomainz

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)

zakari1231 avatar May 03 '22 04:05 zakari1231