DEV Community

Adam
Adam

Posted on

PDF's in PySide6

PySide6 allow for us as a developers to show an pdf file in a window or in a Widget.

And to do this, the first thing we need is to import the necessary libraires

from PySide6.QtWidgets import QVBoxLayout, QWidget
from PySide6.QtPdf import QPdfDocument
from PySide6.QtPdfWidgets import QPdfView
from PySide6.QtGui import QIcon
from PySide6.QtCore import QPointF
Enter fullscreen mode Exit fullscreen mode

The QPdfDocument is your Pdf document
And the QPdfView is what will show your document

First thing we need is to upload the PDF document

self.book = QPdfDocument(self)
self.book.load(Your PDF path)
Enter fullscreen mode Exit fullscreen mode

And then show it:

self.book_view = QPdfView(self)
self.book_view.setDocument(self.book)
Enter fullscreen mode Exit fullscreen mode

And literally that's it
But we still also have another options and things to do, for example:

self.book_view.setPageMode(QPdfView.PageMode.MultiPage)
And this will make the page mode is Multiple witch is we can see the full document and we can scroll between all pages

And we also have another modes like:
self.book_view.setPageMode(QPdfView.PageMode.SinglePage)
And we can also set the zoom of the document

with all of these options:

self.book_view.setZoomMode(QPdfView.ZoomMode.Custom)
self.book_view.setZoomMode(QPdfView.ZoomMode.FitToWidth)
self.book_view.setZoomMode(QPdfView.ZoomMode.FitInView)
Enter fullscreen mode Exit fullscreen mode

And for every one it's own purpose
And for more informations, i encourge you so much to visit:

And we can also jump into pages using:

self.book_view.pageNavigator().jump(self.current_page - 1, QPointF(0, 0), self.book_view.zoomFactor())
Enter fullscreen mode Exit fullscreen mode

Top comments (0)