def print_to_pdf(self, filename): """ Saves page as a pdf file. See qt4 QPrinter documentation for more detailed explanations of options. :param filename: The destination path. """ # TODO: read these from notebook metadata? args? paper_size = (8.5, 11.0) paper_margins = (0, 0, 0, 0) paper_units = QPrinter.Inch resolution = 1200 printer = QPrinter(QPrinter.HighResolution) printer.setColorMode(QPrinter.Color) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPageMargins(*(paper_margins + (paper_units,))) printer.setPaperSize(QtCore.QSizeF(*paper_size), paper_units) printer.setResolution(resolution) printer.setFullPage(True) printer.setOutputFileName(filename) # get some sizes for calculations nb_width, nb_height = self.selector_size("#notebook") # make the screen really long to fit the notebook self.session.page.setViewportSize( QtCore.QSize(VIEWPORT[0], nb_height + 40) ) body_width, body_height = self.selector_size("body") # calculate the native size ratio = paper_size[0] / body_width # make the page really long to fit the notebook printer.setPaperSize( QtCore.QSizeF(paper_size[0], nb_height * ratio), paper_units) painter = QPainter(printer) # this is a dark art painter.scale(8, 8) self.session.main_frame.render(painter) painter.end()
def print_to_pdf(self, filename): """ Saves page as a pdf file. See qt4 QPrinter documentation for more detailed explanations of options. :param filename: The destination path. """ # TODO: read these from notebook metadata? args? paper_size = (8.5, 11.0) paper_margins = (0, 0, 0, 0) paper_units = QPrinter.Inch resolution = 1200 printer = QPrinter(QPrinter.HighResolution) printer.setColorMode(QPrinter.Color) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPageMargins(*(paper_margins + (paper_units, ))) printer.setPaperSize(QtCore.QSizeF(*paper_size), paper_units) printer.setResolution(resolution) printer.setFullPage(True) printer.setOutputFileName(filename) # get some sizes for calculations nb_width, nb_height = self.selector_size("#notebook") # make the screen really long to fit the notebook self.session.page.setViewportSize( QtCore.QSize(VIEWPORT[0], nb_height + 40)) body_width, body_height = self.selector_size("body") # calculate the native size ratio = paper_size[0] / body_width # make the page really long to fit the notebook printer.setPaperSize(QtCore.QSizeF(paper_size[0], nb_height * ratio), paper_units) painter = QPainter(printer) # this is a dark art painter.scale(8, 8) self.session.main_frame.render(painter) painter.end()
def screenshot(self, filename): """ do an individual slide screenshot big thanks to https://gist.github.com/jmaupetit/4217925 """ printer = QPrinter(mode=QPrinter.ScreenResolution) printer.setOutputFormat(QPrinter.PdfFormat) printer.setPaperSize(QtCore.QSizeF(*reversed(VIEWPORT)), QPrinter.DevicePixel) printer.setOrientation(QPrinter.Landscape) printer.setOutputFileName(filename) printer.setPageMargins(0, 0, 0, 0, QPrinter.DevicePixel) painter = QPainter(printer) painter.scale(1.45, 1.45) self.session.main_frame.render(painter) painter.end()