mPDF only getting "Finished" when progressBar enabled - php

I'm generating a very large pdf file on my PHP application. The document can reach over 300 pages. The build process may take more than 40 seconds, that's why I want to show the user a message to understand that the file is being generated.
The problem is that when i set $this->progressBar = 1; on config.php file, the progress shows perfect, but when the proces finish, I'm only getting a Finished message, and i'm not redirected to the file.
Heres' my code:
<?php
ob_start();
include('include/mpdf/mpdf.php');
define('_MPDF_URI','../');
$mpdf = new mPDF('','A4', 9, 'freesans', 10, 10, 20, 15, 5, 5, 'L');
$mpdf->SetHTMLHeader($cabecera);
$mpdf->setFooter("Pagina {PAGENO} de {nb}");
$html=utf8_encode($html);
$mpdf->WriteHTML($html);
$mpdf->Output();
ob_end_flush();
I believe someone already has gone through this problem. Please guide me on how I can solve this problem.
Thank you,

The documentation has a note:
Note: You may need to define the constant _MPDF_URI if you use progress bars. The constant _MPDF_URI is needed to redirect the user
to the PDF file (and prior to mPDF 5.0 to locate a javascript file
within the progress bar script). It must be either a relative path
(e.g. '../') or a full URI (e.g. 'http://www.example.com/mpdf/'). If
you do not define it before calling mPDF() mPDF will assign it the
same value as _MPDF_PATH. This is fine if you have used a relative
path. _MPDF_PATH requires either a relative path or a filesystem real
path (e.g. '/homepages/27/d84233457/htdocs/')
Then make sure the ->Output() function has the correct parameters, such as:
$mpdf->Output('yourFileName.pdf', 'I');
For more info see this stack answer.

Related

PDF manipulation - images are distorted after few consecutive operations on PDF file

I've run into this weird issue with PDF file handling. Not sure if SO is the right place to ask this, but I couldn't find any specific sites for this. I hope that someone can shed some light on the issue.
This happens with the following specific process, if some of steps are omitted - the issue is not observed.
I have a PHP application that serves PDF files to users. These files are created by authors in MS Word 2007, then printed to protected PDF (using pdf995, most likely, I can confirm if needed).
I'll call this initial PDF file as 'source' hereinafter.
Upon request, the source file is processed in PHP the following way:
we decrypt it using qpdf:
qpdf --decrypt "source.pdf" "tmp_output.pdf"
Then we add security label / wartermark to it, encrypt and output to browser using mPDF 6.0:
$mpdf = new mPDF();
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile($fpath);
if ($pagecount) {
for ($i=1;$i<=$pagecount;$i++){
$tplId = $mpdf->ImportPage($i);
$mpdf->UseTemplate($tplId);
$html = '[security label / watermark contents...]';
$mpdf->WriteHTML($html);
}
}
$mpdf->SetProtection(array('copy','print'), '', 'password',128);
$mpdf->Output('final_output.pdf','I');
With the exact steps described above, images in the output that were pasted in the Word doc appear as follows:
In the source PDF, tmp_output (qpdf decrypted file) the pasted images look correct:
The distortion doesn't take place if any of the following occurs:
Word doc printed to PDF without protection
mPDF output is not protected.
As you can see there too many factors, so I don't know where to look for a bug.
Each component works correctly on it's own and I cannot find any info on the issue. Any insights are greatly appreciated.
EDIT 1
After some more testing, it appears that this only happens to screenshots taken from web browser, Windows explorer, MS Word. Cannot reproduce this with screenshots from Gimp.
It appears that something along the way attempts to convert white to alpha and fails.
The current version (6.1) of Mpdf has a bug which does not handle escaped PDF strings (imported via FPDI) correct if they should be encrypted.
A pull request, which fixes this issue is available here.

using PHP to fill in a PDF

I need to fill in a PDF, in the fly, using PHP - have no idea where to start.
I'm currently doing quizzes on line using PHP - once a series of quizzes are passed the client wants to let the user download a 'certificate of completion'
The PDF of the certificate has blank lines for the users name and the area of study.
My thought is - add 2 form elements to the PDF, and have PHP pill them in when I pull of the certificate.
BUT HOW?
Is there a different, better way?
Things I need to be 'cautious' of - installing third party stuff is not reliable, UNLESS I can just drop a lib in the the site root. I can't guarantee the hosting provider will let me change a PHP config.
Any help is appreciated - the more specific, the better.
Sorry I have no code at the moment - other than how I'm currently displaying the PDF -
// show cert
echo '<iframe src="1_cert.pdf" width="1000" height="700">';
Thanks.
OK - using TCPDF - as suggested - I've installed, and example pages work...
I've placed a file in the example folder.... I've included a call to import...
require_once('../tcpdf_import.php');
// create new PDF document
$pdf = new TCPDF_IMPORT('1_cert.pdf');
...other boiler plate copied form other examples...
$pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');
// set font
$pdf->SetFont('times', 'B', 20);
$pdf->setPage(1, true);
$pdf->SetY(50);
$pdf->Cell(0, 0, 'test text', 1, 1, 'C');
$pdf->lastPage();
The error I'm getting "TCPDF ERROR: Wrong page number on setPage() function: 1"
Ok, if you're not showing code, then neither will I. ;)
I would recommend doing it with TCPDF.
Import the PDF with TCPDF_Import
In a PDF document, you can navigate to any x/y position, (NB: 0/0 is bottom left). Therefore, simply set your “cursor” to the position to each field, and insert a text with either the Cell or the writeHTMLCell method.
Save the PDF document.
Display it to the user.
Voilà.
By the way, both FPDI and TCPDF are common PHP libraries, so you can just put them somewhere in your base folder, no additional tools should be required on a common web server.

