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.
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.
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.
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.
Check the font type you are using, is like html, but it won't fallback.
Create pdf with static text and check.
Related
I am trying to use a custom font in my tcpdf file.
$pdf->addTTFfont('../../../fonts/RedHatDisplay-Regular.ttf', '', '', 32);
addTTFfont does:
TCPDF_FONTS::addTTFfont($fontfile, $fonttype, $enc, $flags, $outpath, $platid, $encid, $addcbbox);
This is my Code.
It just gives me weird chars when creating the pdf. The thing is, if I use the RedHatDisplay-Italic.ttf file, it just works perfect. Do you have any ideas what I can do?
So I had the same issue with the Quicksand font. The issue was the font itself (or the resource where I got it from).
I tried multiple resources, because in the past I downloaded Google Fonts (like Lato) and they worked.
So for Quicksand the downloaded font from
https://google-webfonts-helper.herokuapp.com/fonts failed
https://www.fontmirror.com/quicksand worked
CAUTION It looks like if the font already exists, it won't be overwritten by TCPDF_FONTS::addTTFfont so you need to manually get rid of an existing font first (probably from previous adding in vendor/tecnickcom/tcpdf/fonts) .
Other than that, the following code just works fine:
Only call this code once to add the font
<?php
$fontnames = [];
$fontnames[] = TCPDF_FONTS::addTTFfont('fonts/Quicksand Bold.ttf');
$fontnames[] = TCPDF_FONTS::addTTFfont('fonts/Quicksand Regular.ttf');
// Make sure you use the real font name. Different file names may (or may not) produce different font names.
die(print_r($fontnames, 1));
This code then for generating the pdf
<?php
$pdf = new TCPDF('P', 'mm', 'A4');
$pdf->SetFont('quicksand'); // This is the fontname from above
$pdf->AddPage();
$txt = '0123456789 ABCDEFGHIJKLMNOPQRSTUVQXYZ';
// print a block of text using Write()
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);
$pdf->Output('test.pdf', 'I');
exit(1);
I´m trying to generate a PDF file that contains Urdu text. I have tried several fonts like: Jameel Noori Nastaleeq, Alvi Nastaleeq, Arial Unicode, etc. But some characters do not show properly. When I copy the text from the PDF and paste it to another program like Notepad the text is showed correctly.
I´m using this code to convert the .ttf file to TCPDF format.
require_once('TCPDF/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$fontname = TCPDF_FONTS::addTTFfont('includes/Fonts/JameelNooriNastaleeqRegular.ttf', 'TrueTypeUnicode', '', 32);""
That code created three files: jameelnoorinastaleeq.php, jameelnoorinastaleeq.z, jameelnoorinastaleeq.ctg.z.
Then in the PHP page this is my code:
require('TCPDF/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// set some language dependent data:
$lg = Array();
$lg['a_meta_charset'] = 'UTF-8';
$lg['a_meta_dir'] = 'rtl';
$lg['a_meta_language'] = 'fa';
$lg['w_page'] = 'page';
// set some language-dependent strings (optional)
$pdf->setLanguageArray($lg);
// ---------------------------------------------------------
// set font
$pdf->SetFont('jameelnoorinastaleeq','',12);
// add a page
$pdf->AddPage();
// Urdu content
$htmlurdu = '<span color="#660000">Urdu example:</span><br />احساسات: اکساہٹ(جوش۔تحریک)؛اعصابی؛
<br />احساسات: اُوپر تک کھدائی کا؛"سوراخ کرنا(نقب لگانا)بل کھودنےیا بنانے کا۔؛جڑ سے اکھاڑنے۔کھود کر نکالنےکا""احساس؛"<br />احساسات: اکڑن(سختی)؛اعضاء(بازو اور ٹانگوں)کے جوڑوں کا؛(بے ڈھنگے پن(اناڑی پن)کا؛)
<br />احساسات: اینٹھن(مروڑ۔کھچاؤ)؛بیرونی(خارجی)طورپر؛';
$pdf->WriteHTML($htmlurdu, true, 0, true, 0);
//Close and output PDF document
$pdf->Output('testUrdu.pdf', 'I');
The Urdu Text is showed like this:
???????: ??????(?????????)????????
I have tried using an external website to convert the JameelNooriNastaleeq.ttf file to TCPDF format. I used this site http://fonts.palettize.me/ then copied the generated files to TCPDF/fonts directory but it didn´t work. Urdu text still not showing properly.
I tried the solution posted here: Urdu supported PDF library compatible with php. But there are some Urdu characters that readability is poor with font ARIAL UNICODE MS. I tried to do the same with font Jameel Noori Nastaleeq, but didn´t work. The PDF shows many spaces and very few characters, when I copy the text to another editor it is showed properly.
I have spent several hours trying to fix this problem. When showing English characters there isn´t any problem, but Urdu is not working. Sometimes it displayed "?" other times showed spaces.
Please help me!
I have issue with PDF generated from CKEditor.
I have some text typed in CKEditor:
HTML source:
Next, this text is saved in DB.
My CKEditor config looks like:
Further, above text is fetched from DB in controller, and PDF file is generated using TCPDF:
public function createRegulationPDF(){
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->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set font
$pdf->SetFont('freeserif', '', 10);
// add a page
$pdf->AddPage();
// GET CONTENT FROM DB
$html = $this->view->settings['reg_statement'];
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
....
And the final point, generated PDF:
The question is: what is this weird (the box) selected char?
And how to avoid it?
I am trying to convert a SVG file to PDF file using TCPDF library in PHP. I have created a SVG file and use PHP to replace text and plan to render the resultant SVG file to PDF file.
Any idea, if TCPDF library supports SVG to PDF conversion. Any pointers in this direction would really help me.
You don't really need to replace text or render, Once you have created your svg file. All you just need to include tcpdf in your script and create its object like e.g.
require_once(DOCUMENT_ROOT . '/library/Lib/tcpdf/mypdf.php');
$this->pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->pdf->AddPage(); // Add page to pdf before addding content
//There are several other property need to be set on basis of your need
$this->pdf->ImageSVG('file/mySVGFile.svg', 15, 20, '', '', '',
'', '', 1, false); // 15,20 are co-ordinate to position graph in pdf
Once you added your content to your pdf, Last step comes is to download the pdf using .
$this->pdf->Output('my.pdf', 'FD');
Feel free to ask for anything you have any query in this code.
Try
$pdf->ImageSVG(VIEW_JS."graph_as_svg/".php", '', '', '210', '100', '', '', '', 0, false);
I have html, when I run that html it is working well. But, when I want to create pdf of that html, design changes and I am not getting what design I have.
Also I can not add pages, my design has two page and I want the pdf to be in two pages.
here is my sample code:
require_once('eng.php');
require_once('tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('RoyalHome.ae');
$pdf->SetTitle('Listing ');
$pdf->SetSubject('PDF of Listings');
$pdf->SetKeywords('Royalhome, PDF, listing');
$pdf->SetFont('Helvetica', 'B', 10);
// add a page
$pdf->AddPage();
my html is here fiddle
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->SetFillColor(255,255,0);
$pdf->lastPage();
$sr = $fileName . '.pdf';
$pdf->Output($sr, 'D');
Just tested your code. Ran into the following issues:
You have relative URLs for img tags, which, on my machine, results in tcpdf throwing an error when trying to fetch the images. Make sure you use absolute URLs to avoid this.
You are using the D flag for Output destination. I got errors because headers were already sent. If you are generating and outputting the pdf after the page loads, or if errors are thrown and output to screen, the output will fail.
I removed all <img /> elements and changed the destination flag to F and the pdf generated just fine on my desktop.