I've tried every conceivable combination of getting something that seems so simple to work.
I need to place an image on a page, A4 (210mm (h) X 297mm (w)) and have that image 100% height, i.e. 297mm, then stretch the width proportionally to that and just crop the overflow (i.e. hidden in css overflow). I've tried every combination I can think of with $mpdf->Image()
, or like I've done everywhere else in the PDF file, use pure HTML and CSS. E.g.
<img src="path(to/file.jpg" />
or
<div style="background: url("path(to/file.jpg") center center no-repeat;"></div>
again, with all possible CSS configurations I can think of.
Is it not possible to stretch and image to fit the entire pages height while maintaining the aspect ratio and crop the image on the sides?
I've seen background-image-resize
on MPDF full page background, but again, nothing.
How can I get an image to be 100% of the page's height (I don't care if I have to define the height (i.e. 297mm) or if it's a percentage) have the image scale proportionally to that and crop any excess of the image on the sides.
I'm sure I've just missed something obvious here. I can't see what I'm doing wrong (I think I'm stuck in a loop in my head now).
An example of what shows up fine (as expected) if I do an echo and view in the browser
$html = '<div style="
background: url('.$imageSource.') center center no-repeat;
position: absolute;
overflow: hidden;
height: 297mm;
width: 100%;
background-size: cover;
top: 0;
left: 0;
"></div>';
However, doing the same with $pdf->WriteHTML( $html, 2 )
and then $pdf->Output()
the image is 297mm in height, but the width is distorted (i.e. it doesn't stretch proportionally to the height).
Here is the resulting PDF (with a placeholder image)
This is what I'm trying to achieve
So the resulting PDF gets "squeezed". Instead of extending the width proportionally to the height (297mm).
PS. Sorry for the lack of actual tested code. But there are just so many different combinations I've tried that I can't reproduce all of them.
PPS. Using latest version of mPDF. Grabbed from GitHub three days ago.