I've been trying to set up this php pdf script. I downloaded the code from the website and uploaded it as is from this page [https://sourceforge.net/projects/tcpdf/files/][1]
It works until the line that starts $pdf - I added echo $filename line to see if I could see errors (an idea found on this website - the page displays 1hello - I gather this probably means the require_once works OK.
<?
// Include the main TCPDF library (search for installation path).
$filename = require_once('../tcpdf/examples/tcpdf_include.php');
echo $filename.'hello';
exit;
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false, true);
etc. etc.
If I remove $filename = and the echo / exit line (below) I get a generic server 500 error - any ideas how to get a detailed error I can actually use? I can see detailed errors on other pages.
<?
// Include the main TCPDF library (search for installation path).
require_once('../tcpdf/examples/tcpdf_include.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false, true);
etc. etc.
EDIT: Found the error by adding ini_set('display_errors', 1); to the page - the error is Fatal error: Class 'TCPDF' not found...on line 31
Line 31 refers to:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
If I look in the tcpdf_include.php include file it says some code that refers to some files I don't think my server has:
$tcpdf_include_dirs = array(
realpath('../tcpdf.php'),
'/usr/share/php/tcpdf/tcpdf.php',
'/usr/share/tcpdf/tcpdf.php',
'/usr/share/php-tcpdf/tcpdf.php',
'/var/www/tcpdf/tcpdf.php',
'/var/www/html/tcpdf/tcpdf.php',
'/usr/local/apache2/htdocs/tcpdf/tcpdf.php' );
/usr/share/php/ is empty. I'm guessing I should put the tcpdf.php file in all of those locations or does it not matter & I'm missing the point?
You can find detailed 500 server error in your web server (Apache?) logs. The location of logs depends of your OS/installation.
You gather fine: 1hello means that the required file is loaded correctly. You don't need to check it, because if require/require_once fails, you get a Fatal Error, and the script dies.
If you can track down server error logs, probably you will see something like:
Fatal error: Class 'TCPDF' not found
because tcpdf/examples/tcpdf_include.php is intended to be used with provided examples, and it works correctly only if the main URL (or the current directory, if you execute the script via commandline) is in the same directory.
To load TCPDF class, you have to
require_once( '../tcpdf/tcpdf.php' );
but I suggest you to indicate absolute path.
These TCPDF constructor does not exist. You have added a boolean variable at the end, which is not allowed lol.
The following is better :
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
Related
I am trying to create hundreds of PDF files with TCPDF using a while loop. The problem is that it seems to create only the first one and then stops. I turned on error reporting and there is no error.
I basically get the data with:
ob_start();
while($row = mysqli_fetch_assoc($data)) {
$invoice_id = $row['invoice_id'];
require_once('tcpdf_include.php');
// create new PDF document
$pdf = new MYCUSTOMPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// All $pdf-> settings are here
// add a page
$pdf->AddPage();
$pdf->lastPage();
$pdf->Output('/path/to/'.$invoice_id.'.pdf', 'F');
echo "Generated ". $invoice_id."\n";
}
The .PDF gets generated fine, but it stops there. It doesn't continue with the next in the loop.
Anybody with TCPDF experience knows what is wrong? I can't figure it out and there is no error even with error_reporting(E_ALL);. Any help is greatly appreciated.
I am using tcpdf library to create pdf report from php and i have many tables to write on pdf report.
I want to use a css style upon some of table rows but internal css not working on it, that's why I am force to use inline style but it is so much time taking.
So anyone please give some trick if I can use internal css style script to use for all required table rows once.
Use following code for library:
// Include the main TCPDF library (search for installation path).
require_once('tcpdf_include.php');
and set the following parameters for object definition
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
It will work.
For detail description for tcpdf library you can refer URL:
http://www.tcpdf.org/examples.php
All the Best
I'm able to generate a PDF file using TcPDF ver 6.0.44. The file opens fine in the browser, and 3rd party PDF readers but will not open in Adobe Reader XI. I keep getting a the error message 'file type is not supported or the file is damaged'.
What's going on here? Here's the code I wrote:
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->AddPage();
$html = '<html>
<head></head>
<body>
<h1>Hello World!</h1>
</body>
</html>';
$pdf->lastPage();
$pdf->writeHTML($html, true, 0, true, 0);
$pdf->Output('htmlout.pdf', 'I');
In your test file if you are using images , and some of your images are missing icc profile, adobe acrobat doesnt recognize this and cause the problem you are facing.
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.
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.