使用PILLOW 将文字列表转为PDF

from PIL import Image, ImageDraw, ImageFont

def img_txt(head_text,file_list):
    width, height = 2480, 3508
    image = Image.new('RGB', (width, height), color = (255, 255, 255))
    draw = ImageDraw.Draw(image)
    font = ImageFont.truetype("/usr/share/fonts/simsun.ttc", 120)
    font2 = ImageFont.truetype("/usr/share/fonts/simsun.ttc", 80)
    head_text="关于文件《"+head_text+"》的附件"
    head_num=len(head_text)
    head_line=head_num//20+1 if head_num%20>0 else head_num//20
    for i in range(head_line):
        text_position = (20, 20+i*120)  # 文字的位置
        draw.text(text_position, head_text[i*20:i*20+20], font=font, fill=(0, 0, 0))
    start= 180+i*120
    j=0
    for file_one in file_list:
        one=str(j+1)+"."+file_one
        one_num=len(one)
        one_line=one_num//30+1 if one_num%30>0 else one_num//30
        x=0
        for x in range(one_line):
            if x>0:
                j=j+1
                text_position = (120, start+j*100)
            else:
                text_position = (80, start+j*100)  # 文字的位置
            draw.text(text_position, one[x*30:x*30+30], font=font2, fill=(0, 0, 0))
            x=x+1
        j=j+1
    image.save('hello.pdf',"PDF",resolution=300)

1.A4 分辨率为300DPI下2480×3508,需要再保存的时候设置DPI,resolution=300

发表回复