Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pyqt print to pdf page breaks #70

Open
godomainz opened this issue May 2, 2022 · 3 comments
Open

pyqt print to pdf page breaks #70

godomainz opened this issue May 2, 2022 · 3 comments

Comments

@godomainz
Copy link

godomainz commented May 2, 2022

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?

@zakari1231
Copy link

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

@godomainz
Copy link
Author

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

@zakari1231
Copy link

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants