The Challenge
When you print content in a browser usingwindow.print() or by creating a new window, the printed content loses its styling because:
- The new print window is a separate document context
- Stylesheets linked in the parent page aren’t automatically available
- Computed styles from Vue components don’t transfer
- CSS-in-JS and scoped styles require special handling
Style Preservation Architecture
Vue Print It solves this through a multi-layered approach:Style Injection Methods
1. Inline Style Elements
The first step is copying all<style> tags from the page:
- Component-scoped styles
- Inline CSS blocks
- Dynamically injected styles from CSS-in-JS libraries
2. External Stylesheets
Next, all linked stylesheets are copied:- External CSS files (e.g., Bootstrap, Tailwind)
- CSS frameworks
- Font stylesheets (Google Fonts, etc.)
3. CSS Rules Extraction
For more robust style preservation, the plugin also extracts CSS rules directly:Custom Style Injection
In addition to automatic style preservation, you can inject custom styles:- A URL (creates a
<link>element) - Raw CSS (creates a
<style>element)
Usage Examples
Browser Print Window
When using the browser print method, styles are injected into the new window:Bridge Print Method
For bridge printing, styles are embedded directly in the HTML string:Configuration Options
Controls whether page styles are automatically preserved. Set to
false to disable automatic style injection.Array of custom CSS strings or stylesheet URLs to inject. Supports both inline CSS and external stylesheets.
Examples
Best Practices
Use Print-Specific Styles
Use Print-Specific Styles
Add
@media print queries to your global styles or inject them per-call:Handle External Resources
Handle External Resources
When using external stylesheets, ensure they’re accessible from the print context:
Test Cross-Origin Stylesheets
Test Cross-Origin Stylesheets
Stylesheets from different domains may be blocked by CORS. Always provide fallback:
Optimize for Performance
Optimize for Performance
For large applications, consider disabling automatic style preservation and manually specify required styles:
Common Issues and Solutions
Styles Not Appearing
Styles Not Appearing
Problem: Print output has no styling.Solutions:
- Verify
preserveStyles: true(it’s the default) - Check browser console for CORS errors
- Add styles explicitly via the
stylesoption - Ensure stylesheets are loaded before printing
CORS Stylesheet Errors
CORS Stylesheet Errors
Problem: Cannot access stylesheets from CDN.Solutions:
- The plugin automatically falls back to linking the stylesheet
- Provide inline fallback styles in the
stylesarray - Host critical stylesheets on the same domain
Vue Scoped Styles Missing
Vue Scoped Styles Missing
Problem: Component scoped styles don’t appear in print.Solutions:
- Ensure components are rendered before printing
- Scoped styles are automatically captured from
<style>tags - If using CSS-in-JS, ensure styles are injected into the DOM
Background Colors Not Printing
Background Colors Not Printing
Problem: Background colors and images don’t appear.Solutions:
This is a browser default behavior. Override with:
Advanced: Style Timing
The plugin uses thetimeout option to ensure styles are fully loaded:
Next Steps
Plugin System
Learn how the plugin architecture works
Bridge Printing
Explore direct printer communication