學(xué)習(xí)利用ReportLab生成PDF報(bào)表 -- RML基礎(chǔ)(The Basics)

以下內(nèi)容均參考RML User Guide文檔(部分為譯文,還有一份RML for idiots文檔更適合入門),文檔中的Demo都在 https://www.reportlab.com/documentation/rml-samples/

  1. RML簡(jiǎn)介
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文件編寫的同時(shí)可以嵌套很多語(yǔ)言Python、Perl等其他語(yǔ)言。利用RML中的標(biāo)簽來(lái)編寫如同編寫HTML一樣,比調(diào)用API來(lái)生成PDF簡(jiǎn)單的多。

  1. 頁(yè)面及頁(yè)面結(jié)構(gòu)
  • XML語(yǔ)法和RML:描述了一些XML和RML語(yǔ)法注意點(diǎn)。
  • prolog: 即每個(gè)RML文檔的頭部。
    • XML聲明,非必要,但是推薦
<?xml version="1.0" encoding="utf-8"?> 
* 文檔類型定義 rml.dtd
<!DOCTYPE document SYSTEM "rml.dtd">
* 緊隨prolog,<document></document>,filename參數(shù)是必須的,除filename外還有compression,invariant,debug參數(shù)。
<document filename="hello.pdf">
...
</document>
  • Document forms
    <stylesheet></stylesheet>用來(lái)定義一些所需的樣式(內(nèi)容可以為空)。
    • stylesheet/pageDrawing: 最基本,最簡(jiǎn)單。每個(gè)<pageDrawing></pageDrawing>都代表一頁(yè)。
    • template/stylesheet/story: 功能更強(qiáng)大。
  • 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. 功能、格式等實(shí)在太多,暫只對(duì)于必要的常用功能、標(biāo)簽說(shuō)明。
  • <template>: pageSize: 頁(yè)面大小,默認(rèn)大小為A4(21cm, 29.7cm)(595, 842);rotation: 旋轉(zhuǎn)角度(需要是90的倍數(shù));leftMargin、rightMargin、topMarginbottomMargin:上下左右偏移;showBoundary:邊框顯示(0/1);更多可以 產(chǎn)看API。對(duì)于每個(gè)<template></template>標(biāo)簽中,可以存在多個(gè)<pageTemplate>標(biāo)簽,利用其id設(shè)置每一頁(yè)的模板結(jié)構(gòu)。

  • <pageTemplate>: 必須有一個(gè)id屬性作為別名,利用地定義的不同的id能夠?yàn)椴煌捻?yè)面設(shè)置模板結(jié)構(gòu);在<pageTemplate>中可以重寫<template>中的pageSizerotation屬性。

  • <Frame><nextFrame/><Frame>存在于<pageTemplate></pageTemplage>中,且必須有一個(gè)id。如<frame id="common_frame" x1="35" y1="45" width="525" height="590"/>x1、y1是相對(duì)于遠(yuǎn)點(diǎn)的偏移量,width、height是控制大小。在同個(gè)<pageTemplate></pageTemplage>中可以存在多個(gè)<Frame>標(biāo)簽將該頁(yè)分成多個(gè)模塊的布局。利用<nextFrame/>可以強(qiáng)制將接下來(lái)的文本置入下一個(gè)<Frame>的布局中,需要在<para></para>外使用;若像<nextFrame name='id'/>增加了一個(gè)name參數(shù),則接下來(lái)的文本將會(huì)被置入name所對(duì)應(yīng)的<Frame>中(<setNextFrame/>類似)。

  • <setNextTemplate name=''/>:與<setNextFrame name=''/>類似,可以用來(lái)指定下一頁(yè)的模板結(jié)構(gòu)。如需要需要換頁(yè)時(shí):

<setNextTemplate name='nextTemplate'/>
<nextPage/>
<para> frame 1</para>
<nextFrame/>
<nextFrame name='common_frame_7'/>
<para> frame 4</para>
  • <condPageBreak>:設(shè)置高度height(必須且只有一個(gè)屬性),若該頁(yè)面余下空間足夠就在余下空間填充,否則則在下個(gè)頁(yè)面。

  • <storyPlace>:注明位置的懸浮著的區(qū)域(能夠不受<Frame>的限制)。

  • 列表List <ol><ul><li>:類似于HTML中的同種標(biāo)簽,可以通過(guò)bulletColor、bulletFontName、bulletFontSize等設(shè)置顏色,字體和縮進(jìn)等。<ol>有序列表可以通過(guò)設(shè)置bullet的值來(lái)指定序號(hào)類型:

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>```無(wú)序列表可以通過(guò)在```<ul>```或```<li>```中設(shè)置```value```的值來(lái)指定形狀(自己嘗試在```<ul>```中添加```value```屬性但沒(méi)有成功,報(bào)錯(cuò)):

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>: 在文檔頂部定義,用來(lái)注冊(cè)字體,顏色,設(shè)置頁(yè)面布局,裁剪線等。如:
<docinit useCropMarks="1" pageLayout="OneColumn" pageMode="UseThumbs">
         <!--注冊(cè)字體-->
          <registerTTFont faceName="song" fileName="STSONG.TTF"/>
          <color id="BLACK" CMYK="[0,0,0,1]"/>
</docinit>
  • <pageNumber/>:利用該標(biāo)簽可以獲得當(dāng)前頁(yè)。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容