fpdf2 - HTML (original) (raw)
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 requires the use of FPDF.write_html():
`from fpdf import FPDF
pdf = FPDF() pdf.add_page() pdf.write_html("""
- Description title
- Description Detail
Big title
Section title
Hello world. I am tired.
right aligned text
i am a paragraph
in two parts.
hello in green
hello small
hello helvetica
hello times
Other section title
- unordered
- list
- items
- ordered
- list
- items
i am preformatted text.
hello blockquote
ID | Name |
---|---|
1 | Alice |
2 | Bob |
Styling HTML tags globally¶
New in 2.7.9
The style of several HTML tags (<a>
, <blockquote>
, <code>
, <pre>
, <h1>
, <h2>
, <h3>
...) can be set globally, for the whole HTML document, by passing tag_styles
to FPDF.write_html()
:
`from fpdf import FPDF, FontFace
pdf = FPDF() pdf.add_page() pdf.write_html("""
Big title
Section title
Hello world!
Similarly, the indentation of several HTML tags (<blockquote>
, <dd>
, <li>
) can be set globally, for the whole HTML document, by passing tag_styles
to FPDF.write_html()
:
`from fpdf import FPDF, TextStyle
pdf = FPDF() pdf.add_page() pdf.write_html("""
- Term
- Definition
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non risus. Suspendisse lectus tortor, dignissim sit amet, adipiscing nec, ultricies sed, dolor. Cras elementum ultrices diam.""", tag_styles={ "dd": TextStyle(l_margin=5), "blockquote": TextStyle(color="#ccc", font_style="I", t_margin=5, b_margin=5, l_margin=10), }) pdf.output("html_dd_indented.pdf") `
⚠️ Note that this styling is currently only supported for a subset of all HTML tags, and that some FontFace or TextStyle properties may not be honored. However, Pull Request are welcome to implement missing features!
Default font¶
New in 2.8.0
The default font used by FPDF.write_html() is Times.
You can change this default font by passing font_family
to this method:
`from fpdf import FPDF
pdf = FPDF() pdf.add_page() pdf.write_html("""
Big title
Section title
Hello world!
Supported HTML features¶
<h1>
to<h6>
: headings (andalign
attribute)<p>
: paragraphs (andalign
,line-height
attributes)<br>
&<hr>
tags<b>
,<i>
,<s>
,<u>
: bold, italic, strikethrough, underline<font>
: (andface
,size
,color
attributes)<center>
for aligning<a>
: links (andhref
attribute) to a file, URL, or page number.<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>
: (withalign
,border
,width
,cellpadding
,cellspacing
attributes) those tags are rendered using fpdf2 Tables layout and the following sub-tags are supported:<thead>
: optional tag, wraps the table header row<tfoot>
: optional tag, wraps the table footer row<tbody>
: optional tag, wraps the table rows with actual content<tr>
: rows (withalign
,bgcolor
attributes)<th>
: heading cells (withalign
,bgcolor
,width
attributes)<td>
: cells (withalign
,bgcolor
,width
,rowspan
,colspan
attributes)
Page breaks¶
New in 2.8.0
Page breaks can be triggered explicitly using the break-before or break-after CSS properties. For example you can use:
<br style="break-after: page">
or:
`
Top of a new page.
`Known limitations¶
fpdf2
HTML renderer does not support some configurations of nested tags. For example:
<table>
cells can contain<td><b><em>nested tags forming a single text block</em></b></td>
, but not<td><b>arbitrarily</b> nested <em>tags</em></td>
- cf. issue #845
You can also check the currently open GitHub issues with the tag html
: label:html is:open
Using Markdown¶
Check the dedicated page: Combine with Markdown