Skip to content

Instantly share code, notes, and snippets.

@matefs
Created April 26, 2024 11:44
Show Gist options
  • Save matefs/77c97536e6dfcf14509ce712982387c3 to your computer and use it in GitHub Desktop.
Save matefs/77c97536e6dfcf14509ce712982387c3 to your computer and use it in GitHub Desktop.
html-to-pdf-python-convertion.py
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