def pdf(self, filename, rect=None, resolution=72.0, paperColor=None): """Create a PDF file for the selected rect or the whole page. The filename may be a string or a QIODevice object. The rectangle is relative to our top-left position. Normally vector graphics are rendered, but in cases where that is not possible, the resolution will be used to determine the DPI for the generated rendering. """ # map to the original page source = self.pageRect() if rect is None else self.mapFromPage().rect( rect) # scale to target size w = source.width() * self.scaleX h = source.height() * self.scaleY if self.computedRotation & 1: w, h = h, w targetSize = QSizeF(w, h) pdf = QPdfWriter(filename) pdf.setCreator("qpageview") pdf.setResolution(int(resolution)) layout = pdf.pageLayout() layout.setMode(layout.FullPageMode) layout.setPageSize( QPageSize(targetSize * 72.0 / self.dpi, QPageSize.Point)) pdf.setPageLayout(layout) return self.output(pdf, source, paperColor)
def pdf(filename, pageList, resolution=72, paperColor=None): """Export the pages in pageList to a PDF document. filename can be a string or any QIODevice. Normally vector graphics are rendered, but in cases where that is not possible, the resolution will be used to determine the DPI for the generated rendering. The computedRotation attribute of the pages is used to determine the rotation. Make copies of the pages if you run this function in a background thread. """ pdf = QPdfWriter(filename) pdf.setCreator("qpageview") pdf.setResolution(resolution) for n, page in enumerate(pageList): # map to the original page source = page.pageRect() # scale to target size w = source.width() * page.scaleX h = source.height() * page.scaleY if page.computedRotation & 1: w, h = h, w targetSize = QSizeF(w, h) if n: pdf.newPage() layout = pdf.pageLayout() layout.setMode(layout.FullPageMode) layout.setPageSize( QPageSize(targetSize * 72.0 / page.dpi, QPageSize.Point)) pdf.setPageLayout(layout) # TODO handle errors? page.output(pdf, source, paperColor)