美文网首页
reportlab 生成 pdf 文件

reportlab 生成 pdf 文件

作者: woniuxia | 来源:发表于2021-08-02 11:12 被阅读0次

    环境

    • win10、python3.7、reportlab3.5.56

    代码如下:

    • 代码简单,自从参考
    # -*- coding:utf-8 -*-
    """
    File Name: gen_pdf
    Author: 82405
    Data: 2020/12/22 16:12
    -----------------------
    Info:
    
    -----------------------
    Change Activity:
        2020/12/22: create
    """
    import time
    from concurrent import futures
    from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, \
        PageBreak, Table, TableStyle
    from reportlab.lib.styles import ParagraphStyle, getSampleStyleSheet, LineStyle
    from reportlab.lib.units import mm
    from reportlab.lib import colors
    from reportlab.lib.enums import TA_CENTER, TA_LEFT
    from reportlab.pdfbase import pdfmetrics
    from reportlab.pdfbase.ttfonts import TTFont
    
    pdfmetrics.registerFont(TTFont('msyh', 'msyh.ttc'))
    pdfmetrics.registerFont(TTFont('msyhbd', 'msyhbd.ttc'))
    
    rule = [
        '1、本产品由xxxxxxxxx有限公司和天安财产保险股份有限公司联合承保,仅限在中国大陆有固定居住地(常住)的人士投保。',
        '2、本订单保障地区范围为中国大陆境内(不包含香港、澳门、台湾地区)。',
        '3、本订单投保人为经中国人民银行批准的支付机构;本保单保障被保险人名下的在投保人支付机构开具的账户。',
        '4、本订单保险期限为被保险人在平台发起交易指令时(以该行为所处时间点为准)起168小时内(含)。',
        '5、在保险期限内,被保险人通过保险单载明的非金融机构完成支付,如支付资金实际到达收款人账户的时间超过保险起期后12小时(含12小时)未到账,保险人按照本保单约定的保险金额对被保险人进行赔偿。',
        '6、本保单仅承担因支付平台本身的原因造成的支付到账延迟责任,因下列原因导致的迟延到账损失,保险人不负责赔偿:( 1) 被保险人操作失误;( 2)被保险人自身账户问题;( 3)结算银行否认交易;( 4)支付被依法认定为无效或被撤销的;( 5) 被保险人或收款人交易账户异常(详见条款释义);(6)银行系统原因、中国人民银行支付清算系统原因等其他非支付机构的系统原因。'
    ]
    
    
    def gen_pdf():
        """
    
        :return:
        """
        print('gen_def ... start...')
        story = []
        title_style = ParagraphStyle(name="TitleStyle", fontName="msyhbd", fontSize=10, alignment=TA_CENTER, )
        content_style = ParagraphStyle(name="ContentStyle",
                                       fontName="msyh",
                                       fontSize=7,
                                       alignment=TA_LEFT, )
        content_style_1 = ParagraphStyle(name="ContentStyle",
                                         fontName="msyh",
                                         fontSize=9,
                                         leftIndent=20,
                                         alignment=TA_LEFT, )
        content_style_line = ParagraphStyle(name="ContentStyle",
                                            fontName="msyh",
                                            fontSize=9,
                                            underlineOffset=-3,
                                            alignment=TA_LEFT, )
        content_style_line_1 = ParagraphStyle(name="ContentStyle",
                                              fontName="msyh",
                                              fontSize=7,
                                              underlineOffset=-3,
                                              alignment=TA_LEFT, )
        task_data = [['保险责任', '明细', ''],
                     ['非金融机构支付服务安全保险', '累计赔偿限额', '200元'],
                     ['非金融机构支付服务延迟保险', '累计赔偿限额', '200元']]
        basic_style = TableStyle([('FONTNAME', (0, 0), (-1, -1), 'msyh'),
                                  ('FONTSIZE', (0, 0), (-1, -1), 7),
                                  ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
                                  ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
                                  ('BOTTOMPADDING', (0, 0), (-1, -1), 4),
                                  # 'SPAN' (列,行)坐标
                                  ('SPAN', (1, 0), (2, 0)),
                                  ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
                                  ])
        story.append(Spacer(1, 10 * mm))
        story.append(Paragraph("订单信息", title_style))
        story.append(Spacer(1, 5 * mm))
        story.append(Paragraph("订单类型: 非金融机构支付服务保险", content_style))
        story.append(Spacer(1, 2 * mm))
        story.append(Paragraph("订单编号: <u> {} </u>".format(' 202020020000000000 '), content_style))
        story.append(Spacer(1, 2 * mm))
        story.append(Paragraph("鉴于投保人已向保险人递交投保申请及附件, 并同意按约定交纳保险费, 本公司特提供以下的产品订单信息, 投", content_style_1))
        story.append(Spacer(1, 2 * mm))
        story.append(
            Paragraph("<u>保人可于当晚前往天安财险官网(www.95505.cn) 查询详细的凭证信息。 {} 币种:(人民币/元)</u>".format('&nbsp' * 30),
                      content_style_line))
        story.append(Spacer(1, 2 * mm, isGlue=True))
        story.append(
            Paragraph("投保人: 投保公司 &nbsp&nbsp&nbsp 证件类型:组织机构代码 &nbsp&nbsp&nbsp 证件号码: 130181198510027123", content_style))
        story.append(Paragraph("投保人住所: 上海市虹口区四川北路", content_style))
        story.append(
            Paragraph("被保险人: 张三李四王五 &nbsp&nbsp&nbsp 证件类型: 居民身份证  &nbsp&nbsp&nbsp证件号码: 342624199004132376", content_style))
        story.append(Paragraph("受益人&nbsp&nbsp&nbsp: 张三李四王五 &nbsp&nbsp&nbsp 证件类型: 居民身份证  &nbsp&nbsp&nbsp "
                               "证件号码: 342624199004132376  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp 受益比例: 100%", content_style))
        story.append(Paragraph("保险期间:自北京时间起  2020-12-22 18:00:00 至  2020-12-24 18:0:00 时止", content_style))
        story.append(Paragraph("支付时间:  2020-12-22 18:00:00 约定到账周期: 3 天", content_style))
        story.append(Paragraph("约定到账时间:  2020-12-22 支付方式: 银行汇款 ", content_style))
        story.append(Paragraph("支付对象:  张三李四王五", content_style))
        task_table = Table(task_data, colWidths=[75 * mm, 50 * mm, 40 * mm], rowHeights=8 * mm, style=basic_style)
        story.append(task_table)
        story.append(Paragraph("份数: 1份 {} 被保险人数: 1人".format('&nbsp' * 100), content_style))
        story.append(Paragraph("保险金额总计: (大写)&nbsp&nbsp 贰佰 元 {}小写 {} 200 元".
                               format('&nbsp' * 136, '&nbsp' * 10), content_style))
        story.append(Paragraph("<u>保险费总计&nbsp&nbsp&nbsp: (大写)&nbsp&nbsp 贰佰 元 {} 小写 {} 200 元</u>".
                               format('&nbsp' * 136, '&nbsp' * 10), content_style_line_1))
        story.append(Paragraph("伤残表适用:", content_style))
        story.append(Paragraph("条款适用: 天安财产保险股份有限公司《非金融机构支付服务保险条款》", content_style_line_1))
        story.append(Paragraph(
            "<u>_____________________________________________________________________________________________________________________________________________________</u>",
            content_style_line_1))
        story.append(Paragraph("特别约定:", content_style))
        for item in rule:
            story.append(Paragraph(item, content_style))
        story.append(Paragraph(
            "<u>_____________________________________________________________________________________________________________________________________________________</u>",
            content_style_line_1))
        story.append(Paragraph('保险合同争议解决方式: 诉讼 付款方式:趸缴', content_style))
        doc = SimpleDocTemplate('hello_1.pdf',
                                leftMargin=20 * mm, rightMargin=20 * mm, topMargin=2 * mm, bottomMargin=20 * mm)
        doc.build(story)
        print('...gen_pdf...exit')
    
    
    gen_pdf()
    
    
    • 生成文件如下
    image.png

    如果对你有帮助,可以点赞支持一下。

    相关文章

      网友评论

          本文标题:reportlab 生成 pdf 文件

          本文链接:https://www.haomeiwen.com/subject/tewdvltx.html