how to create print view with A4 page size and fix border per page
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Print Layouts: Creating A4 Pages with Fixed Borders using HTML and CSS
As developers, we often deal with the challenge of taking dynamic web content—generated by PHP or any backend framework—and transforming it into a precisely formatted, printable document. The complexity arises because what looks perfect on screen rarely translates perfectly to paper. Today, we will dive deep into how you can master creating print views with specific dimensions like A4 paper size and fixed borders using pure HTML and CSS, ensuring professional results regardless of the user’s browser.
The CSS Foundation for Print Media
The key to achieving page-specific layouts lies in leveraging CSS features designed specifically for print media, primarily the @page rule and print-specific media queries. While standard margin properties control screen layout, @page controls the physical boundaries of the printed page itself.
1. Creating A4 Page Size Precisely
You cannot force a browser to render the actual physical dimensions of an A4 sheet using only standard CSS units in a predictable way across all environments. However, you can instruct the browser on how to handle the paper size when printing. The most effective approach involves setting the paper size directly within the @page rule.
To target specific page dimensions, we use the @page rule:
@page {
size: A4; /* This tells the browser the desired paper size */
margin: 1cm; /* Setting a standard margin for all four sides of the page */
}
body {
/* Ensure content fills the area defined by @page */
width: 210mm; /* A4 width in millimeters */
height: 297mm; /* A4 height in millimeters */
box-sizing: border-box;
}
A Note on Browser Consistency: While this syntax is powerful, the final visual output heavily depends on the user's printer driver settings. For absolute consistency across all systems, ensure your PHP generation outputs clean HTML that relies on these standard print instructions. This robust approach to presentation management aligns with the principles of building scalable applications, much like how robust backend logic in frameworks like Laravel ensures predictable data handling.
2. Implementing Fixed Borders and Margins Per Page
For creating fixed borders around the content area, we combine the @page settings with standard margin properties on the main container element. To ensure consistent spacing (borders) regardless of the page size, defining margins within the @page block is crucial.
To create a distinct border effect for each printed page, you typically apply styles to the content wrapper (body or a specific .print-container).
/* Styles applied specifically when printing */
@media print {
@page {
size: A4;
/* Define consistent margins/borders around the printable area */
margin: 2cm; /* This creates a 2cm border/gutter on all sides */
}
body {
/* Reset default browser margins for cleaner printing */
margin: 0;
padding: 0;
/* Ensure the content container respects the page boundaries */
width: 100%;
height: 100%;
}
.print-area {
border: 3px solid #333; /* Fixed border for the main content area */
padding: 15px; /* Internal padding inside the border */
box-sizing: border-box;
}
}
By wrapping your main content in a class like .print-area and applying the media query, you ensure that when the print dialog opens, everything is neatly contained within the defined A4 bounds with consistent borders.
3. The Role of PHP in Dynamic Generation
PHP plays an essential role here: it generates the base HTML structure, including the necessary CSS classes, dynamically based on your data. Since Laravel and other modern frameworks focus heavily on predictable state management, using PHP to construct this complex HTML ensures that the presentation layer is always aligned with the data layer. You use PHP to inject the specific content into the template defined by your print-specific CSS.
Conclusion: Achieving Print Perfection
Creating a truly fixed print view requires understanding the separation between screen rendering and print rendering. By mastering the @page rule in conjunction with the @media print block, you gain granular control over how HTML is interpreted by the printing mechanism. While browser interpretation always has some variability based on the final printer driver, this technique provides the most reliable foundation for generating professional, fixed-size documents from dynamic PHP data. Focus on clean CSS and robust backend generation to achieve perfect print results every time.