hi friend , m facing problem to generate pdf file. i m using following code:-

if(isset($_POST['pro']))
{  
    include "common.php";// for db conectivity
    $tablename="rentsum";
    $sql = "Select empcode,ticket_no,ctgcode,empname,allot_dt,to_char(qtrno) qtrno,type,seccode,wt,lf,'0' arr ,CONSERV con,met_chg,'0' eu from $tablename where MONTH='$month' AND YEAR='$year' and type not in ('4','5','0') order by qtrno "; 
    $result =oci_parse($conn,$sql);
    oci_execute($result); 
    $filename="hrent.pdf";
header("Content-type: application/pdf");
header("Content-disposition: pdf" . date("Y-m-d") . ".pdf");
//header("Content-disposition: attachment;filename=$filename");
while($result=oci_fetch_array($sqldata,OCI_BOTH))
{
    $empcode=$result['EMPCODE'];
    echo $empcode."\r\n";

}

}

but it is not working. means pdf flie is generate but when im open the file it show error nmassage the this file is in damage format. plz help me that

means pdf flie is generate

Actually, no, it's not. This code does not generate a PDF file. All it does is print the EMPCODE values in plain-text, on separate lines, and then tells the browser that that is a PDF file. It's still just a normal text file, all you've done is confuse the browser into thinking it's PDF.

PDF generation is more complicated than this. It uses a standard that you can't generate easily with echo statements. There is a PDF Extension in PHP that you can use to set up a PDF file, and several popular 3rd party libraries as well, like TCPDF and FPDF.

To add to the above reply: PDF Extension uses PDFlib library which is not free (you have to purchase it). TCPDF and FPDF are free. TCPDF is better maintained and actively developed, while FPDF is being more or less idle (last version from June 2011). They both do their job very good and have plenty of examples.

thanks friends for your help , i have download tcpdf , i have aslo see the examples. they r very nice . but if i put any of the example.php out of example folder it shows error:-"Class 'TCPDF' not found". while i have modify the path of all the file to be include correctly

The path in the command that includes the library must be correct. If you move the example script change the path in the require_once command.

What is the path to the TCPDF library? What is the path in the require_once command? Can you post both? And let us know what OS do you use.

thanks broj1 sir , i have solve that error by ur kind help.now i m using the following code:-

<?php

$tcpdf_include_dirs = array(realpath('tcpdf.php'), '/usr/share/php/tcpdf/tcpdf.php', '/usr/share/tcpdf/tcpdf.php', '/usr/share/php-tcpdf/tcpdf.php', '/var/www/tcpdf/tcpdf.php', '/var/www/html/tcpdf/tcpdf.php', '/usr/local/apache2/htdocs/tcpdf/tcpdf.php');
foreach ($tcpdf_include_dirs as $tcpdf_include_path) {
    if (@file_exists($tcpdf_include_path)) {
        require_once($tcpdf_include_path);
        break;
    }
}
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);


// set font
$pdf->SetFont('times', 'BI', 20);
///$empcode='913919';
 include "../common.php";// for db conectivity
$tablename="rentsum";
$sql = "Select empcode,ticket_no,ctgcode,empname,allot_dt,to_char(qtrno) qtrno,type,seccode,wt,lf,'0' arr 
,CONSERV con,met_chg,'0' eu from $tablename where MONTH='1' AND YEAR='2013' and type not in ('4','5','0') order by qtrno ";
$sqldata =oci_parse($conn,$sql);
oci_execute($sqldata);


// add a page
$pdf->AddPage();

// set some text to print
while($result=oci_fetch_array($sqldata,OCI_BOTH))
{
$empcode=$result['EMPCODE'];
//echo $empcode."\r\n";
$txt = <<<EOD
$empcode
EOD;
$pdf->Write(0, $txt, '', 0, 'C', true, 0, false, false, 0);        
}
$pdf->Output('example_002.pdf', 'I');

?>

but it shows error :-

TCPDF ERROR: Some data has already been output, can't send PDF file

and move over i want that this file should create on clint pc not in the same browser
like the text file create on clint pc by:-
"header("Content-type: application/pdf");
header("Content-disposition: attachment;filename=test.pdf");"

m using windows 8 os , apache 2.2 and php 5 and orical 10g as db

Lines 3 to 9 are a strange way of cycling through paths to find the library. You should know where the library resides, shouldn't you. But if it works and you are happy with it, it is OK with me, too.

TCPDF ERROR: Some data has already been output, can't send PDF file

Make sure that fetching a row does not return an error. Maybe you should change the index to lower case:

$empcode=$result['empcode'];

or even better first check if it exists:

if(isset($result['EMPCODE'])) {
    $empcode=$result['EMPCODE'];
} else {
    $empcode = 'NOT DEFINED';
}

The whole while loop could be shortened, no need to use a $txt variable:

// set some text to print
while($result=oci_fetch_array($sqldata,OCI_BOTH))
{
    if(isset($result['EMPCODE'])) {
        $empcode=$result['EMPCODE'];
    } else {
        $empcode = 'NOT DEFINED';
    }
    //echo $empcode."\r\n";
    $pdf->Write(0, $empcode, '', 0, 'C', true, 0, false, false, 0);
}

and move over i want that this file should create on clint pc not in the same browser like the text file create on clint pc by:-

Use the D parameter for the Output method. It will ask you where to save the document on local PC:

$pdf->Output('example_002.pdf', 'D');

