Create PDF’s in seconds with Claude 4.6

Prompt for Claude to Generate eBook

eBook Generation: HTML to PDF with WeasyPrint

Overview

Convert an HTML ebook file to PDF using WeasyPrint, with automated handling of images, CSS fixes, and pagination optimization.


Step 1: Locate the HTML File

Check /mnt/user-data/uploads/ for the HTML file you’ve uploaded.


Step 2: Install WeasyPrint

bash

pip install weasyprint --break-system-packages

Step 3: Handle Base64 Images

If image files accompany the HTML:

  • Read each image file
  • Convert to base64 format
  • Replace placeholder src attributes in the HTML before rendering

Step 4: Scan & Fix Pagination Issues

4a — Chapter Page Breaks

Every .chapter-header or top-level section heading must have break-before:page to start each chapter on a new page.

  • If it only has break-after:avoid, add break-before:page alongside it

4b — Page Margins for Continuation Pages

Change @page margin rules from margin:0 to:

css

@page {
  size: A4;
  margin: 8mm 0 6mm 0;
}

Keep content side margins as padding on .content wrappers (e.g., padding: 0 16mm), not through @page left/right margins.

4c — Prevent Element Splitting

Add break-inside:avoid to:

  • .takeaway-block, .takeaway-grid, .takeaway-header (also add break-after:avoid)
  • .framework-block, .framework-row
  • .card-row, .card-table
  • .stat-table, .stat-row
  • .checklist-row
  • .step-row
  • .pull-quote
  • .tools-bar, .catch-bar
  • .speaker-card
  • Any compound block with a heading + body pair

4d — Orphan/Widow Control

Add to the body or html rule:

css

orphans: 3;
widows: 3;

4e — Header Anchoring

Any .section-label, .takeaway-header, .framework-title, or .chapter-header should have break-after:avoid to prevent it sitting alone at a page bottom.


Step 5: WeasyPrint-Specific Rules

Before rendering, verify:

Suppress Warnings:

python

import warnings
warnings.filterwarnings('ignore')

Layout Compatibility:

  • Use display:table for card grids and multi-column layouts (not flexbox or grid)
  • All fonts must reference system paths
  • All images must be base64-encoded inline (no external HTTP/HTTPS URLs)
  • If an external image URL cannot be fetched, remove the <img> tag entirely

Step 6: Generate the PDF

python

from weasyprint import HTML

HTML(filename='YOUR_MODIFIED_FILE.html').write_pdf(
    '/mnt/user-data/outputs/ebook.pdf',
    presentational_hints=True
)

Step 7: Verify the Output

  • Check total page count
  • Confirm each chapter starts on a new page
  • Confirm no content is flush against the top edge with zero padding

Step 8: Troubleshoot if Needed

IssueSolution
display:flex or display:grid on card layoutsReplace with display:table
<hr> tags presentRemove them
Dark backgrounds on full sections/chapters (except cover, pull quotes, prompt boxes, back cover)Remove or limit scope
Low-contrast text on dark backgroundsChange to #ffffff or rgba(255,255,255,0.85) minimum
Headings stranded at bottom with content on next pageAdd break-after:avoid
Large blocks (tables, cards, frameworks) split across pagesAdd break-inside:avoid

Step 9: Output & Share

The PDF is generated to /mnt/user-data/outputs/ebook.pdf — download link ready.

Free Resource

If you see this message you are not logged in to access this free resource.

Choose an option below to start reading this content.