I am using PHP and FPDF to generate a PDF with a list of items. My problem is, item does not goes to a second or third page.
I want to print next block of data in 2nd page of pdf.
Someone please help me. I have used setautopage break() but not working. PLEASE HELP!
<?php
require('fpdf.php');
class PDF extends FPDF {
function Header() {
$this->SetFont('Arial','B',10);
$this->Rect(50,30,100,30,'F');
$this->Text(80,45,"3D");
$this->SetXY(20,20);
$this->Cell(30,10,'A',1,0,'L');
$this->SetXY(80,20);
$this->Cell(30,10,'B',1,0,'L');
$this->SetXY(80,60);
$this->Cell(30,10,'C',1,1,'L');
$this->SetXY(150,60);
$this->Cell(30,10,'D',1,1,'L');
}
function Footer() {
$this->SetY(-12);
$this->Cell(169,20,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
$pdf=new PDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->AliasNbPages();
$pdf->Output();
You would need to use GetY and Addpage for this purpose.
Use GetY to get the current position, subtract it from the height of your document. If that is less than 6x (you have 6 rows) your multicell height, then force a page break by using AddPage.
See a detailed answer here
Related
I am trying to work with fpdf class in yii. So I import the total fpdf folder in yii vendor folder then tried to import in view file like:
Yii::import('application.vendors.*');
require_once('fpdf/fpdf.php');
then just paste a class as follows
class PDF extends FPDF
{
// Page header
function Header()
{
// Logo
$this->Image('logo.png',10,6,30);
// Arial bold 15
$this->SetFont('Arial','B',15);
// Move to the right
$this->Cell(80);
// Title
$this->Cell(30,10,'Title',1,0,'C');
// 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(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
$pdf->Cell(0,10,'Printing line number '.$i,0,1);
$pdf->Output();
?>
But that ends up with a error as "This pdf document might not be displayed correctly. And if I try to download and view then it says
"File type HTML document (text/html) is not supported". I added
"header('Content-Type: application/pdf');" on the view file with no help.
can anyone help?
Here are my recommendations.
First, follow this instructions and start using composer in your project.
http://main.org.ua/yii1-composer/
It is future, it's worth to learn it.
Then, install this package using composer https://packagist.org/packages/itbz/fpdf
And you are done.
No more manual includes or troubles with custom autoloaders.
I am using PHP and FPDF to generate a PDF. My problem is, the data is not displaying in Arabic. How to show Arabic data in content of PDF?
Here is my code:
require('include/fpdf/fpdf.php');
class PDF extends FPDF {
// Page header
function Header()
{
$this->Image('include/fpdf/tutorial/logo.png',10,6,30);// Logo
$this->SetFont('Arial','B',15);// Arial bold 15
$this->Cell(80);// Move to the right
$this->Cell(30,10,'العنوان',1,0,'C');// Title
$this->Ln(20);// Line break
}
// Page footer
function Footer()
{
$this->SetY(-15);// Position at 1.5 cm from bottom
$this->SetFont('Arial','I',8);// Arial italic 8
$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');// Page number
}}
// Instanciation of inherited class
$pdf = new PDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
for($i=1;$i<=40;$i++)
$pdf->Cell(0,10,' تجربة '.$i,0,1);
$pdf->Output();
FPDF doesn't support natively UTF-8 characters, but only ISO-8859-1 or Windows-1252.
You should try to generate your own font, but I don't find that an easy task. Take a look at this answer for some help.
This is the code for index which will export what ever is in page under HTML varialbe:
<?php
require('fpdf/fpdf.php');
$html = 'kkjkjkjkjjkj';
$pdf = new FPDF();
$pdf->AliasNbPages($html);
$pdf->AddPage($html);
$pdf->SetFont('Times','',12);
$pdf->Output();
?>
Here it's exporting it to PDF but here how I can let the $html to get export to PDF using FPDF
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
1) Have a look at the Manual and Tutorial sections of the FPDF website
There are a lot of simple examples to take a look at.
2) You can't send html code with the AddPage() function. It should be orientation ("P" or "L") and size (like "A4") (see their documentation here)
3) It is not possible to just insert html into FPDF. You have to cut it up into text, links, images, tables and build the PDF content up just as you build up the HTML. There are some script that do support html input, but even those make mistakes now and then. My advise: instead of creating/building/compiling HTML, generate FPDF elements.
This example clearly exports a (simple) $html variable to a PDF
http://fpdf.org/en/tutorial/tuto6.htm
It is not possible to directly write HTML to a PDF using FPDF - you would have to separate the data into strings, set formatting to them, etc - basically, you will not be dealing with HTML anymore.
However, if you are not limited to FPDF, I would suggest looking into wkhtmltopdf, which can take HTML as an input, and render it to PDF using Webkit rendering engine. It can even take a remote URL and convert it to a PDF on your server.
i'm having the next problem:
I try to see a pdf in one magento phtml this is my code:
$fileName = Mage::getConfig()->getOptions()->getMediaDir()."/pdf/Gutschein-v2.pdf";
$pdf = new FPDI();
$pdf->addPage();
$pdf->setSourceFile($fileName);
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at point 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);
// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
//ob_start();
$pdf->Output();
//ob_end_flush();
When i comment ob_start(); i see on my screen the next error:
FPDF error: Some data has already been output, can't send PDF file
When i don't comment that i see the normal pdf format but not the white page with
my pdf, i tried to do that with pure php and everything was ok example:
http://milton.bommelme.com/fpdf/pddf.php
but with magento something are not gut, maybe i don't know how to load the pdf or something else. I'm very new with magento.
thank you.
The error "Some data has already been output, can't send PDF file" appears because Magento has already sent the header output. Look at http://php.net/manual/en/function.header.php for further informations.
So the Output() function of FPDF is also sending header informations and it declares the 'Content-Type' to 'application/pdf'. The main problem here is that you cannot just put a PDF file between HTML tags. PDF is an own format and needs a diffrent presantation mechanism than HTML. Like in the example link you gave the PDF file is embeded by the embed-tag with the right Content-Type declaration:
<embed width="100%" height="100%" name="plugin" src="http://milton.bommelme.com/fpdf/pddf.php" type="application/pdf">
There are also other ways to embed a PDF file in HTML: Recommended way to embed PDF in HTML?
EDIT: For example you can create two view actions. The first one renders the HTML. Inside the HTML the embed-tag calls the second action which will generate the PDF.
I don't think this is the best solution,but it should work easily:
class Foo_Pdf_Controller_SomeController extends Mage_Core_Controller_Front_Action
{
public function viewAction()
{
// View stuff
}
public function getPdfAction()
{
// create pdf
$fpdf->output();
}
}
The embed-tags calls the getPdf() action:
<embed width="100%" height="100%" name="plugin" src="url_to_getPdf_action" type="application/pdf">
I tried to put an image in my pdf document, but when I use that method it only loading for a while, and do nothing (no errors, or such things) only a grey site...
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('logo.png');
$pdf->Output();
So what I do wrong? If I only use text in the pdf, it's no problem, but with the Image. The Image is in the root-directory.
Edit: Added $pdf->AddPage(); as per the comment in Ewan Heming's answer.
Your example doesn't include a method call to add a page to the PDF before placing the image on it:
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image('logo.png');
$pdf->Output();
Sry guys i found out the problem , the resolution of the picture was too big