FPDF error: Missing or incorrect image file - php

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.

Related

Imagick fails to read pdf files or takes extremely too long time to read it

I want to get a PDF thumbnail by converting the first page of the pdf file to an image using PHP, so I tried this code:
<?php
$image = new Imagick(realpath('my.pdf')."[0]");
$image->setResolution( 300, 300 );
$image->setImageFormat( "png" );
$image->writeImage('my.png');
?>
when I run the above code in Xampp, the page would load for a long time then I get this error
The connection to the server was reset while the page was loading.
I thought that the pdf file is too large, so I used a smaller pdf (13KB) and it didn't work.
then I started to check which line causes this lag, so I omitted all lines and left this one only:
$image = new Imagick(realpath('my.pdf')."[0]");
then I found out that this line is causing the trouble, so I thought that the problem might be because Imagick is not properly installed, but I checked and it seems okay.
I tried to open an image instead:
$image = new Imagick(realpath('my.jpg'));
and it worked perfectly.
so, where is the problem and how to solve it?
Thanks for the help in advance :)

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.

With PHP, how can I check if a PDF file has errors

I have a DB system built in PHP/MySql. I'm fairly new at this. The system allows the user to upload an invoice. Others give permission to pay the invoice. The accounting person uploads the check. After check is uploaded, it generates a PDF as a cover, then uses PDFTK (using Ben Squire's PDFTK-PHP-Library) to combine all of the files together and present the user with a single PDF to download.
Some users upload PDF files which cause PDFTK to hang indefinitely when it tries to combine the PDF with others (but most of the time it works fine). No returned error, just hangs. In order to get back onto the sytem, user must clear cache and re-log in. There are no error messages logged by the server, it just freezes. The only difference I can find in the files that do or do not work in looking at them with Acrobat is that the bad files are legal sized (8.5 x 14) ... but if I create my own legal sized file and try that, it works fine.
Using Putty I've gone to command line and replicated the same problem, PDFTK can't read the file, it hangs on the command line as well. I tried using PDFMerge which uses FPDF to combine the files and get an error with the file as well (The error I get back from this is: FPDF error: Unable to find object (4, 0) at expected location). On the command line I was able to use ImageMagick to convert PDF to JPG, but it gives me an error: "Warning: File has an invalid xref entry: 2. Rebuilding xref table." and then it converts it to a jpg but gives a few other less helpful warnings.
If I could get PHP to check the PDF file to determine if is valid without hanging the system, I could use ImageMagick to convert the file and then convert it back to a PDF, but I don't want to do this to all files. How can I get it to check the validity of the file when uploaded to see if it needs to be converted without causing the system to hang?
Here is a link to a file that is causing problems: http://www.cssc-testing.org/accounting/school_9/20130604-a1atransportation-1.pdf
Thanks in advance for any guidance you can offer!
My Code (which I'm guessing is not very clean, as I'm new):
$pdftk = new pdftk();
if($create_cover) { $pdftk->setInputFile(array("filename" => $cover_page['server'])); }
// Load a list of attachments
$sql = "SELECT * FROM actg_attachments WHERE trans_id = {$trans_id}";
$attachments = Attachment::find_by_sql($sql);
foreach($attachments as $attachment) {
// Check if the file exists from the attachments
$attachment->set_variables();
$file = $attachment->abs_path . DS . $attachment->filename;
if(file_exists($file)){
// Use the pdftk tool to attach the documents to this PDF
$pdftk->setInputFile(array("filename" => $file));
}
}
$pdftk->setOutputFile($save_file);
$pdftk->_renderPdf();
the $pdftk class it is calling is from: https://github.com/bensquire/php-pdtfk-toolkit
You could possibly use Ghostscript using exec() to check the file.
The non-accepted answer here may help:
How can you find a problem with a programmatically generated PDF?
I wont say this is an appropriate/best fix, but it may resolve your problem,
In: pdf_parser.php, comment out the line:
$this->error("Unable to find object ({$obj_spec[1]}, {$obj_spec[2]}) at expected location");
It should be near line 544.
You'll also likely need to replace:
if (!is_array($kids))
$this->error('Cannot find /Kids in current /Page-Dictionary');
with:
if (!is_array($kids)){
// $this->error('Cannot find /Kids in current /Page-Dictionary');
return;
}
in the fpdi_pdf_parser.php file
Hope that helps. It worked for me.

Unsupported image type with fpdf

i have this message error:
Unsupported image type:imagePath was not found on this server.
I want to create pdf with jpg images taken from the iphone. Unfortunately this does not work. The image path is just seen that I can access it from my browser. When I change the way the path is erroneous.
A exemple of the code
$pdf->Image('../firstParthPath/'.$_SESSION['environment'].'/secondParthPath'.$ImageName,null,$heigth,40,50);
$ImageName are /image404-120404.jpg for exemple. I have tested variable name. It has /image404-120404.jpg
$heigth is a variable that is also handled automatically. It always has the desired content.
I tried to replace null with 0
I tried to add the option 'JPG'. Without success.
a exemple of image
http://imageshack.us/photo/my-images/72/image398120404.jpg/
It could be that PHP is not understanding the location via the relative path starting with '../'. You could try explicitly prepending the script path before it to make sure PHP is looking in the correct place:
(dirname(__FILE__) . '/../firstParthPath/'.$_SESSION['environment'].'/secondParthPath'.$ImageName,null,$heigth,40,50);
It's resolved. It was a verification error in the next picture. Now the problem's are resolved

FPDF Image from Dynamic Source. php/png/pdf. Can't get image to work

http://babymoments.co/preview/highres%20preview/5_357/
According to the FPDF documentation here: http://www.fpdf.org/en/doc/image.htm
You are supposed to be able to use an Image from a dynamic source.. however, as per the first link, i'm getting an fopen error.
Any suggestions?
Code Snippet:
// Overlay Text & Images
$pdf->Image($conf['rbase'].'page_maker/image_hr.php?id=5&side=1&bg=cover_pink&lo=0_1&imgtxt=0|0|u5_1310329746.jpg##1|1|Elina\'s Puppies 9/2/2010|15|arial_bi.ttf|db0ddb|fedfe4&applet_type=cover',$sx,$sh,(0-$dpi), 0, 'png');
You are trying to open a local php file with get parameters - try instead to open the image file as a url. For example :
http://domain.com/image.php?id=5
Or in your case...
http://babymoments.co/preview/page_maker/image_hr.php?id=5&side=1&bg=cover_pink&lo=0_1&imgtxt=0|0|u5_1310329746.jpg##1|1|Elina's%20Puppies%209/2/2010|15|arial_bi.ttf|db0ddb|fedfe4&applet_type=cover
I wouldn't fetch the preview over HTTP but would include the required code and generate the image in the same script that generates the PDF. That way there is no problems with setups that have disabled fopen()-usage with URLs.
I was implementing a barcode for a label printer. So I was using the 'barcodegen' library and 'fpdf' library for this project, but I was having problems including dynamically the image generated with barcodegen, giving me the following error:
FPDF error: Not a PNG file: ./misc/barcodegen/mostrar-codigo-bcgcode39.php
Afeter that, I use one of the answers described here, and I solved the problem using the full URL of the image, like this:
$pdf->Image("http://localhost/caaf/misc/barcodegen/mostrar-codigo-bcgcode39.php",0,0,20,0,'PNG');
And it worked for me.

Categories