TCPDF, "Could not include font definition file" with OpenType fonts

I am a web programmer with no in-depth knowledge of fonts and am struggling to get TCPDF to include our custom OpenType font. We have bought OpenType font files (.oft), which are not protected by any kind of DRM.
A lot of questions regarding this error message end up getting the same advice. I've set the correct file permissions for the folders used by TCPDF (755) and I have no trouble using the addTTFfont() to including .ttf TrueType fonts like so:
$pdf->addTTFfont('/path-to-font/DejaVuSans.ttf', 'TrueTypeUnicode', '', 32);
So I've made up the following code to include our OpenFont type. The addTTFfont() documentation seems to indicate support of OpenTypeUnicode and OpenType parameters.
$pdf->addTTFfont('/path-to-font/customfont.otf', 'OpenTypeUnicode', '', 96);
Which results in:
TCPDF ERROR: Could not include font definition file:
We're using TCPDF v6.0.020 and I've been reading the TCPDF Fonts information page with no luck so far. I've noticed TCPDF also has the addFont() function (documentation here) which seems more obvious to use as it does not include any reference to any font type. However, I was unable to get this function to work with the little documentation it has.
Any help would be greatly appreciated.
Are you sure that you are getting that error when calling addTTFfont()? The reason I ask is because I checked the TCPDF code (just did a global search on "Could not include font definition file") and that message only appears in the addFont() method.
These two methods can be a bit confusing, but I wrote myself some notes a few months ago, which I will partially reproduce below in the hope that it helps you somehow:
addTTFfont() - The primary function of this method is to convert a font from TTF (or OTF) to the "raw" version that TCPDF needs. The way this function is implemented you could, in theory, use it as your primary method of adding fonts to a document. It will check the tcpdf font folder first and if the converted files aren't there it will go ahead and do the conversion. It is only a little bit more overhead, but still not my preferred method of adding fonts to files, as you need to know what style of font you are converting for the process to even work successfully. IMO, it is better to use this method to pre-convert any fonts that you plan on using and simply use addFont() to add the "raw" versions to the document.
AddFont() - This adds a "raw" (ie. already converted) font to the document, which means it is then available for writing text.
SetFont() - This sets the font for the next chunk of text that you write.
So I would use addTTFfont() to pre-convert the font to the "raw" version, then use addFont() and setFont() in the code that actually creates the PDF.
If addFont() is failing with the error message above, it means it cannot find the font definition file. Keep in mind that if you call addFont() with style set ('i', 'b', 'bi', etc), all it does is append this to the file name (before the extension).
Most importantly, you need to make sure that your call to addTTFFont() is producing the "raw" font files and saving them into your fonts folder. There should be three files per style, with extensions of .php, .z and .ctg.z. So if you converted a font called blah.ttf you will end up with blah.php, blah.z and blah.ctg.z. If you convert blah bold.ttf, TCPDF will figure out that it is a bold font and append the 'b' to the end of the file names: blahb.php, blahb.z and blahb.ctg.z.
Hopefully there will be some nugget in here that will help! Good luck!
TCPDF New Font add and turkish char problem solved method.
Use Converter Link:
http://fonts.snm-portal.com
Download 3 files,
Copy TCPDF-master/fonts
Useage: $pdf->SetFont('roboto', '', 14);
To solve this problem with generating font.
Make sure all path are correct.
Use this link
Remember to put folder make_font inside folder of html2pdf
Remember about correct path to: html2pdf.class.php
inside folder make_font in index.php
or
If you have other problem - font: Could not include font definition file:
It's a problem of correct name of your new font.
For example using when generating pdf:
Correct: $html2pdf->addFont('lato i', '', 'latoi.php');
Incorrect: $html2pdf->addFont('lato', '', 'latoi.php');
Remember there are only 2 files generated:
latoi.php
latoi.z
You don't need any ctg file.
I hope it will help
In my case the problem was, that the fonts path wasn't writable. I fixed it by changing permissions of the folder: var_dump(K_PATH_FONTS);
In my case, it was simply a wrong path to the (no more found) .ttf font file.
For a test I used the drive D:/ successfully, but I forgot to change it back to the prod drive C:/ in my php file in the code line where I use the addTTFfont('C:/...') command.
Unfortunately, the TCPDF error message doesn't output anything about the source file and the code line producing the problem.
add the dejavusans.php file into the font folder into the tcpdf library into the codeingiter library folder.
"cid0jp" this font supports Japanese and Chinese.

