Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating PDF with tcpdf shows blank on iphone

Tags:

php

tcpdf

I am using tcpdf to produce PDF's from HTML. Everything is working fine and when I view the PDF on my computer I can see it just fine, but for some reason when I look at the PDF on my iPhone it shows up blank. All I can see are the borders I created that contain values in them but there are no values showing.

Here is my code

require_once('/tcpdf/tcpdf/config/lang/eng.php');
require_once('/tcpdf/tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->setLanguageArray($l);
$pdf->SetFont('dejavusans', '', 10);
$pdf->AddPage();
$html = file_get_contents('http://www.website.com/invoice.php?invoice_id='.$invoice_id);
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();
$pdf->Output('/html/admin/emailattachements/invoice.pdf', 'F');

In that last line. I copy the PDF to that directory when I grab it later with an email script and sends it off to a customer.

Blank PDF

Edit: SOLVED

I discovered it was the font I was using to produce the PDF. iPhone's can't read dejavusans :) I changed it to 'times' and it works fine

Edit: Update

Since this article I have had to create many more PDF's with tcpdf and while I can't really explain why some fonts were not working while others where I recently applied some of the suggestions over at http://www.tcpdf.org/fonts.php and applied

$fontname = $pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);

By adding a font manually and setting the font file path and uploading the file manually I was able to get existing font's that did not work and actually get them to work.

like image 907
Cesar Bielich Avatar asked Jan 17 '13 20:01

Cesar Bielich


2 Answers

Change the below lines:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

$pdf->SetFont('dejavusans', '', 10);

To:

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, false, 'ISO-8859-1', false);

$pdf->SetFont('helvetica', '', 10, '', true);

And then check.

like image 136
user3548394 Avatar answered Sep 28 '22 08:09

user3548394


Not sure how its working for you without the AddFont method, but I had the same issue and adding the fourth param $subset = false fixes it.

$subset (mixed) if true embedd only a subset of the font (stores only the information related to the used characters); if false embedd full font; if 'default' uses the default value set using setFontSubsetting(). This option is valid only for TrueTypeUnicode fonts. If you want to enable users to change the document, set this parameter to false. If you subset the font, the person who receives your PDF would need to have your same font in order to make changes to your PDF. The file size of the PDF would also be smaller because you are embedding only part of a font.

$pdf->AddFont('dejavusans', '', 'dejavusans', false);
$pdf->AddFont('dejavusans', 'B', 'dejavusans', false);

$pdf->SetFont('dejavusans', '', 10);

dejavusans fixes a number of issues with multibyte characters so not possible to simple change to helvetica. By making this change we found the PDF to more than double in size.

This is kinda the same idea as @user3548394 but only have to make this change once.

like image 29
John Magnolia Avatar answered Sep 28 '22 06:09

John Magnolia