Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: html2pdf.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- Description:
- This script converts an HTML file (.html) to a PDF file (.pdf).
- It uses BeautifulSoup to parse the HTML content and generates a PDF with the prettified HTML.
- Requirements:
- - Python 3.x
- - FPDF library (install using: pip install fpdf)
- - BeautifulSoup library (install using: pip install beautifulsoup4)
- Usage:
- 1. Save this script as 'html2pdf.py'.
- 2. Ensure your HTML file ('example.html') is in the same directory as the script.
- 3. Install the FPDF library using the command: 'pip install fpdf'
- 4. Install the BeautifulSoup library using the command: 'pip install beautifulsoup4'
- 5. Run the script.
- 6. The converted PDF file ('html2pdf.pdf') will be generated in the same directory.
- Note: Adjust the 'html_filename' and 'pdf_filename' variables in the script as needed.
- """
- from fpdf import FPDF
- from bs4 import BeautifulSoup
- def html_to_pdf(html_filename, pdf_filename):
- pdf = FPDF()
- pdf.add_page()
- pdf.set_font("Arial", size=12)
- with open(html_filename, 'r') as htmlfile:
- soup = BeautifulSoup(htmlfile, 'html.parser')
- pdf.multi_cell(0, 10, soup.prettify())
- pdf.output(pdf_filename)
- if __name__ == "__main__":
- html_filename = 'example.html'
- pdf_filename = 'html2pdf.pdf'
- html_to_pdf(html_filename, pdf_filename)
- print(f"Converted '{html_filename}' to '{pdf_filename}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement