美文网首页
学习利用ReportLab生成PDF报表 -- RML基础(Th

学习利用ReportLab生成PDF报表 -- RML基础(Th

作者: 蒋狗 | 来源:发表于2017-01-05 16:45 被阅读0次

    以下内容均参考RML User Guide文档(部分为译文,还有一份RML for idiots文档更适合入门),文档中的Demo都在 https://www.reportlab.com/documentation/rml-samples/

    1. RML简介
    What is RML?

    RML is the Report Markup Language - a member of the XML family of languages, and the XML dialect used
    by rml2pdf to produce documents in Adobe's Portable Document Format (PDF).

    在RML文件编写的同时可以嵌套很多语言Python、Perl等其他语言。利用RML中的标签来编写如同编写HTML一样,比调用API来生成PDF简单的多。

    1. 页面及页面结构
    • XML语法和RML:描述了一些XML和RML语法注意点。
    • prolog: 即每个RML文档的头部。
      • XML声明,非必要,但是推荐
    <?xml version="1.0" encoding="utf-8"?> 
    
    * 文档类型定义 rml.dtd
    
    <!DOCTYPE document SYSTEM "rml.dtd">
    
    * 紧随prolog,<document></document>,filename参数是必须的,除filename外还有compression,invariant,debug参数。
    
    <document filename="hello.pdf">
    ...
    </document>
    
    • Document forms
      <stylesheet></stylesheet>用来定义一些所需的样式(内容可以为空)。
      • stylesheet/pageDrawing: 最基本,最简单。每个<pageDrawing></pageDrawing>都代表一页。
      • template/stylesheet/story: 功能更强大。
    • a template:
      The template tells rml2pdf what should be on the page: headers, footers, any graphic elements you use as a
      background.
    • a stylesheet: The stylesheet is where the styles for a document are set. This tells the parser what fonts to use for paragraphs
      and paragraph headers, how to format tables and other things of that nature.
    • a story: The story is where the "meat" of the document is. Just like in a newspaper, the story is the bit you want people to
      read, as opposed to design elements or page markup. As such, this is where headers, paragraphs and the actual
      text is contained.
    1. 功能、格式等实在太多,暂只对于必要的常用功能、标签说明。
    • <template>: pageSize: 页面大小,默认大小为A4(21cm, 29.7cm)(595, 842)rotation: 旋转角度(需要是90的倍数);leftMarginrightMargintopMarginbottomMargin:上下左右偏移;showBoundary:边框显示(0/1);更多可以 产看API。对于每个<template></template>标签中,可以存在多个<pageTemplate>标签,利用其id设置每一页的模板结构。

    • <pageTemplate>: 必须有一个id属性作为别名,利用地定义的不同的id能够为不同的页面设置模板结构;在<pageTemplate>中可以重写<template>中的pageSizerotation属性。

    • <Frame><nextFrame/><Frame>存在于<pageTemplate></pageTemplage>中,且必须有一个id。如<frame id="common_frame" x1="35" y1="45" width="525" height="590"/>x1y1是相对于远点的偏移量,widthheight是控制大小。在同个<pageTemplate></pageTemplage>中可以存在多个<Frame>标签将该页分成多个模块的布局。利用<nextFrame/>可以强制将接下来的文本置入下一个<Frame>的布局中,需要在<para></para>外使用;若像<nextFrame name='id'/>增加了一个name参数,则接下来的文本将会被置入name所对应的<Frame>中(<setNextFrame/>类似)。

    • <setNextTemplate name=''/>:与<setNextFrame name=''/>类似,可以用来指定下一页的模板结构。如需要需要换页时:

    <setNextTemplate name='nextTemplate'/>
    <nextPage/>
    <para> frame 1</para>
    <nextFrame/>
    <nextFrame name='common_frame_7'/>
    <para> frame 4</para>
    
    • <condPageBreak>:设置高度height(必须且只有一个属性),若该页面余下空间足够就在余下空间填充,否则则在下个页面。

    • <storyPlace>:注明位置的悬浮着的区域(能够不受<Frame>的限制)。

    • 列表List <ol><ul><li>:类似于HTML中的同种标签,可以通过bulletColorbulletFontNamebulletFontSize等设置颜色,字体和缩进等。<ol>有序列表可以通过设置bullet的值来指定序号类型:

    In ordered lists, you can use the following types of enumeration in the bulletType attribute:

    • 'I': Roman numerals (capitals)
    • 'i': Roman numerals (lower case)
    • '1': Arabic numerals
    • 'A': Capital letters
    • 'a': Lowercase letters
    ```<ul>```无序列表可以通过在```<ul>```或```<li>```中设置```value```的值来指定形状(自己尝试在```<ul>```中添加```value```属性但没有成功,报错):
    

    Unordered lists can use bullet types of the following shapes by setting the 'value' attribute in <ul> or <li> tags:

    • square
    • disc
    • diamond
    • arrowhead
    • <docinit>: 在文档顶部定义,用来注册字体,颜色,设置页面布局,裁剪线等。如:
    <docinit useCropMarks="1" pageLayout="OneColumn" pageMode="UseThumbs">
             <!--注册字体-->
              <registerTTFont faceName="song" fileName="STSONG.TTF"/>
              <color id="BLACK" CMYK="[0,0,0,1]"/>
    </docinit>
    
    • <pageNumber/>:利用该标签可以获得当前页。

    相关文章

      网友评论

          本文标题:学习利用ReportLab生成PDF报表 -- RML基础(Th

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