Solved

html2fpdf tables and formatting not printing

Posted on 2013-05-22
15
1,346 Views
Last Modified: 2013-06-24
I have a PHP site which prints a permit out to PDF.

2 issues:

1. How can I get it to (also) open up the PDF upon generation? I have it e-mail if required (got that side sorted) but it needs printed as well upon time of generation.


2. The formatting for tables doesn't carry through.
i.e. Coloured table borders don't show up in the PDF, nor do coloured backgrounds.

PDF Generation Code:
include "PermitPrint.php";
$permitView = ob_get_clean();
echo $permitView;

require('/scripts/html2pdf/html2fpdf.php');
$pdf=new HTML2FPDF('P','mm','A4');
$pdf->AddPage();

$pdf->WriteHTML($permitView);
// $pdf->Output('Permit.pdf', 'S');
$pdfcontent = $pdf->Output('Permit.pdf', 'S');

Open in new window


$permitView is printed into the e-mail html body and $pdfcontent is the attachment. This works fine - except for the formatting issue in the PDF file..

Any assistance would be greatly appreciated.
0
Comment
Question by:kiwistag
15 Comments
 
LVL 15

Expert Comment

by:Jagadishwor Dulal
ID: 39187458
I think browser itself will not open pdf file, you can add force download
like:

header('Content-disposition: attachment; filename='.$pdfcontent);
header('Content-type: application/pdf');
readfile($pdfcontent);

Open in new window

0
 
LVL 12

Assisted Solution

by:sivagnanam chandrakanth
sivagnanam chandrakanth earned 250 total points
ID: 39189836
All the CSS styles should be written inline to get it reflected in PDF..have you did that??
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39190104
Thanks jagadishdulal. I tried putting that in at the end of the script but it wouldn't generate a HTML file.

I've tried the following but it generates a corrupted PDF file.

header('Content-disposition: attachment; filename='.$pdfcontent);
header('Content-type: application/pdf');


include "PermitPrint.php";
$permitView = ob_get_clean();
echo $permitView;
$htmlFile = $permitView;

require('/scripts/html2pdf/html2fpdf.php');

$pdf=new HTML2FPDF('P','mm','A4');
$pdf->AddPage();

$pdf->WriteHTML($permitView);
$pdfcontent = $pdf->Output('Permit.pdf', 'S');

//this is probably wrong.
readfile($pdfcontent);

Open in new window

0
 
LVL 2

Expert Comment

by:vid_yag
ID: 39201302
Hello kiwistag,

try to add all css into head tag of your html output from php file and check that your HTML structure is proper or not.
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39212527
Thanks - I'll give it a shot.
At present I have to echo out the input in the PHP to generate into PDF output.
To clarify it means that for output.php I have to go:
<?php echo "<html>
<head>
<title>Permit $year</title>
.....etc

Open in new window


otherwise some of the output doesn't seem to generate properly.
This makes it a headache when you want to visually want to edit the layout with all the escaped quotations in html.
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39212818
I'm trying TCPDF also and it isn't working when I do this (sorry if it's off the original question but trying alternative means to the same end).

require_once('/wwwroot/scripts/tcpdf/tcpdf.php');
ob_start();
// 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->SetAuthor('AuthorHere');
$pdf->SetTitle($permittypeName);
$pdf->SetSubject('Permit');
$pdf->SetKeywords($permittypeName . ', Permit');

// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (file_exists(dirname(__FILE__).'/lang/eng.php')) {
	require_once(dirname(__FILE__).'/lang/eng.php');
	$pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('times', 'BI', 20);

// add a page
$pdf->AddPage();

// set some text to print
$htmlSite = include('PermitPrint.php');

// print a block of text using Write()
$pdf->writeHTML($htmlSite, true, false, true, false, '');
// $pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);

// ---------------------------------------------------------
ob_flush();
//Close and output PDF document
$pdf->Output('Permit.pdf', 'I');

Open in new window


I'm assuming that:
$htmlSite = include('PermitPrint.php');
isn't set correctly as I get the html output followed by:
TCPDF ERROR: Some data has already been output to browser, can't send PDF file
at the end of the page.

By having ob_flush(); in it also shows the following error:
PHP Warning:  Cannot modify header information - headers already sent by (output started at XXXXXXXXXXXX in /sfgweb/scripts/tcpdf/tcpdf.php on line 7588
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39212971
You are getting the errors because to send the pdf, headers must be added by the server to identify what it is sending.  You cannot output anything prior to sending the pdf, even a blank line before the <?php tag will cause headers to be sent, any later attempt to send headers (as is required for the pdf) will result in an error.

Cd&
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Author Comment

by:kiwistag
ID: 39213597
I had a look at that too, the first lines are:

<?php
ob_start();
require_once('/wwwroot/scripts/tcpdf/tcpdf.php');

Open in new window


I ensured it's encoded as UTF-8 and had a good search, even deleting & recreating the first few rows but to no avial :(
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39213663
$pdfcontent = $pdf->Output('Permit.pdf', 'S');
The output type seems to bring up the error - even with html2fpdf if it's set to D or I.
ERROR: Some data has already been output to browser, can't send PDF file

This of course doesn't help since the issue is that there is output before the generation (which there isn't)...
0
 
LVL 53

Accepted Solution

by:
COBOLdinosaur earned 250 total points
ID: 39214339
ob_start does not stop all output, it allows headers to be sent.  Therefore, I speculate that the creation of the pdf is generating a pdf header.

So you should be able to copy the buffer to a string using:

$yourpdfstring=ob_get_contents();

Then instead of ob_flush() you can try  ob_end_clean() which should silently discard the buffer contents.

Cd&
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39215061
COBOLdinosaur: Thank you! This instantly fixed the issue for the PDF generation for saving.
I'm still working on the coding for the html formatting coming through. So far all the CSS data is in the head then called in the tables/cells/rows as required but isn't being passed on.

At least I can give tcpdf a try with your fix and see if that version processes it properly.
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39215694
I'm going back through the examples at http://html2fpdf.sourceforge.net/examples.php and it looks like it needs 2 sets of tags in some cases.
i.e. css doesn't seem to pass across to PDF in most cases so a cell border is <td border=1>
So to show in both html & PDF it may have to be set up as:

<td class=\"auto-style2\" style=\"width: 300px\" rowspan=\"2\" border=1 >

Open in new window


A cell with both a colored background and border needs to have the border set in the table (if whole table border) and then:
<td bgcolor=#CCCCCC style=\"width: 180px; border: 1px solid #000000; background-color: #CCCCCC;\">

Open in new window


This seems to be working...

I have also from COBOLdinosaur's suggestion to show the HTML on the page add in
ob_start();
include "Permitprint.php";
ob_end_clean();

Open in new window

at the end however of course it will state
ERROR: Some data has already been output to browser, can't send PDF file
Which would be nice to hide the error as I know it will fail as I've already generated the PDF anyway.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 39217102
You should be able to suppress the display of the error with:

ini_set('display_errors', 0);

Cd&
0
 
LVL 6

Author Comment

by:kiwistag
ID: 39234296
Thanks.
I might have to revisit this as from reading up on some formatting, there are other options for html to PDF outputs.

Does anyone have experience with different options?
html2fpdf
xhtml2pdf
tcpdf
0
 
LVL 6

Author Closing Comment

by:kiwistag
ID: 39270495
There was quite a lot of variables to the issue.
A lot of the coding was specific to html2fpds - especially some of the formatting commands that varied outside of standard css.
Buffer commands for output of strings were a large help to resolve some of the headaches that were happening.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Read about why website design really matters in today's demanding market.
These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now