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)
def savePdf(self,fileName): pt = QPdfWriter(fileName) # pt.logicalDpiX pt.setPageSize(QPagedPaintDevice.A4) # print('resolution',pt.resolution()) pt.setResolution(10000) # print('resolution',pt.resolution()) textDocument = QTextDocument() textDocument.setHtml(self.pcontext.thtmlGet()) textDocument.print_(pt) textDocument.end() print('pdf creat = ',fileName)
def writePdf(self, name): pdfFile = QFile(name) #打开要写入的pdf文件 pdfFile.open(QIODevice.WriteOnly) #创建pdf写入器 pPdfWriter = QPdfWriter(pdfFile) #设置纸张为A4 pPdfWriter.setPageSize(QPagedPaintDevice.A4) #设置纸张的分辨率为300,因此其像素为3508X2479 pPdfWriter.setResolution(300) pPdfWriter.setPageMargins(QMarginsF(60, 60, 60, 60)) pPdfPainter = QPainter(pPdfWriter) # 标题上边留白 iTop = 100 #文本宽度2100 iContentWidth = 2100 # 标,22号字 font = QFont() font.setFamily("simhei.ttf") fontSize = 22 font.setPointSize(fontSize) pPdfPainter.setFont(font) pPdfPainter.drawText(QRect(0, iTop, iContentWidth, 90), Qt.AlignHCenter, "我是标题我骄傲") # 内容,16号字,左对齐 fontSize = 16 font.setPointSize(fontSize) pPdfPainter.setFont(font) iTop += 90 pPdfPainter.drawText(QRect(0, iTop, iContentWidth, 60), Qt.AlignLeft, "1、目录一") iTop += 90 # 左侧缩进2字符 iLeft = 120 pPdfPainter.drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60), Qt.AlignLeft, "我的目录一的内容。") iTop += 90 pPdfPainter.drawText(QRect(0, iTop, iContentWidth, 60), Qt.AlignLeft, "2、目录二") iTop += 90 pPdfPainter.drawText(QRect(iLeft, iTop, iContentWidth - iLeft, 60), Qt.AlignLeft, "我的目录一的内容") pPdfPainter.end() pdfFile.close()
def save_pdf(self, file): wr = QPdfWriter(str(file)) wr.setResolution(300) wr.newPage() wr.setPageSize(QPagedPaintDevice.A4) self.on_paint_request(wr)
class CardPDFWriter: RESOLUTION = 300 # 300dpi @staticmethod def mm2pix(args): return [x * (CardPDFWriter.RESOLUTION / 25.4) for x in args] def __init__(self, file_name, card_format, paper_format, separation=[0.8, 0.8]): self.cardFormat = self.mm2pix(card_format) self.paperFormat = self.mm2pix(paper_format) self.separation = self.mm2pix(separation) self.paperFormatMM = paper_format self.file = QFile(str(file_name)) self.file.open(QIODevice.WriteOnly) self.writer = QPdfWriter(self.file) self.writer.setResolution(self.RESOLUTION) self.writer.setPageSizeMM(QSizeF(*self.paperFormatMM)) self.writer.setPageMargins(QMarginsF(0, 0, 0, 0)) self.painter = QPainter(self.writer) self.pen = QPen() self.pen.setWidth(self.mm2pix([1])[0]) self.painter.setPen(self.pen) self.bleeding = [0, 0] self.bleeding[0] = (self.paperFormat[0] % int(self.cardFormat[0] + self.separation[0])) / 2 self.bleeding[1] = (self.paperFormat[1] % int(self.cardFormat[1] + self.separation[1])) / 2 self.cursor = self.bleeding[:] self._setupPage() def _setupPage(self): # self.writer.setPageSizeMM(QSizeF(*self.paperFormatMM)) # self.writer.setPageMargins(QMarginsF(0, 0, 0, 0)) # Horizontal lines pos = self.bleeding[0] - self.separation[0] / 2 while (pos < self.paperFormat[0]): self.painter.drawLine(pos, 0, pos, self.paperFormat[1]) pos += self.cardFormat[0] + self.separation[0] # Vertical lines pos = self.bleeding[1] - self.separation[1] / 2 while (pos < self.paperFormat[1]): self.painter.drawLine(0, pos, self.paperFormat[0], pos) pos += self.cardFormat[1] + self.separation[1] @assert_file_open def addPage(self): self.writer.newPage() self._setupPage() @assert_file_open def addCard(self, card, num_copies): for _ in range(num_copies): if self.cursor[1] > (self.paperFormat[1] - self.bleeding[1] - self.cardFormat[1]): self.cursor = self.bleeding[:] self.addPage() self.painter.drawPixmap(*self.cursor, *self.cardFormat, card) self.cursor[0] += self.cardFormat[0] + self.separation[0] if self.cursor[0] > (self.paperFormat[0] - self.bleeding[0] - self.cardFormat[0]): self.cursor[0] = self.bleeding[0] self.cursor[1] += self.cardFormat[1] + self.separation[1] @assert_file_open def close(self): self.painter.end() self.file.flush() self.file.close() def isOpen(self): return self.file.isOpen()