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.
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.
any way to add malayalam font to PDF generation. any methods existing?
mPDF supports default malayalam font. after including mpdf folder to third party directory in codeIgniter insert code like
$html = $this->load->view('masterentries/pdfReport',$data,TRUE);
include_once APPPATH.'/third_party/mpdf/mpdf.php';
$pdf = new mPDF('ml', 'A4');
$pdf->SetFooter($_SERVER['HTTP_HOST'].'{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html); // write the HTML into the PDF
$pdf->Output();
will produce the pdf containing malayalam.
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
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 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.