Quick Start
This guide will get you up and running with PlutoPrint in minutes.
1. Using the Command Line
PlutoPrint comes with a built-in CLI tool. To convert a simple HTML file to a PDF:
# Create a dummy file
echo "<h1>Hello Pluto</h1>" > input.html
# Convert to PDF
plutoprint input.html output.pdf --size=A4
2. Python: Hello World PDF
The core of the library is the Book class. It manages the document content and rendering settings.
import plutoprint
# 1. Initialize the Book with a page size
book = plutoprint.Book(plutoprint.PAGE_SIZE_A4)
# 2. Load content (HTML string)
html_content = """
<html>
<head>
<style>
body { font-family: sans-serif; text-align: center; margin-top: 50px; }
h1 { color: navy; }
</style>
</head>
<body>
<h1>Hello, PlutoPrint!</h1>
<p>This is a PDF generated from HTML.</p>
</body>
</html>
"""
book.load_html(html_content)
# 3. Write the output to a file
book.write_to_pdf("hello_world.pdf")
print("PDF generated: hello_world.pdf")
3. Python: Generating an Image
You can also render HTML content directly to an image (PNG). This is useful for generating preview thumbnails or social media cards.
import plutoprint
# Use SCREEN media type for images usually
book = plutoprint.Book(media=plutoprint.MEDIA_TYPE_SCREEN)
book.load_html(
"<div style='background: #f0f0f0; padding: 20px; display: inline-block;'>Badge Content</div>"
)
# Write to PNG.
# width=-1, height=-1 means auto-detect size based on content.
book.write_to_png("badge.png")
Next Steps
- Explore the Usage Guide for more complex workflows.
- Check the API Reference for detailed method signatures.
- See Examples for advanced use cases like QR codes and Charts.