FPDF image not displaying - php

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

Related

FPDF page break in fpdf

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

How to export variables data with fpdf

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.

magento FPDF error: Some data has already been output, can't send PDF file

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">

how can i make the horizontal pdf file

hi guys how can i make a harizontal pdf file by using php.i created for the register form(name ,email, street,city country) can any one say some ideas.
thanks in adv
For that purpose you need to include a library of php after downloading it FPDF
And this is just an example to use fpdf library.
<?php
require('fpdf.php');
$pdf=new FPDF('l','mm','A4');
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'NAME');
$pdf->Cell(40,30,'EMAIL');
$pdf->Cell(40,50,'STREET');
$pdf->Cell(40,70,'CITY');
$pdf->Cell(40,100,'COUNTRY');
$pdf->Output();
?>
Take a look at FPDF to generate the PDF, or for converting HTML straight into a PDF - wkhtmltopdf.

Can FPDF/FPDI use a PDF in landscape format as a template?

I am trying to import an existing PDF as a template with FPDI. The template is in landscape format. If I import the template into a new document the template page is inserted in portrait form with the content rotated 90 degrees. If my new document is in portrait the full content appears, but if the new document is also landscape, the content is cropped.
Is it possible to use a landscape template with FPDI?
sure, it is no problem. Just add "L" as parameter when calling "addPage()". Here is a sample which works fine for me (the template is in landscape)
<?php
require_once('fpdf.php');
require_once('fpdi.php');
$pdf =& new FPDI();
$pdf->addPage('L');
$pagecount = $pdf->setSourceFile('template.pdf');
$tplIdx = $pdf->importPage(1);
$pdf->useTemplate($tplIdx);
$pdf->SetFont('Arial');
$pdf->SetTextColor(255,0,0);
$pdf->SetXY(25, 25);
$pdf->Write(0, "This is just a test");
$pdf->Output('newpdf.pdf', 'F');
?>
Finally got to look at this problem again... Although crono's answer is perfectly valid. It seems this only works with more recent versions of the FPDI tools. Upgrading from v1.1 to v1.3 solves the problem.

Categories