R2D2
R2D2

Reputation: 2640

TCPDF - adding a background to a table cell or div that shows on PDF

I am using TCPDF to create PDF files on the fly on my website.

This is all working well, and so I would be reluctant to change and use something else now.

But my problem I have just now is that I need to include an image in the PDF file, which will be of unknown size, dimensions, aspect ratio, etc.

My solution for adding this was going to be to add it as a background to a table cell or to a div, and use the 'cover' property to add the image.

This works fine in HTML and looks the way I want it to.

But adding it to the PDF file results in a blank cell or blank div, and no background is added.

I have tried both of the following methods:

$html .= "<div align=\"center\">\n";
$html .= "<table cellpadding=\"0\" cellspacing=\"0\" style=\"width:200px; height:200px; border:2px solid black;\">\n";
$html .= "<tr><td style=\"width:200px; height:200px; background: url(model.jpg) 50% 0 no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;\"></td></tr>\n";
$html .= "</table>\n";
$html .= "</div>\n";

$html .= "<div align=\"center\">\n";
$html .= "<div style=\"width:200px; height:200px; border:2px solid black; background: url(model.jpg) 50% 0 no-repeat; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;\"></div>\n";
$html .= "</div>\n";

Both of these show up the solid border, but no background.

There is one example on the TCPDF site about adding backgrounds, but it adds a background that covers the whole page, which is not what I want.

Can anyone help me get these background images showing up on the created PDF, or suggest an alternative way I can add an image to the document that needs to be a fixed size on the page, but where the source image is of unknown dimensions and aspect ratio?

Upvotes: 2

Views: 15581

Answers (2)

Shehary
Shehary

Reputation: 9992

TCPDF has a very limited CSS support. It doesn't support all attributes.

Currently, only the following CSS attributes are supported:

  • font-family
  • font-weight
  • font-style
  • color
  • background-color
  • text-decoration
  • width
  • height
  • text-align

So, As far I can understand what you trying to achive the way you are tyring to achive is not possible and there is no way you can add background unless you use this example TCPDF Background which you don't want to use;

So Alternatives to TCPDF that are definitely worth trying is: domPDF

Upvotes: 5

R2D2
R2D2

Reputation: 2640

I wanted to stick with TCPDF, as I was also using that in other places and it was working well.

How I managed to do this in the end was to do a two fold process.

Firstly I used PHP to resize and crop the image to the correct aspect ratio for the PDF.

Then secondly I used TCPDF to add the image as standard, not as a background image.

This now all works fine and looks the way I want it to.

Upvotes: 1

Related Questions