Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print an image completely filling an A4 or other format?

Tags:

html

css

image

I have a little issue if I print images with Chrome or Firefox. I haven't found how to get rid of white borders in portrait or landscape. This stackoverflow-solution seems to not works for me sadly.

Currently I use this code:

    echo "<body onload=\"window.print()\" onfocus=\"window.close()\">";

    echo "<style type=\"text/css\" media=\"print\">";
    echo "@page { size: auto; }";
    echo "</style>";

    echo "<img src=\"". $dirname . '/' . $photo ."\">";

I get the same result as drag and drop it from windows folder to internet navigator like this exemple:

Chrome is set as: - Landscape - Colors - A4 - Marges none

bug

like image 302
BackTrack57 Avatar asked Jan 08 '23 02:01

BackTrack57


1 Answers

try to apply the container css :

@page {
  size: A4;
  margin: 0;
}
@media print {
  html, body {
    width: 210mm;
    height: 297mm;
  }
}

then instead of using img tag use background image + size cover

body {
    background-image:  url(images/background.svg);
    background-size:   cover;                      /* <------ */
    background-repeat: no-repeat;
    background-position: center center;            /* optionally, center the image */
}

source:

  • page size
  • background image size
like image 109
Jeffrey Nicholson Carré Avatar answered Apr 30 '23 03:04

Jeffrey Nicholson Carré