Skip to content
Snippets Groups Projects

I173 print help doc in pdf

Merged Lan Dam requested to merge i173_print_help_doc_in_pdf into develop
import sys
from pathlib import Path
from typing import Tuple, Callable, Union, List, Dict, Optional
from PIL import Image
from PySide6 import QtCore, QtGui, QtWidgets
from PySide6.QtPrintSupport import QPrinter
@@ -108,7 +109,11 @@ class HelpBrowser(QtWidgets.QWidget):
"""
self.search_results_path: Path = self.docdir_path.joinpath(
'Search Results.md')
"""
default_pdf_file_name: the default name for pdf file when the current
help document is saved
"""
self.default_pdf_file_name: str = ''
# Get screen dimensions
geom = QtGui.QGuiApplication.screens()[0].availableGeometry()
self.setGeometry(10, 10,
@@ -373,10 +378,46 @@ class HelpBrowser(QtWidgets.QWidget):
Load file from file_path to help_view
:param file_path: absolute path to a document
:type file_path: str
"""
url = QtCore.QUrl.fromLocalFile(file_path)
self.help_view.setSource(url)
self._check_image_size(file_path)
self._set_default_pdf_file_name(file_path)
def _set_default_pdf_file_name(self, file_path: str) -> str:
"""
Set the default_pdf_file_name for the current help document
:param file_path: absolute path to a document
"""
file_name = file_path.split(' _ ')[1]
file_name = "SOHViewer Help - " + file_name
self.default_pdf_file_name = file_name.replace('.help.md', '')
print("default:", self.default_pdf_file_name)
def _check_image_size(self, file_path: str) -> None:
"""
Checking all image in the given file to give a warning if there is any
image of which width is greater than 1584px which is the limit of being
cut off when saving the file to pdf
:param file_path: path to the help document
"""
with open(file_path) as f:
lines = f.readlines()
for line in lines:
s = line.split('src="')
if len(s) == 2:
img_path = s[1].split('"')[0]
abs_img_path = self.docdir_path.joinpath(img_path)
img = Image.open(abs_img_path)
if img.width > 1584:
msg = (f"Image {abs_img_path}'s\n\nwidth is "
f"{img.width}px which is GREATER than 1584px,"
"\n\nwhich results in some cut off of the "
"image when saving this help document to file.")
QtWidgets.QMessageBox.warning(
self, 'Oversize image', msg)
@QtCore.Slot()
def on_tree_view_item_clicked(self, index: QtCore.QModelIndex):
@@ -541,15 +582,22 @@ class HelpBrowser(QtWidgets.QWidget):
def save_to_pdf(self):
"""
Save the current document to pdf file.
A3 size is selected because the current font look well in that. After
the document is saved, user can print it in the size they want. The
size of A3 is 11.7 x 16.5. So the width of pictures in the document
will be cut off if greater than (11.7 - margins).
Note: the pictures' width in the document must be smaller than 11 in/
1584px or its content will be cut off.
"""
home_path = Path.home()
doc_path = home_path.joinpath("Documents")
save_file = QtWidgets.QFileDialog.getSaveFileName(self, 'Save to PDF',
doc_path.as_posix())
if save_file:
filename = save_file[0]
file_path = doc_path.joinpath(self.default_pdf_file_name)
save_file = QtWidgets.QFileDialog.getSaveFileName(
self, 'Save to PDF', file_path.as_posix())[0]
if save_file != "":
filename = save_file
if not filename.endswith('.pdf'):
filename = filename + '.pdf'
printer = QPrinter(
@@ -560,6 +608,8 @@ class HelpBrowser(QtWidgets.QWidget):
printer.setOutputFormat(QPrinter.OutputFormat.PdfFormat)
printer.setOutputFileName(filename)
self.help_view.document().print_(printer)
msg = f"The current help document has been saved at {filename}"
QtWidgets.QMessageBox.information(self, "Document Saved", msg)
def main():
Loading