Converting HTML Articles to PDF
You have multiple options for converting HTML articles to PDF using Python. Here are two approaches:
Approach 1: Playwright with Chromium
- Reliability: High
- Control: High
- Complexity: Medium
You can use Playwright with Chromium to convert HTML articles to PDF. Playwright provides a high-level API for automating Chromium, allowing you to control the browser's behavior and generate PDFs with custom layouts.
To achieve the desired layout, you can use the following steps:
- Load the HTML article in a Chromium browser instance using Playwright.
- Set the page layout to landscape and adjust the zoom level as needed.
- Use the
page.pdf() method to generate a PDF with the desired layout.
- To add a header and footer, you can use the
page.add_style_tag() method to inject custom CSS styles that position the header and footer elements correctly.
Example code:
from playwright.sync_api import sync_playwright
with sync_playwright() as p:
browser = p.chromium.launch()
page = browser.new_page()
page.set_viewport_size({"width": 1024, "height": 768})
page.set_layout("landscape")
page.zoom = 0.75 # adjust zoom level
# load HTML article
page.goto("https://example.com/article")
# add header and footer
page.add_style_tag(content="""
@page {
size: landscape;
margin: 1in;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 1in;
background-color: #f0f0f0;
text-align: center;
}
footer {
position: fixed;
bottom: 0;
left: 0;
width: 100%;
height: 1in;
background-color: #f0f0f0;
text-align: center;
}
""")
# generate PDF
page.pdf(path="article.pdf", display_header_footer=True, header_template="<header>Header</header>", footer_template="<footer>Footer</footer>")
browser.close()
Approach 2: Selenium with Chrome
- Reliability: Medium
- Control: Medium
- Complexity: High
You can use Selenium with Chrome to convert HTML articles to PDF. Selenium provides a lower-level API than Playwright, requiring more manual configuration and scripting.
To achieve the desired layout, you can use the following steps:
- Load the HTML article in a Chrome browser instance using Selenium.
- Set the page layout to landscape and adjust the zoom level as needed using Selenium's
execute_script() method.
- Use the
execute_script() method to inject custom JavaScript code that generates a PDF with the desired layout.
- To add a header and footer, you can use the
execute_script() method to inject custom JavaScript code that positions the header and footer elements correctly.
Example code:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--window-size=1024,768")
driver = webdriver.Chrome(options=options)
# load HTML article
driver.get("https://example.com/article")
# set layout and zoom level
driver.execute_script("""
document.body.style.transform = 'scale(0.75)';
document.body.style.transformOrigin = '0 0';
""")
# add header and footer
driver.execute_script("""
var header = document.createElement('header');
header.style.position = 'fixed';
header.style.top = '0';
header.style.left = '0';
header.style.width = '100%';
header.style.height = '1in';
header.style.backgroundColor = '#f0f0f0';
header.style.textAlign = 'center';
header.innerHTML = 'Header';
document.body.appendChild(header);
var footer = document.createElement('footer');
footer.style.position = 'fixed';
footer.style.bottom = '0';
footer.style.left = '0';
footer.style.width = '100%';
footer.style.height = '1in';
footer.style.backgroundColor = '#f0f0f0';
footer.style.textAlign = 'center';
footer.innerHTML = 'Footer';
document.body.appendChild(footer);
""")
# generate PDF
driver.execute_script("""
var pdf = new jsPDF('l', 'mm', [297, 210]);
pdf.fromHTML(document.body);
pdf.save('article.pdf');
""")
driver.quit()
Note that the Selenium approach requires more manual configuration and scripting, and may be less reliable than the Playwright approach.
Approach 3: WeasyPrint
- Reliability: High
- Control: High
- Complexity: Low
WeasyPrint is a Python library that allows you to generate PDFs from HTML documents. It provides a high-level API for customizing the layout and appearance of the PDF.
To achieve the desired layout, you can use the following steps:
- Load the HTML article using WeasyPrint's
HTML() function.
- Set the page layout to landscape and adjust the zoom level as needed using WeasyPrint's
write_pdf() method.
- To add a header and footer, you can use WeasyPrint's
write_pdf() method to inject custom CSS styles that position the header and footer elements correctly.
Example code:
from weasyprint import HTML
from weasyprint.fonts import FontConfiguration
font_config = FontConfiguration()
html = HTML(string="<html><body>...</body></html>")
html.write_pdf("article.pdf", font_config=font_config, page_size=(297, 210), zoom=0.75)
# add header and footer
html.write_pdf("article.pdf", font_config=font_config, page_size=(297, 210), zoom=0.75, header_template="<header>Header</header>", footer_template="<footer>Footer</footer>")
WeasyPrint provides a simple and efficient way to generate PDFs from HTML documents, with a high degree of control over the layout and appearance.