Generate PDF file in my web app by using php - php

I want to generate a PDF file in my web app. I am using php and I tried fpdf library
I tried to create pdf by using following code:
<?php
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Привет мир!');
$pdf->Output();
?>
It works when we use english characters inside file, but file encoding is ANSI and inside writen russian characters I get that error:
if I change file encoding to utf-8 get error:
FPDF error: Some data has already been output, can't send PDF file (output started at Z:\home\fpdf\www\tutorial\tuto1.php:1)
Who knows how to resolve the problem, please help me !

$pdf->Cell(40,10, iconv('UTF-8', 'CP-1251', $str) );
Make sure you're saving your document as UTF8

Related

mPDF error: Some data has already been output to browser

I'm using mpdf to convert and display html to PDF
when i run it on my local pc/server i see the pdf, but when i run it on my site server i'm getting this error:
mPDF error: Some data has already been output to browser, can't send PDF file
$url = "http://".SITE_DOMAIN."/itinPage-printVer.php?itinID=".$_GET['itinID'];
$htmlStr = file_get_contents($url);
include('html2pdf/mpdf.php');
//ob_end_clean();
$mpdf=new mPDF('utf-8');
$mpdf->WriteHTML( $htmlStr);
$mpdf->Output();
exit;
Could it be folder permissions?
I addressed the problem successfully by changing the encoding of the .php file which contains the mpdf code from UTF-8 to ANSI!
Note: The file encoding does not affect the contents of your $htmlStr variable, so the output can be in any character set you wish (utf-8, etc).

php fpdf simple file is not working

I have a simple app where the php file updates the database then it generates a pdf file and stores in local folder. below is the code. the pdf file is getting generated and working fine. but am not able to add text into it. when i insert image its working. but inserting cell or text is not working. i dont know whats gone wrong.. pl;z help me.. thanks
$pdf = new FPDF();
$pdf->AddPage();
// $pdf->Image('images/logo.png',90,15,90,0,'PNG');
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output("pdffiles/testsuccess.pdf",'F');
When I test your code it works fine for me. The only thing I DON'T see in your code is the require statement for the fpdf.php file.
heres the FULL php file contents for the example that works for me:
<?php
require('fpdf/fpdf.php'); // MAKE SURE YOU HAVE THIS LINE
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output("testsuccess.pdf",'F');
?>
Finally, I found it.. weird but it will be very useful for the developers who is new to fpdf... I have mentioned that image is working and only text is not working.. Means you should include the fonts folder which comes with fpdf when downloading. If fonts folder is missing it does not print any text..
Thank you

how to generate pdf and save to server

Well, i'm trying to generate pdf file and save it to server but when i'm trying to view that pdf its showing invalid format.
Code :
<?php
$content = "32w434";
file_put_contents("xyz.pdf",$content);
?>
If i change file from pdf to doc it works and opening perfectly but not pdf
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output();
file_put_contents("xyz.pdf",$content);
That code giving output i don't want output on browser and still file is corrupted / invalid format
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$content = $pdf->Output('doc.pdf','F');
http://www.fpdf.org/
You can use FPDF to generate pdf file....
http://www.fpdf.org/
This is simple and best way to generate pdf...
Use TCPDF to generate PDF file,
TCPDF Site: http://www.tcpdf.org/
Demo: http://www.tcpdf.org/examples.php
#Mohit,
This
$content = $pdf->Output('doc.pdf','F');
generates a new file. If your file is generated using variables that can change in php, the pdf file you see on screen, will be different from the pdf file saved on the server.
You should generate pdf file using pdf libraries such as tcpdf or fpdf. Putting contents directly into a file with .pdf extension will definitely not working since pdf has its own file format

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

well I get this error trying to use php class FPDF:
FPDF error: Some data has already been output, can't send PDF file (output started at
/mnt/webc/e1/12/5691512/htdocs/adminpanel/fpdf/test.php:1)
My test.php
<?php
require_once("fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
There are no blanks or anything... So where is the error???
Some editors adds BOM at the start of a file.
View your file in hex and remove it
it is becouse of two reason
1-there is no leading space before the opening
2-are some data has been output before
The solution of 2 reson is
go to fpdf.inc.php
and find this funciton
function Output($name='', $dest='') {
and then in the defination of this function write this at starting
ob_clean();
this will clean the previous output.
For me, it was the deprecation warnings. I added & ~E_DEPRECATED to my error_reporting in my php.ini and rebooted Apache.
I got same problem in Live Server only. But its working on Local Machine. Adds BOM at the start of a file also its not working on Server. But working in local Machine. Some changes need in your Live code.
1. Place the ob_start() in first line of your file.
Example:
ob_start();
$DOCROOTPATH = $_SERVER['DOCUMENT_ROOT'];
$DOCROOTBASEPATH = dirname($_SERVER['DOCUMENT_ROOT']);
include_once($DOCROOTPATH."/lib/commonarray.inc");
include_once($DOCROOTPATH."/includes/class.payslip.php");
include_once($_SERVER['DOCUMENT_ROOT']."/menucontrol.php");
define('FPDF_FONTPATH','fpdf/font/');
require('fpdf/fpdf.php');
2. After the $pdf->Output(),you should place ob_end_flush() in Same File .
Example:
$pdf=new PDF();
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times','',12);
$pdf->pdfAllPages();
$pdf->Output();
ob_end_flush();
Now go to hit the browser and see the pdf which you want.
(OR)
Some editors adds BOM at the start of a file.
View your file in hex and remove it

how to use pdf library in php?

When i start it's showing this error
<?php
$p = PDF_new();
?>
Fatal error: Call to undefined function PDF_new() in D:\wamp\www\upload.php on line 2
I am using Wamp Server. I tried in XAMPP also. Is there any directives i have to enable to execute the code ?
i suggest to you tcpdf. it was good for me.
some feature:
no external libraries are required
for the basic functions;
all standard page formats, custom
page formats, custom margins and
units of measure;
UTF-8 Unicode and Right-To-Left
languages;
I think http://www.fpdf.org/ is the best PDF library for PHP.
Download latest version from http://www.fpdf.org.
Put this library folder on your root server or in your project.
Create on test file named test.php & put below code in file as below.
<?php
include("fpdf/fpdf.php");
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',36);
$pdf->Cell(40,10,'Hello World!');
$pdf->Output();
?>
It will create one pdf file with contents "Hello World!" in it.
You are done..

Categories