HTMLMixin.write_html(text: str, image_map = None)


PyFPDF supports basic HTML, mainly intended to write reports from web frameworks.

It understands a limited subset of the HTML language, and it doesn't support advanced features nor CSS (look below).

HTMLMixin could be used along with FPDF class to implement this functionality (see the example).

Sample: html.pdf



string with HTML markup


dictionary with image map


HTML tags and attributes supported:

  • H1 to H8: headings (align attribute)
  • P: paragraphs (align attributes)
  • B, I, U: bold, italic, underline
  • FONT: (face, size, color attributes)
  • CENTER for aligning
  • A: links (href attribute)
  • IMG: images (src, width, height attributes)
  • OL, UL, LI: ordered, unordered and list items (can be nested)
  • TABLE: (border, width attributes)
    • THEAD: header (opens each page)
    • TFOOT: footer (closes each page)
    • TBODY: actual rows
    • TR: rows (bgcolor attribute)
      • TH: highlight cells (align, bgcolor, width attributes)
      • TD: rows (align, bgcolor, width attributes)

Note: Tables should have at least a first TH row with a width attribute.


html = """
<H1 align="center">html2fpdf</H1>
<h2>Basic usage</h2>
<p>You can now easily print text mixing different
styles : <B>bold</B>, <I>italic</I>, <U>underlined</U>, or
<B><I><U>all at once</U></I></B>!<BR>You can also insert links
on text, such as <A HREF="http://www.fpdf.org">www.fpdf.org</A>,
or on an image: click on the logo.<br>
<A HREF="http://www.fpdf.org"><img src="tutorial/logo.png" width="104" height="71"></A>
<h3>Sample List</h3>
<ul><li>option 1</li>
<ol><li>option 2</li></ol>
<li>option 3</li></ul>

<table border="0" align="center" width="50%">
<thead><tr><th width="30%">Header 1</th><th width="70%">header 2</th></tr></thead>
<tr><td>cell 1</td><td>cell 2</td></tr>
<tr><td>cell 2</td><td>cell 3</td></tr>

from pyfpdf import FPDF, HTMLMixin

class MyFPDF(FPDF, HTMLMixin):

pdf = MyFPDF()
#First page
pdf.output('html.pdf', 'F')

See html.py or Web2Py for a complete example.

See also

write, add_font, image.