def __init__(self, filePath, parent=None): """ Constructor @param filePath path of the file to write into @type str @param parent reference to the parent widget @type QWidget """ super(PrintToPdfDialog, self).__init__(parent) self.setupUi(self) self.pdfFilePicker.setMode(E5PathPickerModes.SaveFileOverwriteMode) self.pdfFilePicker.setFilters( self.tr("PDF Files (*.pdf);;" "All Files (*)")) if not os.path.isabs(filePath): documentsPath = QStandardPaths.writableLocation( QStandardPaths.DocumentsLocation) if documentsPath: filePath = os.path.join(documentsPath, filePath) else: filePath = os.path.abspath(filePath) self.pdfFilePicker.setText(filePath, toNative=True) self.__currentPageLayout = QPageLayout(QPageSize(QPageSize.A4), QPageLayout.Portrait, QMarginsF(0.0, 0.0, 0.0, 0.0)) self.__updatePageLayoutLabel()
def printPDF(url, margins): app = QtWidgets.QApplication(sys.argv) loader = QtWebEngineWidgets.QWebEngineView() loader.setZoomFactor(1) loader.load(QtCore.QUrl(url)) layout = QPageLayout( QPageSize(QPageSize.A4), QPageLayout.Portrait, QMarginsF(margins[0], margins[1], margins[2], margins[3])) def printFinished(): page = loader.page() page.profile().clearHttpCache() log("%s Printing Finished!" % page.title()) app.exit() def printToPDF(finished): loader.show() page = loader.page() page.printToPdf("./pdfs/%s.pdf" % page.title(), layout) loader.page().pdfPrintingFinished.connect(printFinished) loader.loadFinished.connect(printToPDF) app.exec_()
def load_finished(self): if self.do_preview: self.show() else: pageLayout = QPageLayout(QPageSize(QPageSize.A5), QPageLayout.Portrait, QMarginsF(0, 0, 0, 0)) self.page().printToPdf(self.filename, pageLayout) self.page().pdfPrintingFinished.connect(on_pdf_finished)
def loadFinished(self, ok): if ok: self.status_bar.showMessage("Printing PDF") self.page.printToPdf(self.filename, pageLayout=QPageLayout( QPageSize(QPageSize.A5), QPageLayout.Portrait, QMarginsF(30.0, 30.0, 30.0, 30.0))) else: self.status_bar.showMessage("There was an error printing the PDF")
def get_painter(self, printer) -> QPainter: layout = QPageLayout() layout.setPageSize(QPageSize(QPageSize.A4)) layout.setOrientation(QPageLayout.Portrait) printer.setPageLayout(layout) printer.setResolution(self.resolution) painter = QPainter() self.painter = painter return painter
def __post_init__(self): # Scaling ratio for Qt point 72dpi -> constants.PRINT_DPI ratio = 72 / constants.PRINT_DPI super().__setattr__( "qt_object", QPageLayout( QPageSize( QSize( int(self.width.base_value * ratio), int(self.height.base_value * ratio), ), ), QPageLayout.Portrait, # Margins are implemented at a higher level QMarginsF(0, 0, 0, 0), ), )
def generate_pdf(df_data): app = QtWidgets.QApplication(sys.argv) list_globalid = list(set(df_data['Global ID'].to_list())) print(list_globalid) for gid in list_globalid: html_file = 'P' + str(gid) + '.html' df_temp = df_data[df_data['Global ID'] == gid] df_temp.columns = set_columns() th_props = [('font-size', '8px'), ('text-align', 'center'), ('font-weight', 'bold'), ('color', '#6d6d6d'), ('background-color', '#f7f7f9')] td_props = [('font-size', '8px')] pd.set_option('colheader_justify', 'center') # FOR TABLE <th> df_temp.to_html(html_file, index=False, justify='left', border="1") loader = QtWebEngineWidgets.QWebEngineView() loader.load(QtCore.QUrl(get_path() + html_file)) layout = QPageLayout(QPageSize(QPageSize.A4), QPageLayout.Portrait, QMarginsF(8, 12, 8, 12)) def printFinished(): page = loader.page() # print("%s Printing Finished!" % page.title()) app.exit() def printToPDF(finished): loader.show() page = loader.page() page.printToPdf("%s.pdf" % page.title()[:9], layout) print("Generated pdf file %s.pdf" % page.title()[:9]) loader.page().pdfPrintingFinished.connect(printFinished) loader.loadFinished.connect(printToPDF) app.exec_() loader.close() app.closeAllWindows()
def savePdf(self): fileName = QFileDialog.getSaveFileName(self, self.tr("Export document to PDF"), self.currentTab.getBaseName() + ".pdf", self.tr("PDF files (*.pdf)"))[0] if fileName: if not QFileInfo(fileName).suffix(): fileName += ".pdf" title, htmltext, preview = self.currentTab.getDocumentForExport() if globalSettings.useWebEngine and hasattr(preview.page(), "printToPdf"): pageSize = self.getPageSizeByName(globalSettings.paperSize) if pageSize is None: pageSize = QPageSize(QPageSize.A4) margins = QMarginsF(20, 20, 13, 20) # left, top, right, bottom (in millimeters) layout = QPageLayout(pageSize, QPageLayout.Portrait, margins, QPageLayout.Millimeter) preview.page().printToPdf(fileName, layout) # Available since Qt 5.7 return printer = self.standardPrinter(title) printer.setOutputFormat(QPrinter.PdfFormat) printer.setOutputFileName(fileName) document = self.getDocumentForPrint(title, htmltext, preview) if document != None: document.print(printer)
class PrintToPdfDialog(QDialog, Ui_PrintToPdfDialog): """ Class implementing a dialog to enter the data for printing a web page to PDF. """ def __init__(self, filePath, parent=None): """ Constructor @param filePath path of the file to write into @type str @param parent reference to the parent widget @type QWidget """ super(PrintToPdfDialog, self).__init__(parent) self.setupUi(self) self.pdfFilePicker.setMode(E5PathPickerModes.SaveFileOverwriteMode) self.pdfFilePicker.setFilters( self.tr("PDF Files (*.pdf);;" "All Files (*)")) if not os.path.isabs(filePath): documentsPath = QStandardPaths.writableLocation( QStandardPaths.DocumentsLocation) if documentsPath: filePath = os.path.join(documentsPath, filePath) else: filePath = os.path.abspath(filePath) self.pdfFilePicker.setText(filePath, toNative=True) self.__currentPageLayout = QPageLayout(QPageSize(QPageSize.A4), QPageLayout.Portrait, QMarginsF(0.0, 0.0, 0.0, 0.0)) self.__updatePageLayoutLabel() @pyqtSlot() def on_pageLayoutButton_clicked(self): """ Private slot to define the page layout. """ printer = QPrinter() printer.setPageLayout(self.__currentPageLayout) dlg = QPageSetupDialog(printer, self) if dlg.exec_() == QDialog.Accepted: self.__currentPageLayout = printer.pageLayout() self.__updatePageLayoutLabel() def __updatePageLayoutLabel(self): """ Private method to update the page layout label. """ if self.__currentPageLayout.orientation() == QPageLayout.Portrait: orientation = self.tr("Portrait") else: orientation = self.tr("Landscape") self.pageLayoutLabel.setText( self.tr("{0}, {1}", "page size, page orientation").format( self.__currentPageLayout.pageSize().name(), orientation)) def getData(self): """ Public method to get the dialog data. @return tuple containing the file path and the page layout @rtype tuple of str and QPageLayout """ return ( self.pdfFilePicker.text(toNative=True), self.__currentPageLayout, )