12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- # https://www.blog.pythonlibrary.org/2018/06/05/creating-pdfs-with-pyfpdf-and-python/
- from fpdf import FPDF, HTMLMixin
-
- class HTML2PDF(FPDF, HTMLMixin):
- pass
-
- html = '''<h1 align="center">PyFPDF HTML Demo</h1>
- <p>This is regular text</p>
- <p>You can also <b>bold</b>, <i>italicize</i> or <u>underline</u>
- '''
- pdf = HTML2PDF()
- pdf.add_page()
- pdf.write_html(html)
- # pdf.output('html2pdf.pdf')
-
- ##########
-
- # pdf = FPDF()
- # pdf.add_page()
- pdf.set_font("Arial", size=12)
- pdf.cell(200, 10, txt="Welcome to Python!", ln=1, align="C")
- pdf.cell(200, 10, txt="Welcome to Python!", ln=1, align="C")
- pdf.cell(0, 15, txt="Welcome to Python!", ln=1, align="C")
- pdf.rect(20, 20, 100, 50)
-
- ###########
- spacing=1
- data = [['First Name', 'Last Name', 'email', 'zip'],
- ['Mike', 'Driscoll', 'mike@somewhere.com', '55555'],
- ['John', 'Doe', 'jdoe@doe.com', '12345'],
- ['Nina', 'Ma', 'inane@where.com', '54321']
- ]
-
- # pdf = FPDF()
- pdf.set_font("Arial", size=12)
- # pdf.add_page()
-
- col_width = pdf.w / 4.5
- row_height = pdf.font_size
- for row in data:
- for item in row:
- pdf.cell(col_width, row_height*spacing,
- txt=item, border=1)
- pdf.ln(row_height*spacing)
-
-
- ###########
-
- pdf.output("simple_demo.pdf")
|