Dompdf is not rendering image - php

I am using DOMPDF library to convert HTML to PDF at runtime. However, I am facing an issue related to images. System displays images when I render HTML code but when I convert it into PDF, it replace all images by "broken_image.png". I have placed complete url (given below) of images, its still not working for me :/ . Below is the example image url:
<img src='http://img.uksoccershop.com/images/man-utd-gk-away-shirt-2012-13.jpg'/>
Can anyone please tell me how can I resolve the issue. Thanks in advance.

To enable remote images for DOMPDF u have to alter the config dompdf_config.inc.php
Look near line 284 :
def("DOMPDF_ENABLE_REMOTE", false);
This should be set to true. Also be sure that the temp directory defined near line 103 is writeable :
def("DOMPDF_TEMP_DIR", sys_get_temp_dir());

DOMPDF is not a browser, it won't download images or any other assets, it simply converts HTML/CSS into a PDF representation.
You'll need something like wkhtmltopdf to achieve this.

Related

mPDF not showing image in generated PDF instead showing red x symbol and full url for background image

Recently I have moved from my staging server to production server and I don't have access to any of these servers.
Both of these servers are linux.
On the new server while generating the pdf using mPDF with symfony 1.4 framework, images like rupee symbol are not getting displayed instead a small red "x" symbol is getting displayed in pdf.
Also, when I try to give background image to pdf, full image path like "http://example.com/image/rupee-image.jpg" is getting displayed instead of image.
Rupee symbol, other images and background images are working fine on my staging server.
When, I did $mpdf->showImageError(), It's saying "mPDF Image Error: Could not find image file" and pasting the url in browser displays the image perfectly.
Any help will be greatly appreciated.
mPDF is telling you that it's not finding the image.
Without seeing your code it's difficult to hint you on what's wrong.
Still you probably what to try referring to your images using a full path locally.
So instead of reference like http://example.com/image/rupee-image.jpg
use something like /var/www/mysite/image/rupee-image.jpg , same as if checking the image is there using the command line.
HTH
I had similar problem, i fixed it with replacing http:// to https://. Watch out for it.
I fixed it myself... I have created a variable in my template and then replaced that variable with image location from my action class, using $mpdf->WriteHTML(str_replace('rupee_symbol','₹',$html1));
My problem was PHP 7 in this case! I check condition
if (!empty('/images/someimg.png')) {
echo '<img ...';
} else {
echo 'error in PHP 7';
}
Be carefull using it. Version PHP 7.0.19

How to convert a PDF into image exactly similar to the PDF with PHP/Imagemagik/Ghostscript

Im generating PDF documents with PHP(TCPDF is the library behind) and for displaying them Im converting them as images using ghostscript, and displaying the previews, but the preview doesnt actually similar to the PDF document.
The code Im using to convert is here
$pdf = 'my_report.pdf';
$output = 'my_preview.jpg';
$quality=90;
$res='300x300';
$exportPath=$output;
set_time_limit(900);
exec("'gs' '-dNOPAUSE' '-sDEVICE=jpeg' '-dUseCIEColor' '-dTextAlphaBits=4' '-dGraphicsAlphaBits=4' '-o$exportPath' '-r$res' '-dJPEGQ=$quality' '$pdf'",$output);
and the preview generated with the code for this document is right below
where as my actual PDF file looks like below
You can see a lot of inequalities between, I need a way to convert like just a copy of it.
and im sure there is nothing wrong in the PDf report, I tried it uploading it into Google mail, that gave a perfect image, and I did convert the PDf into jpeg here
http://pdf2jpg.net/
That to gave a perfect copy of the document, only the Imagemagick/Gjostscript is unable to generate an exact one.
Any help would be helpful.
What are you using to view the 'correct' display of the PDF ? Does Ghostscript issue you any warnings when rendering ?
It looks to me like there 'may' be fonts missing in your original PDF file, which will lead to font substitution.
Why are you using -dUseCIEColor ? This will almost certainly lead to colour shifts, which I also see in your images. If you have a good reason for using this, what is it ? If you don't have a good reason, don't do that.
Is the second image a JPEG ? The first clearly is, and jpeg is a lossy compression, have you tried using TIFF instead ?
It is always useful with these sorts of questions to post a link to the original PDF file, so that some investigation can be done, without that, this is all guesswork I'm afraid.

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.

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.

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