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.
Related
I have a requirement where two pdfs need to be created at once. A for loop will not work as one file is going to be a certificate pdf. The next file is going to be an Invoice pdf. I know I am missing something simple or else its not possible to do this. I have attached a simple code to demonstrate what I mean. The files are executed on a form submission.I have made two files and then a handler file to include the two files but still only outputs on pdf.
<?php
require('fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Im a certificate');
$pdf->Output('Certificate.pdf', 'D'); //Output of the first invoice
$pdf = new FPDF(); // This is going to be the second pdf the Invoice
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Im an invoice');
$pdf->Output('This is a invoice.pdf', 'D');
//The problem is it only outputs the first one not this one
?>
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);
I wanted to add TCPDF to codeigniter so i downloaded TCPDF from TCPDF Download and created a file in /libraries/Pdf.php as such
require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
class Pdf extends TCPDF
{
function __construct()
{
parent::__construct();
}
}
and used it as such in controller
function pdfTest(){
$this->load->library("Pdf");
$pdf = new Pdf('P', 'mm', 'A4', true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
// Add a page
$pdf->AddPage();
$html = "<h1>Test Page</h1>";
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output();
}
But it gives me below error
TCPDF ERROR: Some data has already been output, can't send PDF file
What could be the reason of the error and how can I solve it? I have searched allot but couldn't find a solution.
Thank you guys in advance.
Finally found the answer I placed ob_clean(); before $pdf->Output('test.pdf','I'); That solved the issue. Thank you all for the help.
make sure not to have whitespace before <?php and after ?>
or
Just use ob_start(); at the top of the page.
This error indicates that already some error or output is displayed or echoed before creating pdf. Please check if you have printed anything.
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 starting a new thread for this one, will try to be as specific as possible.
This is a portion of an app for our sales people that allows them to customize a page with their contact information, after they hit submit, a PDF page is created which is then attached to the end of a larger document.
The first page of the document is also dynamically created from the same form that they fill out. After they press submit, 2 PDF files are created, a front page and a back page, then both pages are attached to a larger document creating a single report.
Everything works great unless they enter Chinese characters then all I get is garbage characters like this - (质釕俕试æμ•ç¨‹). The document still looks like this even before it is merged with the larger document.
I have tried every possible scenario (it seems) from using a Unicode font, to using my own created font and one by one trying each of the fonts in the TCPDF folder. I am at a total loss, and have been for some time now. I feel like I have been on just about every website that mentions TCPDF.
Here is the code that creates the back page, back to the basic commands I started with. I am hoping it is a simple command down here that I am missing:
require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB));
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($html, isset($_GET['vuehtml']));
//Line below saves PDF to file
$html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
echo $e;
}
And for what it is worth, running a Centos 5.5 server to create the documents. Thank you in advance.
After doing some investigation on this issue with HTML2PDF, I was told that the font arialuni.ttf should support all of the characters I need. With that being said, HTML2PDF will not load this font when I use it in my script seen below.
require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L','LETTER','en', false, '', array(mL, mT, mR, mB));
$html2pdf->pdf->AddTTFFont('ARIALUNI.TTF');
$html2pdf->pdf->setFont('arialuni');
$html2pdf->writeHTML($html, isset($_GET['vuehtml']));
//Line below sends output to the screen only
//$html2pdf->Output('exemple00.pdf');
//Line below saves PDF to file
$html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
echo $e;
}
*All I can think is that the syntax is not 100% correct. I have ran the tt2fm and makefont to get the files but do not feel that those routines were a 100% successful.
Please help - I have been working on this thing off and on for months. I am almost ready to just scrap it and hit the door.
Thank you.
According to your codes:
require_once(dirname(__FILE__).'/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('L','LETTER','en', false, 'ISO-8859-15', array(mL, mT, mR, mB));
//$html2pdf->setDefaultFont('Arial'); //Please remove this
$html2pdf->setDefaultFont('stsongstdlight'); //And change with this, it works 101% for me
$html2pdf->writeHTML($html, isset($_GET['vuehtml']));
//Line below saves PDF to file
$html2pdf->Output('created/'.$lastname[1].'_html2pdf.pdf', 'F');
}
catch(HTML2PDF_exception $e) {
echo $e;
}
...And here is my 101% working codes to have the CHINESE characters in pdf output:
<?php
require_once ROOT_DIRECTORY.'/{it's your own location where you put the class at}/html2pdf-4.4.0/html2pdf.class.php'; //ROOT DIRECTORY is my own "defined" so do not use this line.
/* or use this:
* require_once(dirname(__FILE__).'/html2pdf.class.php');
* it depends on what you need
*/
try
{
$html2pdf = new HTML2PDF('L', 'A6', 'en', true, 'UTF-8', 0); //I'm using UTF-8 //Also I'm working on A6 page in my project here, so if you need to have A4 size change A6 back to A4 or change to the size you need
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->setDefaultFont('stsongstdlight'); //this is all you need to have the CHINESE character.
$content = ob_get_clean();
$html2pdf->writeHTML($content);
$html2pdf->Output('Sticker-'.$this->invNumber.'-'.$this->custCode.'.pdf');
exit;
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
Hopefully it works TOO in your codes there!
Happy coding :)
You can render Chinese character in your PDF document by using
cid0jp font in TCPDF.
For your reference :-
PHP Code :-
http://www.tcpdf.org/examples/example_038.phps
Generated PDF :-
http://www.tcpdf.org/examples/example_038.pdf
Please ensure file cid0jp.php should be in font directory.
tcpdf/fonts/cid0jp.php
$html2pdf = new HTML2PDF('L','LETTER','en', true, 'UTF-8', array(mL, mT, mR, mB));
So html2pdf is based on tcpdf, but they are a little bit different:
html2pdf does not support Chinese language.
However, you can get the javiergb.php from the include directory /tcpdf/fonts/. Then you copy this file to the html2pdf/_tcpdf5.xxxx/fonts/ directory.
Afterwards, you just have to add the line $html2pdf->setDefaultFont('javiergb'); to your html2pdf set up as following:
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->setDefaultFont('javiergb');
$html2pdf->writeHTML($html);
$html2pdf->Output('example_zh_cn.pdf');
You can look my fork: https://github.com/cychai/html2pdf