3.文档封面设计

3.1 根据个性化信息生成封面

首先在easypdf 同级目录下,创建一个test.py文件,复制以下代码直接干;

import json
import os
from datetime import datetime
from easypdf.components import constant
from reportlab.lib.pagesizes import A4
from easypdf.core.pdf import PDF
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.platypus.tables import Table, TableStyle

from easypdf.core import doc_examples
from easypdf.core.office_examples.charts import (
    scatter as of_ex_scatter,
    quick_charts as of_ex_quick_charts,
    )
from easypdf.core.office_examples.charts import (
	bar as of_ex_bar,
	area as of_ex_area,
	pie as of_ex_pie,
    lines as of_ex_lines,
    )

BASE_DIR = os.path.dirname(__file__)
IMAGES = os.path.join(BASE_DIR, 'images', 'es_back.png')

class Report(PDF):
    """生成pdf"""

    def __init__(self):
        PDF.__init__(self,
                     # 保存pdf文档的文件名
                     filename='easypdf生成的pdf文档展示.pdf',
                     # 封面图片
                     cover_image=IMAGES,
                     # 文档大小
                     pagesize=A4,
                     # 页眉左边title
                     title=constant.PAGE_TITLE,
                     # 版权信息
                     copyrights=constant.copyright,
                     # 版本信息
                     version_info=constant.version_info,
                     )

    def cover_design(self):
        """封面设计"""
        # 封面的主题
        self.add_main_title('企鹅侠')
        # 封面显示的时间
        self.add_centred(datetime.now().strftime('%Y-%m-%d %H:%M:%S %Z'))
        # 使用目录模板
        self.next_toc_template()
        self.add_toc()
        self.next_normal_template()

# 实例化一个pdf对象        
pdf = Report()
# 封面设计
pdf.cover_design()
# 创建pdf文档
pdf.build_pdf()
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.

es_back.png:这是一个pdf的封面图片,你可以改为个人的封面或者企业的封面,我这里封面比较简单。
filename:生成pdf的文件名,各位可以根据自己业务场景进行命名
pagesize: 默认就是A4纸大小,大家不用去设置
title:标题,会显示在页眉的左边,页眉右边显示的是标题
copyrights:版权信息,大家可以根据需求修改
version_info:版本信息
next_toc_template:目录模板,后面会根据你的标题生成目录
add_toc: 添加目录内容处理模板,如目录字体设置
next_normal_template:后面使用常规模板,就是写入正文的页面