Example #1
0
    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()
Example #2
0
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()