Reputation: 51
I'm trying to save a pdf with some data from database and some images. I can save normally but images are not showing up. $ft1, $ft2 and $ft3 are database variables with image's names.
Here is my test code:
require_once('../tcpdf/config/lang/bra.php');
require_once('../tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Nicola Asuni');
$pdf->SetTitle('TCPDF Example 006');
$pdf->SetSubject('TCPDF Tutorial');
$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
//set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
//set some language-dependent strings
$pdf->setLanguageArray($l);
// ---------------------------------------------------------
// set font
$pdf->SetFont('helvetica', '', 10);
// add a page
$pdf->AddPage();
$html.= "<div><img src='../uploads/".$ft1."' width='200' height='400' /><br><br><img src='../uploads/".$ft2."' /><br><br><img src='../../uploads/".$ft3."' /></div>";
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
ob_clean();
$pdf->Output('../uploads/'.$norepeat.'_relatorio_'.$idcontrato.'_'.formatDataJustNumbers($_POST['datarelatorio']).'.pdf', 'F');
Upvotes: 4
Views: 17268
Reputation: 65
I was running jobs to generate pdfs from HTML templates using tcpdf with a php-fpm docker image. I discovered that tcpdf appends $_SERVER['DOCUMENT_ROOT'] for image src values that start with '/'. This resulted in no images showing in the pdf.
The work around was to set $_SERVER['DOCUMENT_ROOT'] to an empty string.
$_SERVER['DOCUMENT_ROOT'] = "";
Upvotes: 0
Reputation: 349
Check the bar before your absolut path
For me the way to fix the image not displaying in the PDF was to remove the bar from the begining of he absolut path.
Example
Wrong for me:
$image_file = '/assets/img/setor9.png';
$this->Image($image_file, 165, 20, 25, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
Right:
$image_file = 'assets/img/setor9.png';
$this->Image($image_file, 165, 20, 25, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
Upvotes: 0
Reputation: 337
Old post, but I came here looking for help:
Official documentation states that all HTML attributes must be enclosed with double quotes.
width='200' height='400'
Needs to be:
width="200" height="400"
in order for the images to show. That fixed it for me.
REFERENCE: https://tcpdf.org/docs/srcdoc/TCPDF/class-TCPDF/#_writeHTML
Upvotes: 4
Reputation: 418
there are 2 problems;
please change this line;
$html.= "
";
in to this;
$html.= "<div><img src='../uploads/$ft1' width='200' height='400' /><br><br><img src='../uploads/$ft2' /><br><br><img src='../../uploads/$ft3' /></div>";
Upvotes: 1