I have tried to add a Font into fpdf by adding this
$pdf = new FPDF('P', 'mm', 'A4');
$pdf->AddFont('calibri','','calibri.php');
$pdf->AddFont('calibri','B','calibrib.php');
$pdf->AddFont('DejaVuSansMono','','DejaVuSansMono.php');
$pdf->AddFont('DejaVuSansMono','B','DejaVuSansMono-Bold.php');
If I use the font courier
$pdf->SetFont('courier', '', 10);
$pdf->Cell(0, 5, 'Hello world', 0, 1);
and preview the generated pdf in the browser, everything is fine.
If I use e.g. calibri the pdf has to be loaded twice to see the result. On first time it comes with an error like:
Error while loading the PDF-Document
If I load it again, it works and if I load it again it wont work and so on...
What could this be?
Could it be that the font file is to large ?
EDIT:
I have to say, this only appears when I call the PDF from my Preview-Script whats actually is build with jquerys ajax. Adding images to the pdf (e.g. 20MB) does not bring up this problem.
And if I preview, open up the pdf in a other tab, and go back to the first tab and load prieview again also dont brings up the problem.
Related
When I generate and save a document with a link in it using TCPDF. It is always showing a save as dialog before closing in the pdf reader.
"Do you want to save the changes to file.pdf before closing?"
Also there is a yellow note box in the chrome browser when I hover over the link... as shown in the image below.
My code with problem is like this.
$pdf->writeHTML('link');
However, if my links are empty in my html then there is no problem while closing the pdf, also then there is no yellow box anymore in links. The following code for example is working fine.
$pdf->writeHTML('link');
Here is a complete example to reproduce the problem.
require_once __DIR__.'../../external_classes/TCPDF/tcpdf.php';
$pdf = new \TCPDF();
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ABC');
$pdf->SetTitle('ABC');
$pdf->SetSubject('ABC');
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, "ABC", "ABC");
$pdf->setPrintFooter(false);
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, 10, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('helvetica', '', 10);
$pdf->AddPage();
$pdf->writeHTML('link');
ob_clean();
$pdf->Output('example.pdf', 'D');
The above example will generate the output like the following image. Which will have both the problems, I have tested both on tcpdf version 5.9.x and 6.2.x.
Okay, found some explanation to both the problems.
1. First Problem: The issue of Save as Dialog
For some reason the tcpdf library is appending my website html at the end of the pdf. I found this out by viewing my pdf in pure text using a text editor.
Using an exist function immediately after output() will fix it.
$pdf->Output('example.pdf', 'D');
exit();
2. Second Problem: The issue of yellow link box in chrome
This is currently an unresolved active issue in chrome. Check this link for example on mPdf forum.
You may resolve the second problem by removing annotations at all.
Create your own class MyTCPDF extends \TCPDF.
Copy to created class "protected function _putannotsobjs()" from tcpdf.php. You will override it.
Change this string in method form
`$annots .= ' /Contents ' . $this->_textstring($pl['txt'], $annot_obj_id);`
enter code here
to
`$annots .= ' /Contents ()';`
Use your class for generation pdf file.
You may put any logic in this method an disable annotations by constant or something else.
I'm trying to output a pdf file with an italic text in Arial and using FPDI for that.
Since the arial.php and the arialbd.php (bold) are already existing, it's working fine.
Now I found a ariali.ttf on the internet, which looks good when I open the overview of that font (little window with the quick brown fox sentence). But when I use the font in FPDI, it looks like this:
http://i.stack.imgur.com/muTN7.png
Does anybody know, how to fix this?
Is it an ttf issue or where does that strange presentation come from?
Here my example code:
$pdf = new FPDI('P', 'mm', array(210, 297));
$pdf->SetAutoPageBreak(false);
$pdf->AddFont('Arial', 'I', 'ariali.php');
$pdf->AddFont('Arial', '', 'arial.php');
$pdf->AddPage();
$pdf->SetFont("Arial", "I", 16);
$pdf->SetXY(20,20);
$pdf->SetTextColor(0, 0, 0);
$pdf->Cell(20,5,"This is an italic test text in Arial!");
$pdf->Output("test.pdf", "I");
EDIT:
test.pdf
Setasign's question gave me the hint to try to open the resulting PDF in another PDF viewer too than just in my browser (chromium on debian).
The GNOME document viewer Evince is showing a blank page and Adobe Acrobat on Windows is showing nice italic Arial text but giving the error (freely translated from German) "The embedded font "Arial-ItalicMT" could not be taken out. In certain circumstances, some characters won't be printed correctly"
I'm just using my example code and FPDI Version 1.4.4.
The problem was the converter I used to generate the .php and the .z file from an .ttf file.
Always use the makefont scripts, shipped with fpdf/fpdi or use the online makefont: http://www.fpdf.org/makefont/
I need to export html page to pdf file with everything that's written in it, after I press submit button. It will open new page, with info, and I need for script to automatically make .pdf file (already uploaded to webserver), and get the link from file. Could you give me some easy example (if available, without any plugins, or other features that I must download, I would prefer clean PHP).
Just try this
HTML to PDF with PHP
Using open-source HTML2FPDF project
This solution uses HTML2PDF project (sourceforge.net/projects/html2fpdf/). It simply gets a HTML text and generates a PDF file. This project is based upon FPDF script (www.fpdf.org), which is pure PHP, not using the PDFlib or other third party library. Download [HTML2PDF][1], add it to your projects and you can start coding.
Code example
require("html2fpdf.php");
$htmlFile = "your link";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');
I have a doubt about using TCPDF and FPDI together.
I am working on a project where I need to modify existing PDF file and generate new PDF, actually existing PDF are Greeting card template and I have to print certain data at certain pages (such as Image on first page, message on 3rd page, artwork on 4th page) to generate final PDF.
I googled and found with TCPDF, it is not possible to manipulate existing PDF, they suggested using FPDI for opening and manipulating existing PDF.
That's where i am stuck. I need TCPDF (it methods to print images, transparent images, utf text, embed font etc) to do what I want to do, but, I need FPDI to start with. :(.
Please help me: is it possible to use both FPDI and TCPDF together? so that I can use features offered by both APIs ?
Thanks in advance...
http://www.setasign.de/products/pdf-php-solutions/fpdi/about/
"As of version 1.2.1 FPDI can be used with TCPDF - a derivate of FPDF."
libraries_load('tcpdf');
libraries_load('fpdi');
$pdf = new FPDI();
$pdf->setSourceFile("%local_file_path%");
$tplIdx = $pdf->importPage(1);
$pdf->AddPage('L', array(3.5, 2), FALSE);
$pdf->useTemplate($tplIdx, 0, 0, 3.5, 2, false);
I was using Drupal at the time so I used libraries_load but require_once should work.
In a web app developed in PHP we are generating Quotations and Invoices (which are very simple and of single page) using TCPDF lib.
The lib is working just great but it seems to generate very large PDF files. For example in our case it is generating PDF files as large as 4 MB (+/- a few KB).
How to reduce this bloating of PDF files generated by TCPDF?
Here is code snippet that I am using
ob_start();
include('quote_view_bag_pdf.php'); //This file is valid HTML file with PHP code to insert data from DB
$quote = ob_get_contents(); //Capture the content of 'quote_view_bag_pdf.php' file and store in variable
ob_end_clean();
//Code to generate PDF file for this Quote
//This line is to fix a few errors in tcpdf
$k_path_url='';
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
// create new PDF document
$pdf = new TCPDF();
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
// add a page
$pdf->AddPage();
// print html formated text
$pdf->writeHtml($quote, true, 0, true, 0); //Insert Variables contents here.
//Build Out File Name
$pdf_out_file = "pdf/Quote_".$_POST['quote_id']."_.pdf";
//Close and output PDF document
$pdf->Output($pdf_out_file, 'F');
$pdf->Output($pdf_out_file, 'I');
///////////////
enter code here
Hope this code fragment will give some idea?
You need to see what it is putting inside the PDF. Is it embedding lots of images or fonts?
You can examine the contents with lots of PDFtools. If you have Acrobat 9.0, there is a blog article showing how to do this at http://pdf.jpedal.org/java-pdf-blog/bid/10479/Viewing-PDF-objects
Finally I have managed to solve the problem.
The problem was that by mistake I had inserted a link to email id in the web page that was getting rendered to PDF. By just removing this link the size of the generated PDF went down to just 260 kb!
Thanks everyone who tried to help me out in solving this problem.
Current TCPDF version now includes font subsetting by default to dramatically reduce PDF size.
Check the TCPDF website at http://www.tcpdf.org and consult the official forum for further information.