Created
April 26, 2024 11:44
-
-
Save matefs/77c97536e6dfcf14509ce712982387c3 to your computer and use it in GitHub Desktop.
html-to-pdf-python-convertion.py
This file contains 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 pdfkit | |
def html_to_pdf(html_content, pdf_file): | |
try: | |
pdfkit.from_string(html_content, pdf_file) | |
print("PDF conversion successful!") | |
except Exception as e: | |
print(f"PDF conversion failed: {str(e)}") | |
if __name__ == "__main__": | |
# HTML content for testing | |
html_content = """ | |
<html> | |
<head> | |
<title>Test HTML to PDF</title> | |
</head> | |
<body> | |
<h1>This is a test HTML to PDF conversion</h1> | |
<p>This is a paragraph.</p> | |
<p>This is another paragraph.</p> | |
<ul> | |
<li>Item 1</li> | |
<li>Item 2</li> | |
<li>Item 3</li> | |
</ul> | |
</body> | |
</html> | |
""" | |
# Replace 'output.pdf' with the desired output PDF file path | |
output_pdf_file = 'output.pdf' | |
html_to_pdf(html_content, output_pdf_file) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment