I am using HTML2PDF converter in PHP to generate PDF.
My html seems to be big so that it could not fit in single page.
I am getting following error when I tried to generate pdf..
'the contents of a TD tag does not fit on a single page'
could not find proper solution yet. Any idea please.
This is my code at end to generate pdf.
require_once("../html2pdf_new/html2pdf.class.php");
try
{
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->WriteHTML($content);
$html2pdf->Output('exemple.pdf');
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
$content contains all html.
You can set setTestTdInOnePage as false to overcome this error.
$html2pdf = new HTML2PDF('P', 'A4', 'en');
$html2pdf->setTestTdInOnePage(false);
If this problem still exists: I fixed it with the method
HTML2PDF::setTestTdInOnePage(false)
This setting of value works. In the html2pdf main class file we have a method setTestTdInOnePage which does the rendering of td within a single page if set to true. As it is always true we have this exception thrown as our td data exceeds more than one page. So to avoid it we can set it to false to pass the check.
if we have nested tables, it breaks for multiple pages.
Try to avoid nested tables.
It works.
found at Forum
Find this 'throw new HTML2PDF_exception(7);' in html2pdf.class.php and disable it.
Like this:
if ($this->_testTdInOnepage && $this->_subHtml->pdf->getPage()>1) {
//throw new HTML2PDF_exception(7);
}
Related
I tried to use MPDF library for generating PDF.
try {
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
// Other code
$mpdf->Output("1.pdf", 'D');
} catch (\Mpdf\MpdfException $e) { // Note: safer fully qualified exception name used for catch
// Process the exception, log, print etc.
echo $e->getMessage();
}
And I get this error message.
Data has already been sent to output, unable to output PDF file
I used ob_end_clean() but not working.
I used all answers in this question but nothing works for me.
TCPDF & mPDF error: Some data has already been output to browser, can't send PDF file
I have got the same error.
Data has already been sent to output, unable to output PDF file
This means before creating a PDF with mPDF some data is stored in the buffer which is sent to the browser. Therefore it is unable to create PDF.
To rectify this, add this below php built-in function at the first line of your page were you are preparing data for pdf.
ob_start();
And add this below php built-in function before mPDF code (before where you are calling mpdf)
ob_end_flush();
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML($html);
$mpdf->Output();
This will clear the buffer output before processing mPDF.
Make sure if you use any functions to keep them on the same page, don't include a functions page where you have kept your all functions.
Read more about PHP Buffering
I just had this error and the correct solution for it is the following.
if when you type <?php in your script as the very first word, make sure there are no space characters or any characters before <?php whatsoever. This also happens with anything else giving the error Data has already been sent to output. Space characters like space and TAB don't appear to you and it will definitely deceive you.
This won't appear easily to anyone.
For me, it helped to insert <?php instead of <?.
i'm trying to use fdpf with php to generate a pdf file with patient automatic reports system
so after getting all patient info from DB .. i'm trying to print report image from directory
$id=$user_id;
$dir="./user_reports/$id";
opendir("$dir");
$fi = new FilesystemIterator($dir, FilesystemIterator::SKIP_DOTS);
$report_img=array();
for($k=1;$k<=iterator_count($fi);$k++) // iterator_count to count files in folder
{
$report_img[$k]="$dir/$id.jpg";
$id++;
}
and this is to print info.
for($x=1;$x<=$i;$x++)
{
$image=$report_img[$x];
$pdf->Cell( 40, 40, $pdf->Image($image,70,190,-300), 0, 0, 'L', false );
}
after all .. it doesn't work :(
any ideas?
You are assuming $pdf->Image($image,70,190,-300) returns a string which you can put in a cell. This is not so. Try this:
for($x=1;$x<=$i;$x++)
{
$image=$report_img[$x];
$pdf->Image($image,70,190,300);
}
I left out the cell, because it doesn't do anything. I used a positive width for the image.
I'm sure this doesn't do what you want. That's unclear at this point, but it shouldn't generate an error 500 anymore, as long as the images exist.
Generating a PDF file like this is nothing like working with HTML. You're probably thinking of HTML table cells.
In the case you want to print something, you can work with just HTML. It is now possible to make pages in HTML and set the true dimensions when printing with styles. See: https://www.w3.org/TR/css3-page/#page-size
That could be a much better solution for you, since you seem to know HTML. It wouldn't work if it is actually the PDF file you want.
I have downloaded PHP HTMLtoPDF converter from here. But when tried to just print single line output, it does not print in the output pdf. Its empty.
Below is the code which i tried.
$content1 = "<page><a>Sample PDF file</a></page>";
// convert in PDF
require_once('/home/www/APIs/html2pdf_v4.03/html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P', 'A4', 'fr');
$html2pdf->setModeDebug();
$html2pdf->setDefaultFont('Arial');
$html2pdf->writeHTML($content1);
$html2pdf->Output('exemple00.pdf');
}
catch(HTML2PDF_exception $e) {
echo $e;
exit;
}
Whenever I get into trouble with something I simplify. Exterminate all the possibilities that could hinder the workings of a program.
With this in mind I have looked at the website and, probably just like you, found a sample piece.
$html2pdf = new HTML2PDF('P','A4','fr');
$html2pdf->WriteHTML("text");
$html2pdf->Output('exemple.pdf');
Try this and see if it works. Why? This is the simplest of setups. If this does not work you know it's not you (or your refrence to the class is wrong).
What I know from the pdf to html creators is that they all have a problem converting incorrect html to a pdf. In this case your "a" could be interpreted as wrong seeing it has no href (which could be required).
So to make this simple use a single line of string text without html like "test" and check back with us. But if that doesn't work I's probably suggest using another of the converters (which I would recommend anyway).
BTW, I use https://github.com/dompdf/dompdf which is very advanced in html error detection and common options.
I have a HTML document which I'm converting to PDF using TCPDF. The HTML document contains several html tables. This works fine. But, I also need to add the number of pages in the PDF doc to the first page. Is there any solution to build the PDF file and then add a page before current first page ?
I don't know if it works with HTML, but I found a solution that worked for me with movePage.
You create the page, get its position and move it to the first position :
$pdf->AddPage();
$pdf->Cell(0, 10, 'My content');
// Get the current page number
$pageNo = $pdf->PageNo();
// First page of the document
$toPage = 1;
$pdf->movePage($pageNo, $toPage);
Official example here : https://tcpdf.org/examples/example_044/
You should be able to calculate how many pages there will be based on the size of your tables. I would read in all the html data, measure it, and then start to make the PDF.
The idea I found is to clone the TCPDF instance and get the returned number of pages:
$tmp_pdf = clone $pdf;
$tmp_pdf->AddPage();
$tmp_pdf->writeHTML($report, true, false, true, false, '');
$num_pages = $tmp_pdf->getNumPages();
unset($tmp_pdf);
$report = preg_replace('#\{num_pages\}#is', $num_pages, $report);
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