html2fpdf tables and formatting not printing

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.
LVL 6
kiwistagAsked:
Who is Participating?
 
COBOLdinosaurConnect With a Mentor Commented:
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
 
Jagadishwor DulalBraces MediaCommented:
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
 
sivagnanam chandrakanthConnect With a Mentor Technical LeadCommented:
All the CSS styles should be written inline to get it reflected in PDF..have you did that??
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
kiwistagAuthor Commented:
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
 
vid_yagCommented:
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
 
kiwistagAuthor Commented:
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
 
kiwistagAuthor Commented:
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
 
COBOLdinosaurCommented:
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
 
kiwistagAuthor Commented:
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
 
kiwistagAuthor Commented:
$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
 
kiwistagAuthor Commented:
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
 
kiwistagAuthor Commented:
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
 
COBOLdinosaurCommented:
You should be able to suppress the display of the error with:

ini_set('display_errors', 0);

Cd&
0
 
kiwistagAuthor Commented:
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
 
kiwistagAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.