write_html_pdf.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. html = """
  2. <h1 align="center">HTML to PDF</h1>
  3. <h2>An exmaple to convert HTML to PDF</h2>
  4. <p>You can now easily print text mixing different
  5. styles : <B>bold</B>, <I>italic</I>, <U>underlined</U>, or
  6. <B><I><U>all at once</U></I></B>!<BR>You can also insert links
  7. on text, such as <A HREF="https://pypi.org/project/fpdf/">www.pypi.org</A>,
  8. or on an image: click on the logo image.<br>
  9. <center>
  10. <a href="http://www.pypi.org"><img src="logo.png" width="104" height="71"></a>
  11. </center>
  12. <h2>Paragraph</h2>
  13. <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor, facilisis luctus, metus</p>
  14. <h2>Paragraph with Bold Text</h2>
  15. <p><B>Pellentesque habitant morbi tristique</B> senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. <em>Aenean ultricies mi vitae est.</em> Mauris placerat eleifend leo. Quisque sit amet est et sapien ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, <code>commodo vitae</code>, ornare sit amet, wisi. Aenean fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. <a href="#">Donec non enim</a> in turpis pulvinar facilisis. Ut felis.</p>
  16. <h2>Ordered List</h2>
  17. <ol>
  18. <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  19. <li>Aliquam tincidunt mauris eu risus.</li>
  20. </ol>
  21. <blockquote><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna. Cras in mi at felis aliquet congue. Ut a est eget ligula molestie gravida. Curabitur massa. Donec eleifend, libero at sagittis mollis, tellus est malesuada tellus, at luctus turpis elit sit amet quam. Vivamus pretium ornare est.</p></blockquote>
  22. <h3>Unordered List</h3>
  23. <ul>
  24. <li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li>
  25. <li>Aliquam tincidunt mauris eu risus.</li>
  26. </ul>
  27. <h2>Code Block</h2>
  28. <pre><code>
  29. #header h1 a {
  30. display: block;
  31. width: 300px;
  32. height: 80px;
  33. }
  34. </code></pre>
  35. <h2>Definition List</h2>
  36. <dl>
  37. <dt>Definition list</dt>
  38. <dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
  39. aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
  40. commodo consequat.</dd>
  41. <dt>Lorem ipsum dolor sit amet</dt>
  42. <dd>Consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna
  43. aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
  44. commodo consequat.</dd>
  45. </dl>
  46. <h2>Table</h2>
  47. <table border="0" align="center" width="50%">
  48. <thead><tr><th width="30%">Header 1</th><th width="70%">header 2</th></tr></thead>
  49. <tbody>
  50. <tr><td>cell 1</td><td>cell 2</td></tr>
  51. <tr><td>cell 2</td><td>cell 3</td></tr>
  52. </tbody>
  53. </table>
  54. """
  55. from fpdf import FPDF, HTMLMixin
  56. class MyFPDF(FPDF, HTMLMixin):
  57. pass
  58. pdf = MyFPDF()
  59. #First page
  60. pdf.add_page()
  61. pdf.write_html(html)
  62. pdf.output('html2pdf.pdf', 'F')