Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to maintain image quality with FPDF and PHP?

Tags:

php

image

fpdf

I'm using FPDF with PHP to add an image to a PDF. But the image quality in the PDF is much worse than the original image, as you can see here:

Print screen from web pagePrint screen from PDF

Relevant code:

$image_height = 40;
$image_width = 40;
$pdf = new FPDF();
$pdf->AddPage();
$start_x = $pdf->GetX();
$start_y = $pdf->GetY();
$pdf->Image('./images/ds_pexeso_ros_0_17.jpg', $pdf->GetX(), $pdf->GetY(), $image_height, $image_width); 
$pdf->Output("pexeso".date("Y-m-d"),"I");

The original image is 150x150 pixels.

like image 538
chejnik Avatar asked Apr 06 '12 07:04

chejnik


People also ask

How do I display an image in FPDF?

More videos on YouTube php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->Image('images/pdf-header. jpg',0,0); $pdf->Output(); ?> The ouput of above code is here . ( Show Output ) We can add position with height, width and link to above code.

What is use of FPDF in php?

FPDF is a PHP class which allows generating PDF files with PHP code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files.

What is FPDF error?

The FPDF Error Message will point you to the PHP Line that is sending some content. If you get no hint what File & Line send some content you probably have an encoding mismatch in your include / require Files. For me. fpdf.php was ANSI-encoded, my pdf-generator.php was UTF-8-encoded and.

How do I set page breaks in FPDF?

php require('fpdf. php'); $pdf = new FPDF('P','mm','A5'); $pdf->AddPage('P'); $pdf->SetDisplayMode(real,'default'); $pdf->SetFont('Arial','',10); $txt = file_get_contents('comments.


2 Answers

I think the problem could be related to:

 $image_height = 40;
 $image_width = 40;

With these two instructions your are setting the dimensions the image will have in the pdf.

But if the original image is bigger than 40x40 the scaling of the image can cause quality problem.

So what i suggest:

  • Do a correct resize of the image (php provides GD library). Resize it to 40x40. The GD function imagecopyresampled is your friend: resize and resample the image! Complete reference: http://www.php.net/manual/en/function.imagecopyresampled.php
  • Insert now the image in the pdf
like image 152
ab_dev86 Avatar answered Sep 28 '22 03:09

ab_dev86


I faced the same problem in projects for customers. Blurry pictures in a generated pdf document even with hires images.

It took me a couple of hours, but this is what worked for me.

I have a taken a look at the code and saw that there was a scale factor being set in the constructor of the pdf document:

//Scale factor
if($unit=='pt')
    $this->k=1;
elseif($unit=='mm')
    $this->k=72/25.4;
elseif($unit=='cm')
    $this->k=72/2.54;
elseif($unit=='in')
    $this->k=72;
else
    $this->Error('Incorrect unit: '.$unit);

The scalefactor is depending on the value given in the constructor of the pdf document:

function FPDF($orientation='P',$unit='mm',$format='A4')

The default is 'mm'. In most of my documents I initiate a pdf document like:

$pdf = new PDF('P');

This means that there will be a scalefactor of 72/25.4 = 2.83 used. When I placed an image before I just used:

$this->Image('path/to/file', 0, 0);

This way I got the blurry images. It is also possible to give the width of the image in the command

$this->Image('path/to/file', 0, 0, 200); // for a image width 200

This gave me an image that was far too large. But - and here comes the trick - when you divide the real width by the scalefactor (in my case 2.83) and put this in this statement it gives a perfectly sharp image:

$this->Image('path/to/file', 0, 0, 71); // for a image width 200 / 2.83 = app 71

I hope this works for you too!

like image 37
eheydenr Avatar answered Sep 28 '22 03:09

eheydenr