Auto-Fit HTML to Single PDF Page – FitToPage.js
It calculates the exact rendered dimensions of a target DOM element and injects dynamic CSS to adjust the print page size. This prevents awkward page breaks in long reports, invoices, or dashboards.
The library works directly within the browser’s native print system. You can trigger the standard print dialog (Cmd+P or Ctrl+P) to generate a PDF that matches the content’s exact height and width.
It preserves text selectability and searchability because it retains the original HTML structure rather than converting the page to an image.
remeasure() method for updating the page size after content changes.1. Install & download.
# NPM $ npm install fit-to-page
2. Load the minified version of the FitToPage.js library in the document.
<!-- Local --> <script src="fit-to-page.min.js"></script> <!-- CDN --> <script src="https://cdn.jsdelivr.net/npm/fit-to-page/fit-to-page.js"></script>
3. Initialize the library with the init method. This configuration measures your entire body element and sets the page size accordingly. When you press Cmd+P on Mac or Ctrl+P on Windows and save as PDF, your content appears on a single page.
// Initialize with defaults FitToPage.init()
4. If your application loads data asynchronously, you must update the measurements after the DOM updates using the remeasure method.
FitToPage.init({ selector: '.dynamic-report' });
const loadButton = document.getElementById('load-data');
loadButton.addEventListener('click', async () => {
await fetchData(); // Assume this function populates the DOM
// Recalculate dimensions after content changes
FitToPage.remeasure();
}); 5. Available configuration options.
FitToPage.init({
selector: 'body',
margin: 10,
padding: 5,
dpi: 96,
orientation: 'auto',
debug: false,
preventPageBreaks: true,
onReady: null
}) Q: Can I use this for multi-page documents?
A: No, FitToPage.js specifically solves the single-page fitting problem.
Q: What happens if my content is too large?
A: The library calculates and applies whatever page size is needed to fit your content.
Q: Can I measure and fit only part of my page?
A: Yes, use the selector option to target a specific container element. The library measures only that element and its contents, ignoring everything else on the page.
Q: Does the content remain searchable in the PDF?
A: Yes. Since the library uses native browser printing rather than canvas rendering, all text remains selectable and searchable in the final PDF.
Q: Does this library create the PDF file?
A: No. FitToPage.js only adjusts the CSS page size. You use your browser’s native “Print to PDF” function to save the file.
The post Auto-Fit HTML to Single PDF Page – FitToPage.js appeared first on CSS Script.
Looking for a dual-purpose power bank that can charge both your iPhone and your Apple…
The Punisher: One Last Kill has been the subject of online mockery following a wonky-looking…
Warhammer 40,000: Mechanicus II launches on May 21 — alongside the Warhammer Skulls video game…
Former Nintendo staff have branded the recent leak of upcoming Switch 2 game details as…
Want a few new video games? Well, there are some incredible deals on Switch and…
A newly disclosed security vulnerability in Microsoft Teams could allow attackers to spoof local devices,…
This website uses cookies.