Last active
November 20, 2023 10:02
-
-
Save documentprocessing/be5437a12082a93bcd9504707f5800ec to your computer and use it in GitHub Desktop.
Convert HTML to PDF documents in Python using xhtml2pdf library. Check https://products.documentprocessing.com/conversion/python/xhtml2pdf/ for more details.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the pisa module from the xhtml2pdf library | |
from xhtml2pdf import pisa | |
# Import the BytesIO class from the io module | |
from io import BytesIO | |
# Open the HTML file for reading | |
with open("file.html", "r", encoding="utf-8") as file: | |
# Read the content of the file and store it as a string | |
html_content = file.read() | |
# Create a BytesIO object to store the PDF output | |
pdf_output = BytesIO() | |
# Convert the HTML content to a PDF document | |
pisa.CreatePDF(html_content, dest=pdf_output, encoding='utf-8') | |
# Open a PDF file for writing in binary mode | |
with open("html-file-to-pdf.pdf", "wb") as pdf_file: | |
# Write the PDF content to the file | |
pdf_file.write(pdf_output.getvalue()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Import the pisa module from the xhtml2pdf library | |
from xhtml2pdf import pisa | |
# Import the BytesIO class from the io module | |
from io import BytesIO | |
# HTML content that is to be converted to a PDF | |
html_content = """ | |
<!DOCTYPE html> | |
<html> | |
<body> | |
<h1>Example with Hyperlinks</h1> | |
<p>Visit <a href="https://www.example.com">Example</a></p> | |
</body> | |
</html> | |
""" | |
# Create a BytesIO object to store the PDF output | |
pdf_output = BytesIO() | |
# Use xhtml2pdf to convert the HTML content to a PDF and store it in pdf_output | |
pisa.CreatePDF(html_content, dest=pdf_output, encoding='utf-8') | |
# Open a PDF file for writing in binary mode | |
with open("html-to-pdf.pdf", "wb") as pdf_file: | |
# Write the PDF content to the file | |
pdf_file.write(pdf_output.getvalue()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment