Print-to-PDF Tool
Minimal, zero-dependency print-to-PDF tool using browser native window.print()
API.
Key Features
- Zero Dependencies - Uses only browser APIs
- Page Configuration - Size, orientation, margins
- Smart Page Breaks - Avoid breaking tables, images, headings
- Content Optimization - Hide elements, optimize images
- Callbacks - Before/after print events
const printer = Domma.elements.printToPDF('#content', {
pageSize: 'A4',
orientation: 'portrait',
margins: 'normal'
});
printer.print();
Basic Print Example
Note: Configure print options when creating the instance, then call print()
to trigger the browser's native print dialog.
Sample Document
This is a sample document demonstrating the print-to-PDF functionality. The content below will be optimized for printing.
Sample Data Table
| Name | Phone | City | |
|---|---|---|---|
| Alice Johnson | alice@example.com | (555) 123-4567 | New York |
| Bob Smith | bob@example.com | (555) 234-5678 | Los Angeles |
| Carol Williams | carol@example.com | (555) 345-6789 | Chicago |
| David Brown | david@example.com | (555) 456-7890 | Houston |
| Eve Davis | eve@example.com | (555) 567-8901 | Phoenix |
Key Points
- Zero dependencies - uses browser print API
- Configurable page size and orientation
- Smart page break handling
- Works with any HTML content
Note: This tool triggers the browser's native print dialog. Users can then choose to save as PDF or send to a printer.
API Reference
| Option | Type | Default | Description |
|---|---|---|---|
pageSize |
String | 'A4' |
'A4', 'Letter', 'Legal', or custom {width, height} |
orientation |
String | 'portrait' |
'portrait' or 'landscape' |
margins |
String | 'normal' |
'none', 'narrow', 'normal', 'wide', or custom |
title |
String | null |
Document title for print |
removeBackgrounds |
Boolean | false |
Strip background colors/images |
optimizeImages |
Boolean | true |
Ensure images fit page width |
hideElements |
Array | [] |
Selectors of elements to hide |
onBeforePrint |
Function | null |
Called before print dialog |
onAfterPrint |
Function | null |
Called after print/cancel |
Methods
| Method | Description |
|---|---|
print() |
Trigger browser print dialog immediately |
setOptions(options) |
Update configuration options |
destroy() |
Cleanup and remove print styles |
Note: The preview() method is not implemented. Configure all
options when creating the instance.