/tests/html.py

https://code.google.com/p/pyfpdf/ · Python · 98 lines · 80 code · 13 blank · 5 comment · 4 complexity · e8dfd6a96fa3feb9edd396174d2f33b3 MD5 · raw file

  1. # -*- coding: latin-1 -*-
  2. "HTML Renderer for FPDF.py"
  3. __author__ = "Mariano Reingart <reingart@gmail.com>"
  4. __copyright__ = "Copyright (C) 2010 Mariano Reingart"
  5. __license__ = "LGPL 3.0"
  6. # Inspired by tuto5.py and several examples from fpdf.org, html2fpdf, etc.
  7. from fpdf import FPDF, HTMLMixin
  8. if __name__=='__main__':
  9. html="""
  10. <H1 align="center">html2fpdf</H1>
  11. <h2>Basic usage</h2>
  12. <p>You can now easily print text mixing different
  13. styles : <B>bold</B>, <I>italic</I>, <U>underlined</U>, or
  14. <B><I><U>all at once</U></I></B>!<BR>You can also insert links
  15. on text, such as <A HREF="http://www.fpdf.org">www.fpdf.org</A>,
  16. or on an image: click on the logo.<br>
  17. <center>
  18. <A HREF="http://www.fpdf.org"><img src="../tutorial/logo.png" width="104" height="71"></A>
  19. </center>
  20. <h3>Sample List</h3>
  21. <ul><li>option 1</li>
  22. <ol><li>option 2</li></ol>
  23. <li>option 3</li></ul>
  24. <table border="0" align="center" width="50%">
  25. <thead><tr><th width="30%">Header 1</th><th width="70%">header 2</th></tr></thead>
  26. <tbody>
  27. <tr><td>cell 1</td><td>cell 2</td></tr>
  28. <tr><td>cell 2</td><td>cell 3</td></tr>
  29. </tbody>
  30. </table>
  31. <table border="1">
  32. <thead><tr bgcolor="#A0A0A0"><th width="30%">Header 1</th><th width="70%">header 2</th></tr></thead>
  33. <tfoot><tr bgcolor="#E0E0E0"><td>footer 1</td><td>footer 2</td></tr></tfoot>
  34. <tbody>
  35. <tr><td>cell 1</td><td>cell 2</td></tr>
  36. <tr>
  37. <td width="30%">cell 1</td><td width="70%" bgcolor="#D0D0FF" align='right'>cell 2</td>
  38. </tr>
  39. </tbody>
  40. <tbody><tr><td colspan="2">cell spanned</td></tr></tbody>
  41. <tbody>
  42. """ + """<tr bgcolor="#F0F0F0">
  43. <td>cell 3</td><td>cell 4</td>
  44. </tr><tr bgcolor="#FFFFFF">
  45. <td>cell 5</td><td>cell 6</td>
  46. </tr>""" * 200 + """
  47. </tbody>
  48. </table>
  49. <font face='helvetica' size='40'>Font example: Arial 40pt</font>
  50. """
  51. class MyFPDF(FPDF, HTMLMixin):
  52. def header(self):
  53. self.image('../tutorial/logo_pb.png',10,8,33)
  54. self.set_font('Arial','B',15)
  55. self.cell(80)
  56. self.cell(30,10,'Title',1,0,'C')
  57. self.ln(20)
  58. def footer(self):
  59. self.set_y(-15)
  60. self.set_font('Arial','I',8)
  61. txt = 'Page %s of %s' % (self.page_no(), self.alias_nb_pages())
  62. self.cell(0,10,txt,0,0,'C')
  63. pdf=MyFPDF()
  64. #First page
  65. pdf.add_page()
  66. pdf.write_html(html)
  67. # this will fail (tables without widht are not supported):
  68. try:
  69. pdf.write_html("""<table><tr><td></td></tr></table>""")
  70. except RuntimeError:
  71. pass
  72. # this may be rendered incorrectly as currently there is no two pass auto-layout:
  73. pdf.write_html("""<table><tr><th></th><td width="100%">100%</td></tr></table>""")
  74. pdf.output('html.pdf','F')
  75. import os
  76. try:
  77. os.startfile("html.pdf")
  78. except:
  79. os.system("evince html.pdf")