No Description

pdf2.py 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # https://www.blog.pythonlibrary.org/2018/06/05/creating-pdfs-with-pyfpdf-and-python/
  2. from fpdf import FPDF, HTMLMixin
  3. class HTML2PDF(FPDF, HTMLMixin):
  4. pass
  5. html = '''<h1 align="center">PyFPDF HTML Demo</h1>
  6. <p>This is regular text</p>
  7. <p>You can also <b>bold</b>, <i>italicize</i> or <u>underline</u>
  8. '''
  9. pdf = HTML2PDF()
  10. pdf.add_page()
  11. pdf.write_html(html)
  12. # pdf.output('html2pdf.pdf')
  13. ##########
  14. # pdf = FPDF()
  15. # pdf.add_page()
  16. pdf.set_font("Arial", size=12)
  17. pdf.cell(200, 10, txt="Welcome to Python!", ln=1, align="C")
  18. pdf.cell(200, 10, txt="Welcome to Python!", ln=1, align="C")
  19. pdf.cell(0, 15, txt="Welcome to Python!", ln=1, align="C")
  20. pdf.rect(20, 20, 100, 50)
  21. ###########
  22. spacing=1
  23. data = [['First Name', 'Last Name', 'email', 'zip'],
  24. ['Mike', 'Driscoll', 'mike@somewhere.com', '55555'],
  25. ['John', 'Doe', 'jdoe@doe.com', '12345'],
  26. ['Nina', 'Ma', 'inane@where.com', '54321']
  27. ]
  28. # pdf = FPDF()
  29. pdf.set_font("Arial", size=12)
  30. # pdf.add_page()
  31. col_width = pdf.w / 4.5
  32. row_height = pdf.font_size
  33. for row in data:
  34. for item in row:
  35. pdf.cell(col_width, row_height*spacing,
  36. txt=item, border=1)
  37. pdf.ln(row_height*spacing)
  38. ###########
  39. pdf.output("simple_demo.pdf")