There is an invoice preview webpage. I want to set header and footer information off while printing it. I know we can edit the page setup settings. But I wanted to know if we can do something without changing the default page setup settings. With default setting inside page setup, webpage title and webpage path are displayed for header and footer respectively.

Thanks

Dani AI

Generated

For an invoice preview you need to separate two things: the browser-added print header/footer (page title, URL, page numbers) and any header/footer you place in the page itself. 's advice to use a print stylesheet is the right way to control on-page elements, and is correct that you cannot change the browser's print-dialog defaults from PHP or client JS. The notes below expand on practical workarounds and which to choose for invoices.

Browsers insert their own header/footer outside the page DOM and authors cannot reliably disable those lines from a web page. The CSS Paged Media spec defines margin boxes for print, but mainstream browsers do not let pages override the browser UI; paged-media features are implemented by specialized renderers instead. See the spec for background: CSS Page Module.

Practical options

  • Keep a print stylesheet for a clean invoice layout (hide on-page chrome, show only invoice content). This controls what the user prints, but not the browser-inserted metadata.
  • For full control and a consistent printable file, generate a PDF server-side and offer it for download/print. PDFs give exact margins and content and avoid browser header/footer quirks. Common PHP-friendly tools: wkhtmltopdf, Dompdf, mPDF. Example (Dompdf):
    
    use Dompdf\Dompdf;

$dompdf = new Dompdf();
$dompdf->loadHtml($html); // $html = your invoice markup
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream('invoice.pdf', ['Attachment' => false]);



Test across browsers and platforms. If distributing an HTML print button, add a short note telling users how to disable "Headers and footers" in their print dialog as a fallback. For invoices that must print identically every time, server-generated PDF is the most reliable solution.

Recommended Answers

All 3 Replies

.header { width: /* etc */ }
.footer { /* bla bla */ }
.printheader { width: /* etc */ }
.printfooter { /* bla bla */ }
@media print { 
.header {display:none;}
.footer { display:none; }
.printfooter {display:block;}
.printheader {display:block;} 
}
@media screen { 
.header {display:block;}
.footer { display:block; }
.printfooter {display:none;}
.printheader {display:none;} 
}

segments of css, defines headers and footers for print or screen and toggles between which ones are displayed depending on output, great for including copyright messages on printed version of the page

@media print {.header {display:none;}
.footer{display:none;}}

simple version just hides class='header' and class='footer' when printing

ignore above, misinterpreted question,

This is not possible, PHP cannot have any influence over the print settings. The best you can do is supply a print stylesheet to change the appearance of a page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.