I have found this code useful for myself as i was also facing this problem.

mughaltent sir plz share ur code, so that i can also solve the problem

Have you tried the above hints? Do they not solve the problem? If not where did it stop?

<?php
require('classes/fpdf/fpdf.php'); 
class PDF extends FPDF {

function Header() {
    $this->SetFont('Times','',12);
    $this->SetY(0.25);
    $this->Cell(0, .25, "John Doe ".$this->PageNo(), 'T', 2, "R");
    //reset Y
    $this->SetY(1);
}

function Footer() {
//This is the footer; it's repeated on each page.
//enter filename: phpjabber logo, x position: (page width/2)-half the picture size,
//y position: rough estimate, width, height, filetype, link: click it!
    $this->Image("logo.jpg", (8.5/2)-1.5, 9.8, 3, 1, "JPG", "http://www.phpjabbers.com");
}

}

//class instantiation
$pdf=new PDF("P","in","Letter");

$pdf->SetMargins(1,1,1);

$pdf->AddPage();
$pdf->SetFont('Times','',12);

$lipsum1="Lorem ipsum dolor sit amet, nam aliquam dolore est, est in eget.";

$lipsum2="Nibh lectus, pede fusce ullamcorper vel porttitor.";

$lipsum3 ="Duis maecenas et curabitur, felis dolor.";

$pdf->SetFillColor(240, 100, 100);
$pdf->SetFont('Times','BU',12);

//Cell(float w[,float h[,string txt[,mixed border[,
//int ln[,string align[,boolean fill[,mixed link]]]]]]])
$pdf->Cell(0, .25, "lipsum", 1, 2, "C", 1);

$pdf->SetFont('Times','',12);
//MultiCell(float w, float h, string txt [, mixed border [, string align [, boolean fill]]])
$pdf->MultiCell(0, 0.5, $lipsum1, 'LR', "L");
$pdf->MultiCell(0, 0.25, $lipsum2, 1, "R");
$pdf->MultiCell(0, 0.15, $lipsum3, 'B', "J");

$pdf->AddPage();
$pdf->Write(0.5, $lipsum1.$lipsum2.$lipsum3);

$pdf->Output();
?>

I create a PDF in Portrait mode with a Letter size and measured in inches. Then, I set the margins to 1 inch, add our first page, and then set the Font to Times, Size 12. Then, my 3 dummy texts, and after that I set the fill color that will be the space behind my title's text with a bold/underlined modifier. After that, I print the title, then reset the font and specify some different types of MultiCells. The page is made up of cells on an x,y plane in FPDF, which makes it pretty convenient to work with for basic PDF editing.

broj1 sir i have tryed all the hint u have given
the error comes when am using while loop to feach the record and print it in pdf file

What is the error?

the error is

TCPDF ERROR: Some data has already been output, can't send PDF file

currently m useing following code

<?php


$tcpdf_include_dirs = array(realpath('tcpdf.php'), '/usr/share/php/tcpdf/tcpdf.php', '/usr/share/tcpdf/tcpdf.php', '/usr/share/php-tcpdf/tcpdf.php', '/var/www/tcpdf/tcpdf.php', '/var/www/html/tcpdf/tcpdf.php', '/usr/local/apache2/htdocs/tcpdf/tcpdf.php');
foreach ($tcpdf_include_dirs as $tcpdf_include_path) {
    if (@file_exists($tcpdf_include_path)) {
        require_once($tcpdf_include_path);
        break;
    }
}
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);


// set font
$pdf->SetFont('times', 'BI', 20);
//$empcode='913919';
include "../common.php";// for db conectivity
$tablename="rentsum";
$sql = "Select empcode,ticket_no,ctgcode,empname,allot_dt,to_char(qtrno) qtrno,type,seccode,wt,lf,'0' arr 
,CONSERV con,met_chg,'0' eu from $tablename where MONTH='1' AND YEAR='2013' and type not in ('4','5','0') order by qtrno ";
$sqldata =oci_parse($conn,$sql);
oci_execute($sqldata);
//echo $sql;

// add a page
$pdf->AddPage();
    // set some text to print
    while($result=oci_fetch_array($sqldata,OCI_BOTH))
    {
    if(isset($result['EMPCODE'])) {
    $empcode=$result['EMPCODE'];
    } else {
    $empcode = 'NOT DEFINED';
    }
    //echo $empcode."\r\n";
    $pdf->Write(0, $empcode, '', 0, 'C', true, 0, false, false, 0);
    }
     $pdf->Output('example_002.pdf', 'D');  

and the output is

TCPDF ERROR: Some data has already been output, can't send PDF file

The error message is pretty clear: there has been some data output before the PDF is assembled and output. Now, the only candidate I see is an error message that might be produced due to a unsuccessful query. Have you doublechecked that the query is OK (when you echo the query you can paste it into the database client you use and test it)?

i have cheked the sql query, it giving correct out put, now wht to do

You can do two things but it is not guaranted that any of them leads to the solution.

  1. you can post the common.php script so we can check whether it might be sending some output
  2. you can post the structure and a few rows of representational data of the rentsum table so the code can be tested. I personally can test it only in mysql, maybe someone else can test it in oracle.

If you chose to post anything, make sure you remove any possible confidential data.

when i am trying to generate pdf using php code..that time i will face this error "FPDF error: Could not include font definition file" please help me friends.

don't put makefont in font folder ...It will give show "FPDF error: Could not include font definition file".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.