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.
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 have issue with PDF generated from CKEditor.
I have some text typed in CKEditor:
HTML source:
Next, this text is saved in DB.
My CKEditor config looks like:
Further, above text is fetched from DB in controller, and PDF file is generated using TCPDF:
public function createRegulationPDF(){
require_once('TCPDF/tcpdf.php');
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// set font
$pdf->SetFont('freeserif', '', 10);
// add a page
$pdf->AddPage();
// GET CONTENT FROM DB
$html = $this->view->settings['reg_statement'];
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
....
And the final point, generated PDF:
The question is: what is this weird (the box) selected char?
And how to avoid it?
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 am trying to convert a SVG file to PDF file using TCPDF library in PHP. I have created a SVG file and use PHP to replace text and plan to render the resultant SVG file to PDF file.
Any idea, if TCPDF library supports SVG to PDF conversion. Any pointers in this direction would really help me.
You don't really need to replace text or render, Once you have created your svg file. All you just need to include tcpdf in your script and create its object like e.g.
require_once(DOCUMENT_ROOT . '/library/Lib/tcpdf/mypdf.php');
$this->pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$this->pdf->AddPage(); // Add page to pdf before addding content
//There are several other property need to be set on basis of your need
$this->pdf->ImageSVG('file/mySVGFile.svg', 15, 20, '', '', '',
'', '', 1, false); // 15,20 are co-ordinate to position graph in pdf
Once you added your content to your pdf, Last step comes is to download the pdf using .
$this->pdf->Output('my.pdf', 'FD');
Feel free to ask for anything you have any query in this code.
Try
$pdf->ImageSVG(VIEW_JS."graph_as_svg/".php", '', '', '210', '100', '', '', '', 0, false);
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.