HTML¶
fpdf2
supports basic rendering from HTML.
This is implemented by using html.parser.HTMLParser
from the Python standard library. The whole HTML 5 specification is not supported, and neither is CSS, but bug reports & contributions are very welcome to improve this. cf. Supported HTML features below for details on its current limitations.
For a more robust & feature-full HTML-to-PDF converter in Python, you may want to check Reportlab (or xhtml2pdf based on it), WeasyPrint or borb.
write_html usage example¶
HTML rendering require the use of write_html
method:
from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.write_html("""
<dl>
<dt>Description title</dt>
<dd>Description Detail</dd>
</dl>
<h1>Big title</h1>
<section>
<h2>Section title</h2>
<p><b>Hello</b> world. <u>I am</u> <i>tired</i>.</p>
<p><a href="https://github.com/PyFPDF/fpdf2">PyFPDF/fpdf2 GitHub repo</a></p>
<p align="right">right aligned text</p>
<p>i am a paragraph <br />in two parts.</p>
<font color="#00ff00"><p>hello in green</p></font>
<font size="7"><p>hello small</p></font>
<font face="helvetica"><p>hello helvetica</p></font>
<font face="times"><p>hello times</p></font>
</section>
<section>
<h2>Other section title</h2>
<ul><li>unordered</li><li>list</li><li>items</li></ul>
<ol><li>ordered</li><li>list</li><li>items</li></ol>
<br>
<br>
<pre>i am preformatted text.</pre>
<br>
<blockquote>hello blockquote</blockquote>
<table width="50%">
<thead>
<tr>
<th width="30%">ID</th>
<th width="70%">Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Alice</td>
</tr>
<tr>
<td>2</td>
<td>Bob</td>
</tr>
</tbody>
</table>
</section>
""")
pdf.output("html.pdf")
Supported HTML features¶
<h1>
to<h8>
: headings (andalign
attribute)<p>
: paragraphs (andalign
,line-height
attributes)<b>
,<i>
,<u>
: bold, italic, underline<font>
: (andface
,size
,color
attributes)<center>
for aligning<a>
: links (andhref
attribute)<pre>
&<code>
tags<img>
: images (andsrc
,width
,height
attributes)<ol>
,<ul>
,<li>
: ordered, unordered and list items (can be nested)<dl>
,<dt>
,<dd>
: description list, title, details (can be nested)<sup>
,<sub>
: superscript and subscript text<table>
: (andborder
,width
attributes)<thead>
: header (opens each page)<tfoot>
: footer (closes each page)<tbody>
: actual rows<tr>
: rows (withbgcolor
attribute)<th>
: heading cells (withalign
,bgcolor
,width
attributes)<td>
: cells (withalign
,bgcolor
,width
attributes)
Notes:
- tables should have at least a first
<th>
row with awidth
attribute. - currently table cells can only contain a single line, cf. issue 91. Contributions are welcome to add support for multi-line text in them! 😊