def exportToPdf(self): printer = QPrinter(QPrinter.PrinterResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPaperSize(QPrinter.A4) printer.setOutputFileName('export_sample.pdf') document = self.ui.textEdit.document() size = QSizeF(printer.paperRect().size()) document.setPageSize(size) document.print(printer) print('###')
def get_pdf_printer(opts, for_comic=False, output_file_name=None): # {{{ from calibre.gui2 import must_use_qt must_use_qt() printer = QPrinter(QPrinter.HighResolution) custom_size = get_custom_size(opts) if isosx and not for_comic: # On OSX, the native engine can only produce a single page size # (usually A4). The Qt engine on the other hand produces image based # PDFs. If we set a custom page size using QSizeF the native engine # produces unreadable output, so we just ignore the custom size # settings. printer.setPaperSize(paper_size(opts.paper_size)) else: if opts.output_profile.short_name == 'default' or \ opts.output_profile.width > 9999 or opts.override_profile_size: if custom_size is None: printer.setPaperSize(paper_size(opts.paper_size)) else: printer.setPaperSize(QSizeF(custom_size[0], custom_size[1]), unit(opts.unit)) else: w = opts.output_profile.comic_screen_size[0] if for_comic else \ opts.output_profile.width h = opts.output_profile.comic_screen_size[1] if for_comic else \ opts.output_profile.height dpi = opts.output_profile.dpi printer.setPaperSize(QSizeF(float(w) / dpi, float(h) / dpi), QPrinter.Inch) if for_comic: # Comic pages typically have their own margins, or their background # color is not white, in which case the margin looks bad printer.setPageMargins(0, 0, 0, 0, QPrinter.Point) else: printer.setPageMargins(opts.margin_left, opts.margin_top, opts.margin_right, opts.margin_bottom, QPrinter.Point) printer.setOutputFormat(QPrinter.PdfFormat) printer.setFullPage(for_comic) if output_file_name: printer.setOutputFileName(output_file_name) if isosx and not for_comic: # Ensure we are not generating enormous image based PDFs printer.setOutputFormat(QPrinter.NativeFormat) return printer