Related
In PHP I'm trying to set up a 'table' but can't seem to set my cell height uniformly along a row:
And I've added a line break so you can see the issue more clearly:
I would like to get a clean table format where the row where "This is my long..." is all the same cell height. But the only function on the documents was GetStringWidth() so I don't see how I can apply that to the case here.
This is the desired output:
This is my code:
//Custom hard coded widths for the template
$width = array(12, 20, 30, 20, 15, 12, 20, 18, 25, 15, 10);
foreach ($row as $col) {
$y = $this->GetY();
$x = $this->GetX();
$this->MultiCell($width[$i], 3, $col, 1, 'C', FALSE);
$this->SetXY($x + $width[$i], $y);
$i++;
}
EDIT:
After doing some more research I found that this library won't give me what I need, it actually lacks majority of the things a basic library has so I've decided to use tcpdf as my library.
For such result, use this script: http://www.fpdf.org/en/script/script3.php
I am trying to display data using multiCell. And when the data in the page arrives to y=228 I want it to go to the next Page And be displayed in the position y=112.
As first Step I tried to only add 2 simple conditions :
when the data arrives to position y=228 create a new page
when the data goes to the next Page display the result at position =112
It worked. But if the current multiCell content is large it dosn't go to the next page untill it finishs writting all the multicell content , so I added the function SetAutoPageBreak So it inserts a page Break when y=228 .
Here where the problems start The code doesnt insert my data in the new page in the position I defined (y=112) it insert it in the start .I don't know how to fix this problem I hope I could find some help I will appreciate it.
Here is My code :
<?php
require ('../fpdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 12);
$y = $pdf->GetY();
$x = $pdf->GetX();
$width_cell = array(
10,
30,
50
);
$pdf->SetFillColor(193, 229, 252);
$pdf->SetY(112);
$pdf->Cell($width_cell[0], 10, 'ID', 1, 0, C, true);
$pdf->Cell($width_cell[1], 10, 'NAME', 1, 0, C, true);
$pdf->Cell($width_cell[2], 10, 'CLASS', 1, 1, C, true);
$pdf->SetFont('Arial', '', 10);
for ($i = 0;$i < 30; $i++) {
$pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
$pdf->Cell($width_cell[1], 10, 'John Deo', 1, 0, C, false);
$pdf->Cell($width_cell[2], 10, 'Four', 1, 1, C, false);
$y = $pdf->GetY();
$pdf->Cell($width_cell[0], 10, $i, 1, 0, C, false);
$pdf->Cell($width_cell[1], 10, 'Y:' . $y, 1, 0, C, false);
// $pdf->Cell($width_cell[2],10,'Four',1, 1, C, false);
$pdf->MultiCell($width_cell[2], 10, 'four four four four four four four four four four four four four four four four four four four four four four ', 1, C, false);
// Uncomment this line to see what Happends when the Page Break is inserted
// $pdf->SetAutoPageBreak(auto,69);
$y = $pdf->GetY();
if ($y > 228 && $i != 29) {
$pdf->AddPage();
$pdf->SetY(112);
}
/*
if ($pdf->PageNo() != 1 && $y < 20){
$pdf->SetY(112);
}
*/
}
$pdf->Output();
?>
Extend fPDF and add a header function which positions Y where you want it every time a new page is started.
require ('fpdf.php');
class PDF extends FPDF {
function Header() {
$this->SetY(112);
}
} // end of the PDF class
$pdf = new pdf();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$y=$pdf->GetY();
$x=$pdf->GetX();
$width_cell=array(10,30,50);
$pdf->SetFillColor(193,229,252);
$pdf->SetY(112);
The rest of your code goes here and you DO want to uncomment the AutoPageBreak line. You'll also want to change the AutoPageBreak line to read
$pdf->SetAutoPageBreak(1,69);
since the first argument is a Boolean to indicate whether or not it should be enabled.
I want to highlight specific cell based on results in FPDF. I am displaying cells using array.
$a = array(1, 2, 3, 4, 5);
foreach($a as $col)
$pdf->Cell(32,10,$col,1,0,'C');
Each of these array values are basically results. So for example
if result == 3
I want cell 3 that has 3 written in it can highlight in some manner(advice is welcomed) I thought about making a circle there or changing cell background/border colour or changing text colour.
I tried something like this but it does not work
if(in_array(3,$a ,TRUE)){
$pdf->SetFillColor(128, 0, 0);
$pdf->SetTextColor(255, 255, 255);
}
Can somebody give me hints only through FPDF and php please.
If you are saying that you only want to fill the array value that equals 3, this is how you could do it:
$pdf = new FPDF();
$a = array(1, 2, 3, 4, 5);
foreach($a as $col) {
if ($col == 3) {
$pdf->SetFillColor(128, 0, 0);
$pdf->SetTextColor(255, 255, 255);
} else {
$pdf->SetFillColor(255);
$pdf->SetTextColor(0);
}
$pdf->Cell(32,10,$col,1,0,'C');
}
The if (in_array(3, $a)) wouldn't work because by saying that you are setting the fill and text color for all if there is a 3.
Simplify the if guard, better yet remove it altogether and test the background color code
if(true){
$pdf->SetFillColor(128, 0, 0);
$pdf->SetTextColor(255, 255, 255);
}
How can I export a table created by PHP into an excel file? Is there any way?
I actually want to do it this way:
Say I am querying the database is a php file and displaying results in a html and PHP table...
This table will be dynamic depending on what the user is searching for. And the users should be able to save the table as a excel file by clicking a EXPORT button...
Is there any option in php to do that?
PHPExcel is probably the best library to go about doing this with. If you download it, there are a ton of examples that will almost certainly show what you want to do.
A quick example would be the following:
$objPHPExcel = new PHPExcel();
$objWorksheet = $objPHPExcel->getActiveSheet();
$objWorksheet->fromArray(
array(
array('', 'Rainfall (mm)', 'Temperature (°F)', 'Humidity (%)'),
array('Jan', 78, 52, 61),
array('Feb', 64, 54, 62),
array('Mar', 62, 57, 63),
array('Apr', 21, 62, 59),
array('May', 11, 75, 60),
array('Jun', 1, 75, 57),
array('Jul', 1, 79, 56),
array('Aug', 1, 79, 59),
array('Sep', 10, 75, 60),
array('Oct', 40, 68, 63),
array('Nov', 69, 62, 64),
array('Dec', 89, 57, 66),
)
);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('sheet.xlsx');
Alternatively, you could export pretty easily as a CSV, which the user could then open in excel without difficulty (not technically an excel document though - there are limitations).
store the table in a varaiable
and write into file
ex:
$content = "";//in content you must have table data
$file = fopen("file location", "w");
fwrite($file ,$content);
this should help easy
If there is an end-user that will view the file use the answer already given using PHPExcel
If your doing it for data-access then use CSV.
https://php.net/manual/en/function.fputcsv.php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
If you do not need formulas or fancy features, it would also be easy to output a CSV file (which Excel can open). If you had an array of arrays like the one enigma demonstrated, you could just use
$string = "";
foreach ($array as $row)
$string .= implode(',', $row) . "\n";
file_put_contents("filename.csv", $string);
Take a look a this class. It is able to create multi-sheet excel files from HTML tables.
https://github.com/GSTVAC/HtmlExcel
$xls = new HtmlExcel();
$xls->addSheet("Names", $names);
$xls->headers();
echo $xls->buildFile();
I am trying to use FPDF to generate at PDF file, this is my first time attempting it.
I have the latest FPDF files and have also set up the WriteHTML add-on. The below code is working up until the WriteHTML part at the very bottom. I am getting the error "Warning: Division by zero in /home4/fwall/public_html/fpdf/fpdf.php on line 796". When I look at line 796 of the FPDF.php, I find this:
// Output text in flowing mode
$cw = &$this->CurrentFont['cw'];
$w = $this->w-$this->rMargin-$this->x;
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; // <--LINE 796
$s = str_replace("\r",'',$txt);
$nb = strlen($s);
$sep = -1;
$i = 0;
$j = 0;
$l = 0;
$nl = 1;
If I add a conditional statement, along the lines of:
if ($this->FontSize != 0) {
$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize; // <--LINE 796
}
I can get the error to go away, but I know that can't be correct. Does anyone see an error in my code that would cause this?
require ('/home4/fwall/public_html/fpdf/fpdf.php');
//create a FPDF object
$pdf=new FPDF();
$pdfhtml=new PDF_HTML();
//set document properties
$pdf->SetAuthor('Author Name');
$pdf->SetTitle('PRESS RELEASE - NAME OF SHOW');
//set font for the entire document
$pdf->SetFont('Helvetica','B',10);
$pdf->SetTextColor(0,0,0);
//set up a page
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');
//display the top block
$contact = 'Contact Name';
$addline1 = '4002 2nd Ave NE, #2';
$addline2 = 'Address Line 2';
$cityzip = 'Seattle, WA 98105';
$pdf->Cell(0, 4, 'PRESS RELEASE', 0, 0, 'L');
$pdf->Cell(0, 4, 'FOR IMMEDIATE RELEASE', 0, 1, 'R');
$pdf->Cell(0, 4, 'CONTACT: '.$contact, 0, 0, 'L');
$pdf->Cell(0, 4, 'KILL DATE: August 1, 2014', 0, 1, 'R');
$pdf->Cell(0, 4, $addline1, 0, 1, 'L');
$pdf->Cell(0, 4, $addline2, 0, 1, 'L');
$pdf->Cell(0, 4, $cityzip, 0, 1, 'L');
//display the title
$pdf->SetFontSize(20);
$pdf->Cell(0,10,'PRESS RELEASE TITLE',0,1,'C',0);
//display the sub-title
$pdf->SetFontSize(16);
$pdf->Cell(0,10,'The Subtitle',0,1,'C',0);
//display the italic summary
$pdf->SetXY(10,55);
$pdf->SetFont('Helvetica','I',10);
$summary = 'SEATTLE - Theatre Off Jackson presents SPF 8, the annual exhibition of solo performance. Featuring four featured performers and one shorts night, the festival will occur between February 6th and March 1st, 2014. This year\'s festival is an exciting mix of experienced artists and new-comers with exciting stories to tell. From tales of a ten-day vows of silence to what it\'s like growing up with deaf parents and siblings, this year\'s festival is a potpourri of styles and stories.';
$pdf->MultiCell(0, 4, $summary , 0, 'J');
//display the main content
$pdf->SetFont('Helvetica','',10);
$maincontent = '
First line.
Second Line?
<ul>
<li>
Item 1
</li>
</ul>
';
$pdfhtml->WriteHTML($maincontent);
//Output the document
$pdf->Output('example1.pdf','I');
The WriteHTML add-on is a class called PDF_HTML which extends the original FPDF. To use the add-on functionality you have to instantiate the subclass and use it:
$pdfhtml=new PDF_HTML();
You don't need the additional instance ($pdf) of the parent class. Remove it and change all references of $pdf to $pdfhtml and you are good to go:
<?php
define('FPDF_FONTPATH', '../Classes/FPDF/font/');
require ('../Classes/FPDF/fpdf.php');
require ('writeHtml.php');
//create a PDF_HTML object
$pdfhtml=new PDF_HTML();
//set document properties
$pdfhtml->SetAuthor('Author Name');
$pdfhtml->SetTitle('PRESS RELEASE - NAME OF SHOW');
//set font for the entire document
$pdfhtml->SetFont('Helvetica','B',10);
$pdfhtml->SetTextColor(0,0,0);
//set up a page
$pdfhtml->AddPage('P');
// $pdfhtml->SetDisplayMode(real,'default'); //<-- commented this line, what is real?
//display the top block
$contact = 'Contact Name';
$addline1 = '4002 2nd Ave NE, #2';
$addline2 = 'Address Line 2';
$cityzip = 'Seattle, WA 98105';
$pdfhtml->Cell(0, 4, 'PRESS RELEASE', 0, 0, 'L');
$pdfhtml->Cell(0, 4, 'FOR IMMEDIATE RELEASE', 0, 1, 'R');
$pdfhtml->Cell(0, 4, 'CONTACT: '.$contact, 0, 0, 'L');
$pdfhtml->Cell(0, 4, 'KILL DATE: August 1, 2014', 0, 1, 'R');
$pdfhtml->Cell(0, 4, $addline1, 0, 1, 'L');
$pdfhtml->Cell(0, 4, $addline2, 0, 1, 'L');
$pdfhtml->Cell(0, 4, $cityzip, 0, 1, 'L');
//display the title
$pdfhtml->SetFontSize(20);
$pdfhtml->Cell(0,10,'PRESS RELEASE TITLE',0,1,'C',0);
//display the sub-title
$pdfhtml->SetFontSize(16);
$pdfhtml->Cell(0,10,'The Subtitle',0,1,'C',0);
//display the italic summary
$pdfhtml->SetXY(10,55);
$pdfhtml->SetFont('Helvetica','I',10);
$summary = 'SEATTLE - Theatre Off Jackson presents SPF 8, the annual exhibition of solo performance. Featuring four featured performers and one shorts night, the festival will occur between February 6th and March 1st, 2014. This year\'s festival is an exciting mix of experienced artists and new-comers with exciting stories to tell. From tales of a ten-day vows of silence to what it\'s like growing up with deaf parents and siblings, this year\'s festival is a potpourri of styles and stories.';
$pdfhtml->MultiCell(0, 4, $summary , 0, 'J');
//display the main content
$pdfhtml->SetFont('Helvetica','',10);
$maincontent = '
First line.
Second Line?
<ul>
<li>
Item 1
</li>
</ul>
';
$pdfhtml->WriteHTML($maincontent);
//Output the document
$pdfhtml->Output('example1.pdf','I');
Note that I changed the include paths on top. I also commented the line where you call SetDisplayMode.
You might have some elemets such as got unbalanced in your pdf html. Some elements might not have been closed or started.
It cost me 2 days to find this issue.