what should I do to be able to create large PDF files without error?

I'm creating a pdf file on fly using fpdf of PHP from the database but when there are a lot of data in my db , either the file cant be created completely and just half of it comes out, or nothing will happen or I get run-time error what should I do to be able to create large PDF files without error?
large pdf :
The code is like :
require('lib\fpdf.php');
require ('lib\pdf.php');
include ('lib\bukaDb.php');
$pdf = new PDF('P','pt','A4');
$pdf->SetFont('Arial','',8);
$pdf->AliasNbPages();
$pdf->Open();
$pdf->setY($pdf->tMargin);
for ($i=0; $i<count($var); $i++){
//Do something , show data
//$pdf->Ln(20);
}
// Finally, produce the PDF!!
$pdf->Output();
And my table has hundreds of rows, I cant use set time limit function, any other recommendation?
Your script is probably timing out. Try disabling the built-in time limit:
set_time_limit(0);
If you host your site or app on a shared-hosting server though, make sure you are allowed to do this. Some web hosters (including mine) frown upon using lots of server resources for tasks like this.
We create PDF files (using fpdf) with hundreds of pages without any problem, so I guess it's your code that is collecting / displaying data that causes the request to time out.
When your HTTP request takes long to complete (more than 10 seconds) then it's probably your script simply taking too much time to complete.
I really think the problem is what happens inside that for loop. Try to create a simple dummy PDF with about the same amount of pages. If that works, you know that you have to search the problem inside the for loop.
Move you script to crontab and set saved pdf as file. Once your script finished, set it to send email telling you that the process finished.
As rough pseudo-code:
require('lib\fpdf.php');
require ('lib\pdf.php');
include ('lib\bukaDb.php');
$pdf = new PDF('P','pt','A4');
$pdf->SetFont('Arial','',8);
$pdf->AliasNbPages();
$pdf->Open();
$pdf->setY($pdf->tMargin);
for ($i=0; $i<count($var); $i++){
//Do something , show data
//$pdf->Ln(20);
}
// Finally, produce the PDF!!
$savefile = "/path/to/output/dir/output.pdf";
$pdf->Output($savefile,'I');
mail('admin#example.com', 'PDF Generator', "PDF has been generated at $savefile");
Then, all you have to do is, save it to somewhere on your server and set it's path as crontab:
/path/to/php/cli/php /path/to/your/saved/php/pdf_generator.php
Please note that you have to:
make sure your $savefile directory is writable by script.
use php cli, not by accessing script via browser as it will also affected by time limit.

FPDF error: Missing or incorrect image file

I am using the FPDF library for generating PDF files by PHP. This library is working fine with plain text(i.e PDF files are generating for any text), but this is giving me an error of FPDF error: Missing or incorrect image file:{MY_FILE_PATH} while trying to add an image to my PDF page. Accessed that file path through the browser, then the corresponding image is appearing fine.
My code is:
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Write(10, 'Example image');
$image_path = MY_FILE_PATH; // This variable contains my actual file path
$pdf->Image($image_path, 10, 10, 350, 450);
$pdf->Output();
This code is working perfectly in my localhost and generating the corresponding PDF files even with the images also, but this is not working after moving to the server.
I have tried with these possibilities like:
With absolute and relative paths for the image.
Tried with a local image placed in the same folder.
All image formats like jpg, png and gif also.
Checked the permissions for the image and corresponding folders also.
None of these cases are working for me, stuck with this problem, can anyone help me to slove this issue.
Thanks!
Siva ...
After a long struggle finally I found the reason for this issue which I've already discussed here.
Mainly this problem is due to the 'allow_url_fopen' setting which is not enabled for my server, I've solved this issue by using the CURL. I'm giving the step by step procedure to solve this issue because this may useful for somebody like me to avoid wastage of time(for finding the solution).
1. Create a temporary file in the local system with write permission.
Ex: $fp = #fopen($local_file_path, 'x');
fclose($fp);
2. Get the remote location file contents using CURL and write that to local file
which is created in the previous step.
Ex: $curl = new CurlWrapper($remote_file_path);
$curl->copyToFile($local_file_path);
3. Now send this local file path to FPDF function(image) then the corresponding
PDF will get generate for the file without any issues.
I think this is one method to solve this issue, if anybody knows some other ways then you can post them here so that this question may be useful for somebody.
just add:
allow_url_fopen = ON
on php.ini (create new if not exists) solves the problem.
http://www.solo-technology.com/blog/2010/04/07/quick-fix-for-url-file-access-is-disabled-issues/
anyone can use this code for solve. may be it will work. because sometime some case may be different. in my case i was getting same issue but after use it. i fixed my issue..
$pdf->Image($image_path, 10, 10, 350, 450 ,'');
try to give empty string to 6 parameter of $pdf->Image() function or you can give file extension like ( PNG or JPG). in my case empty string '' was working. because i was cropping image so that image going some time wrong. so after do that my problem gone fixed and i tested with different images.

Categories