I have a list of customers who need to get a dynamically generated pdf.
class PDF extends FPDF
{
// Page header
function Header()
{
global $backToTOC;
// Logo
$this->Image('logo.jpg',70,10);
$this->Write(5,'Back to TOC',$backToTOC);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
// Line break
$this->Ln(20);
}
// Page footer
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Arial italic 8
$this->SetFont('Arial','I',8);
// Page number
$this->Cell(5,'Contact support: 1-800-support');
$this->Cell(5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
foreach ($customer as $k => $v)
{
$pdf = new PDF();
$pdf->AliasNbPages();
//....pdf stuff.....
$pdf->Output($v.'.pdf','F');
}
the result of this is a divide by zero error.
PHP Warning: Division by zero in /var/www/lib/fpdf/fpdf.php on line 796
and the footer page numbers show 0. any thoughts?
The Cell method expects the second parameter to be the cell height, not the cell content (see Cell method). When calling:
$this->Cell(5,'Page '.$this->PageNo().'/{nb}',0,0,'C');
you are using 'Page '.$this->PageNo().'/{nb}' as the height and 0 as the content. It should be:
$this->Cell(5, $cellHeight, 'Page '.$this->PageNo().'/{nb}',0,0,'C');
Try setting your font right after creating the PDF object, it worked on my machine.
Also if it doesn't work, please paste a stacktrace.
Related
When I hide header with
$this->setPrintHeader(false);
it still leaves an empty gap (space) from the top. How to remove the gap so the body would appear from the beginning of page?
EDIT1: this the prestashop function I use
public function writePage()
{
$this->SetTopMargin(-50);
$this->SetHeaderMargin(-55);
$this->setPrintHeader(false);
$this->SetFooterMargin(21);
$this->setMargins(10, 50, 10);
$this->SetAutoPageBreak(true, 40);
$this->AddPage();
$this->writeHTML($this->content, true, false, true, false, '');
}
EDIT 2: this combination works for me
$this->setPrintHeader(false);
$this->SetTopMargin(0);
$this->setMargins(10, 20, 10); <- Now I can set margins as I want
Apart from not printing the header you also need to set margins to 0, so in this case:
$this->SetTopMargin(0);
Note that you can manipulate the margin of the header too with $this->SetHeaderMargin();
Edit: you can use negative values too.
I have a function to generate pdf using Fpdf in laravel.
My problems are:
After all Cell I have some extra space. I need to remove that. Please find the image given below.
How can I download this pdf file in to my system. Currently it's just showing in to browser. Code samples are given below.
Code
Controller: Controller.php
public function index()
{
$orders = Order::select('firstname', 'lastname', 'street', 'postal', 'country')->get();
foreach ($orders as $order){
Fpdf::SetMargins(5, 5, 5);
Fpdf::AddPage('L', array(60,90), 'A4');
Fpdf::SetAutoPageBreak(TRUE, 0);
Fpdf::SetFont('helvetica', '', 7); //IF bold letter SetFont('Arial','B',14)
Fpdf::SetTextColor(0, 0, 0);
Fpdf::Cell(10,5,iconv('UTF-8', 'windows-1252', 'Falls unzustellbar, zurück an Absender'),0,"1","L");
Fpdf::SetFont('','U');
Fpdf::Cell(10,5,iconv('UTF-8', 'windows-1252', 'schrillALARM.jetzt c/o 365group • Grasgasse 2 • 93047 Regensburg'),0,"1","L");
Fpdf::SetFont('helvetica', '', 11);
Fpdf::Cell(10,5,$order->firstname,0,1,"L");
Fpdf::Cell(10,5,$order->lastname,0,1,"L");
Fpdf::Cell(10,5,$order->street,0,1,"L");
Fpdf::Cell(10,5,$order->postal,0,1,"L");
Fpdf::Cell(10,5,$order->country,0,1,"L");
}
Fpdf::Output();
exit;
}
Route: Route::get('/test', 'Controller#index');
No experience with FDPF, but you can download this way:
Route::get(
'download/pdf/{pdf}',
function ($pdf) {
$file = // Get file
return response()->download($file);
}
);
Or just from your controller with
return response()->download($pdf);
for saving, just specify output path and filename in your output call string
Fpdf::Output([string dest [, string name [, boolean isUTF8]]])
For your white space though, when you're constructing your PDF document, you can use a default size of one of the following: A3, A4, A5, Letter, Legal with A4 being default. However, you can also declare custom sizes. This is more than likely what you're looking for, as you'll want to play with the sizes to get it with the amount of white space you're looking for. FPDF puts out the canvas first then fills it, so you're white space is coming from a canvas that is too big. This can be done in the constructor, or AddPage as you have done.
VIA Constructor:
//(L)andscape or (P)ortrait, unit type (mm millimeters), array size in mm
$pdf = new FPDF('L','mm',array(100,150));
VIA AddPage (must likely what you're looking for):
currently you have:
Fpdf::AddPage('L', array(60,90), 'A4');
however, params are supposed to be landscape/portrait, Predefined or custom size array, then rotation. So try this:
Fpdf::AddPage('L', array(60,90));
Now you'll need to play with those numbers, more than likely the 90, and shorten that up to rid yourself of the white space.
I am using TCPDF library to write a custom size label with background image and multiple text blocks on it.
The user when sees the preview on screen of the PDF it should show in horizontal, but for printing, I need the full page rotated -90 degrees.
How can I just rotate the whole page for printing version without having to move anything?
Basically:
In my case I've already had to use a new document format for the special sizes my document required.
So I've duplicated that format, created one for Landscape and one for Portrait.
Then based on the $preview variable, if previewing I'm rendering the normal landscape document, but if not previewing, I'm using the Portrait format and orientation and also starting the transformation and rotating everything on page.
Hope this helps someone I've found no other "quick" way to accomplish this kind of full-page rotation.
<?php
// #1 get the preview attribute from
// the form that was submitted from the user
$preview= isset($_POST['preview'])?(int)$_POST['preview']:0;
// load TCPDF for CodeIgniter as a library
$this->load->library('Pdf');
// #2 set default orientation and format
$orientation='L';
$format='MAKE-L';
// #3 if not previewing, switch orientation and format to portrait
if (!$preview) {
$orientation='P';
$format='MAKE-P';
}
// create new pdf object
// (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
$pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage($orientation, $format);
// #4 if not previewing, start transformation
// and rotate everything before inserting any content
if (!$preview) {
// Start Transformation
$pdf->StartTransform();
// Rotate counter-clockwise centered by (x,y)
$pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
}
// put your content here,
// for example set font and add a text
$pdf->SetFont('times', '', 7, '', true);
$pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
/// end content
// #5 if not in preview mode, finish the transformation
if (!$preview) {
// Stop Transformation
$pdf->StopTransform();
}
$pdf->Output('example.pdf', 'I');
/**
* Last but very important note:
* I have added my formats in tcpdf/includes/tcpdf_static.php file.
* >> MAKE-L for Landscape
* >> MAKE-P for Portrait
*/
public static $page_formats = array(
// Make
'MAKE-L' => array( 396.850, 425.196), // = ( h 140 x w 150 ) mm
// Make
'MAKE-P' => array( 425.196, 396.850 ), // = ( h 140 x w 150 ) mm
// .. rest of formats here ../
);
The setPageFormat() method should do the job. You also can pass the parameter to the $format parameter of AddPage():
$pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);
i use FPDF to generate Report From Database and i want the title of the Report to be Printed on the header of the PDF generated
my code look like this
function Header()
{
$name="Export PDF";
$this->SetFont('Arial','B',15);
$this->Image('images/pdflogo.png', 5,5,60);
$this->Image('images/hr1.jpg', 10,25,190);
$this->Text(100,25,'$d');
$this->Cell(80);
$this->SetFont('Arial','B',9);
$this->Ln(20);
}
?>
can any body show me how to do this Please !!
Use: $this->Cell(80, 10, 'Example Report Title' );
It will show Example Report Title on every pages header.
Working on a project and need to generate pdf of the product details using FPDF. The product details are passed into an array and I need the following to get each of the variable elements in the array '$prod_details' into the functions within the class 'PDF' as shown below:
Examples of how I tried passing the variable array elements:
$this->Cell(30,8,$prod_data['prod_name'],0,0,'C');
$this->Cell(30,10,$prod_data['company_name']);
$this->Cell(20,8,$prod_data['prod_cost'],0,0,'C');
I have tried running this script but I keep getting an error message 'Cannot access empty property'...
find the codes below
<?php
#include_once("includes/db.php");
require('fpdf/fpdf.php');
#include_once("includes/class_product_info.php");
$obj = new allProducts();
$prod_data = array();
if(isset($_GET['c_id'])){
$prod_data = $obj->getProdDetails($_GET['c_id']);
class PDF extends FPDF
{
public $prod_data;
public function createData($input){
$this->prod_data = $input;
}
function Header()
{
// Logo
$this->Image('big_logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',20);
// Move to the right
$this->Cell(40);
// Title
$this->Cell(30,10,$this->prod_data['company_name']);
// Draw an header line
$this->Line(10,26,200,26);
// Line break
$this->Ln(20);
}
function Footer()
{
// Position at 1.5 cm from bottom
$this->SetY(-15);
// Begin with regular font
$this->SetFont('Arial','',9);
// Then put a blue underlined link
//$this->SetTextColor(0,0,255);
$this->SetFont('','U');
$this->Write(10,$this->prod_data['company_name'],'http://www.skills.com');
// Arial italic 8
$this->SetFont('Arial','I',9);
// Page number
$this->Cell(0,10,'Page '.$this->PageNo().' ',0,0,'R');
}
function prodDetailTop()
{
// Course title cell
$this->Cell('',10,'',0,0,'C');
$this->Ln();
/* Build cells for the first row */
$this->SetFont('Arial','',10);
$this->SetY(40);
// First Row
$this->Cell(35,8,'Product Name : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['prod_name'],0,0,'C');
$this->SetX(150);
$this->Cell(25,8,'Product Cost : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['prod_cost'],0,0,'C');
$this->Ln();
// Second Row
$this->Cell(35,8,'Discount : ',0,0,'L');
$this->Cell(30,8,$this->prod_data['disc_amt'],0,0,'L');
$this->SetX(150);
$this->Cell(25,8,'No Purchased : ',0,0,'L');
$this->Cell(20,8,$this->prod_data['items_count'].' product(s)',0,0,'L');
$this->Ln();
}
function prodDetailBtm()
{
$this->SetY(80);
$this->Write(10,$this->prod_data['prod_desc']);
}
function generatePageData()
{
$this->AddPage();
$this->prodDetailTop();
$this->prodDetailBtm();
}
}
$pdf = new PDF();
$pdf->createData($prod_data);
//$pdf->Header();
$pdf->generatePageData();
$pdf->Output();
}
else {
?>
<script language="javascript">
window.location = "prod_invoice_err.php";
</script>
<?php
}
?>
Hope to get some help.
Your question is a little vague. It would be helpful it you asked specifically what you're trying to accomplish.
But the first thing I see is that your subclass of the fpdf class, you don't need to write functions to do each and everything you want to do with the pdf. You only need to extend the parts of the class you are overriding (or extending), like header and footer.
So extend it, manipulate header and footer, then close the class. Create your $pdf instance of your new fpdf class, then manipulate that object with your data. You don't need to 'pass in' that data at all.
for instance:
$pdf->Ln(10);
$pdf->Cell($w,9,$title,1,1,'C',true); //from fpdf tutorial
Or, if that doesn't accomplish what you want (although I can't see why it wouldn't, I've done this lots of times), you can always override the constructor. Pass in an array (or event a custom object that you create), and store that in a private variable.