phton pdf制作中reportlab.canvas参数

reportlab.canvas有这六个主要参数

1、pagesize:设置纸张大小 #from reportlab.lib.pagesizes import letter, A4 导入常见的页面大小库

#

2、bottomup:确定(0,0)点是不是在左下角

3、pageCompression:页面是否压缩

4、verbosity : 是否打印日志 0:否 1:是

5、encrypt:是否加密

i`mport io #添加文字用
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A3,letter
from reportlab.lib.pagesizes import letter, landscape
from reportlab.pdfbase import pdfmetrics #可以写入中文
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.units import cm,inch
pdfmetrics.registerFont(TTFont(‘song’, ‘simfang.ttf’)) #注册字体TTFont ('s

        packet = io.BytesIO()
        # 使用Reportlab创建一个新的PDF
        # 
        # print(landscape(A3)) #宽高(841.8897637795277, 1190.5511811023623)
        can = canvas.Canvas(packet, pagesize=landscape(A3))
        can.setFont('song',16)
        can.setFillColor('#008000')
        can.drawString(39.8*cm, 28.5*cm, str(nonums[num]))
        samepic=1
        for same in range(0,len(nonums)):
            if same==num:
                pass
            else:
                if file_path2[same]==file_path2[num]:
                    can.drawString(39.8*cm, (28.5-samepic*0.6)*cm, str(nonums[same]))
                    samepic+=1

        can.save()
        #buffer从偏移0开始
        packet.seek(0)
        new_pdf = PdfFileReader(packet)
        #读取已有的PDF
        existing_pdf = PdfFileReader(open(filelist, "rb"))
        output_temp = PdfFileWriter()
        # 
        ttpage=existing_pdf.getNumPages()
        for page_no in range(ttpage):
            page = existing_pdf.getPage(page_no)
            # print(page)
            # print(dir(page.__sizeof__))
            # print('{}page,{}height*width{}'.format(page_no,page.height,page.width))
            # print(1*cm,type(1*cm))
            if page.get('/Rotate', 0) in [90, 270]:
                print('转90-270')
                height,width= page['/MediaBox'][2]-page['/MediaBox'][0], page['/MediaBox'][3]-page['/MediaBox'][1]
            else:
                height,width=page['/MediaBox'][3]-page['/MediaBox'][1], page['/MediaBox'][2]-page['/MediaBox'][0]
            print("{}文件的高{:.2f}cm  ** 宽{:.2f} cm".format(filelist,float(height)/(1*cm),float(width)/(1*cm)))
            page.mergePage(new_pdf.getPage(0))
            output_temp.addPage(page)
            # print(dir(output_temp))
            # exit()
        outputStream = open("temp_dest"+str(num)+".pdf", "wb")
        output_temp.write(outputStream)
        outputStream.close()`

你可能感兴趣的:(phton pdf制作中reportlab.canvas参数)