Hello everyone..
How to print web page in PHP
without Header and footer when print button is clicked by user

Thanks in advance

Dani AI

Generated

Short answer for : handle printing with a print stylesheet (CSS) and @page for orientation — not with PHP. was right to point to CSS; ’s “separate print page” is one workable pattern, but you can usually keep one page and use a print-only stylesheet to hide the on‑screen header/footer and adjust layout for printing. (developer.mozilla.org)

Practical print CSS you can apply (place in a file linked with media="print" or inside an @media print block):

@page {
  size: landscape;
  margin: 10mm;
}

@media print {
  /* hide site chrome when printing */
  header, nav, footer, .site-header, .site-footer, .no-print {
    display: none !important;
  }

  /* make content use printable area */
  main, .content { margin: 0; width: 100%; }
}

The @page size / page-orientation rules request landscape pages, but browser support and exact behavior vary, so test in your target browser(s). (developer.mozilla.org)

Important caveat (echoing ): the browser’s built‑in print headers/footers (URL, date, page number) are added by the browser UI and are not something a page can reliably switch off for every user. Users must disable those options in the print dialog (or browser preferences). If you must guarantee a zero‑click, pixel‑accurate print output for everyone, render a PDF server‑side (wkhtmltopdf, headless Chrome / Puppeteer, etc.) and serve/print that instead. (support.mozilla.org)

A couple of troubleshooting tips: 1) small page margins or @page { margin: 0 } can behave oddly across browsers and might trigger different UI behavior in Chrome — test with your printers; 2) printers have unprintable edges, so avoid relying on margin: 0 to guarantee no clipping. For fully deterministic results, generate a PDF on the server and print that. (stackoverflow.com)

Recommended Answers

All 8 Replies

Why would you want to print in PHP ?

For a special print style, you can use CSS.

You cant determine to what I set my header and footer, nor should you be able to access my hardware settings, think for a moment

I have generated a report...in a php page...
The report has to be printed in landscape mode
but
each time i have to set in into page setup
.and have to remove header and footers...
.
Please tell me the css code to do that

There are few steps to print only content on pages

Is your page have dynamic and static contents, well whats type of that contents follow these 2 simple steps

1. create a page without header and footer

That is because when we use print command in webpage whole page with header and footer will print, this is more simple to create a PHP page without any header and footer, only inner contents.
2. use this code to print that page

echo "<a href='javascript:window.print()'>[Click to Print This Page]</a>";

You cannot set the printer driver's page layout like that.

For the CSS see .

Problem is to print a web page without any header and footer, 'print()' code will print web page as it is looks on the screen. That's why we have to create a new page without header and footer so that these two things didn't show in that page and can get print only contents of that page

Not true. Using CSS you can hide this when printing the page.

That is an other way to create div's for header and footer and on print command we can hide these through css, and even can some JavaScript, i just give the simple solution without creating new css or writing any JavaScriptto create new page. Well we can also create only one css for all header and footer,"named as media_css" and use on the pages where we want to print the page, when the print command executed "media_css" will automatically applied and hide header and footer

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.