FitToPage.js JS Library Demo

Download Back To CSSScript.Com

Automatically fit HTML content to a single PDF page

📋 How to Test

1. Press Cmd+P (Mac) or Ctrl+P (Windows/Linux)

2. Select "Save as PDF" as the destination

3. Notice everything fits on ONE page!

Features

  • Automatic content measurement
  • Dynamic @page CSS injection
  • Works with native browser print
  • Zero dependencies
  • Lightweight (~2KB)
  • Preserves text (not image-based)

Configuration Options

  • Custom element selector
  • Adjustable margins and padding
  • Orientation control (auto/portrait/landscape)
  • Debug mode with dimension info
  • Page break prevention
  • Ready callback support

Why FitToPage.js?

Native Print Support

Works seamlessly with Cmd+P / Ctrl+P. No custom PDF generation needed.

Smart Measurement

Automatically calculates content dimensions and sets optimal page size.

Text Preserved

Unlike image-based solutions, text remains selectable and searchable.

Zero Dependencies

Pure vanilla JavaScript. No jQuery, no frameworks, no bloat.

Lightweight

Only ~2KB minified. Won't slow down your page load.

Easy Integration

Just call FitToPage.init() and you're done. Simple API.

Usage Example

<script src="fit-to-page.js"></script>
<script>
    FitToPage.init({
        selector: '.container',
        margin: 10,
        padding: 5,
        orientation: 'auto',
        debug: true
    });
</script>