Hi frens,

I have already asked this question in another thread, but then felt this issue was irrelevent to that thread.

I have a requirement to generate a invoice in pdf format and download it.

i have a dynamic form, where user can enter date, choose option from dropdown, add/remove rows depending upon no. of products.

code in abc.php

<form action="abc.php" method="POST">
Enter ur name<input type="text" name="chk">
<input type="submit" value="save" name="submit">
</form>

i ve written the code to save the user input into db in the same file. it works fine.

to generate pdf, i looked around in internet and found pdflib lite, r&os, fpdf to generate pdf, i decided to use fpdf(hoping that this has good support from its makers),i generated some pdfs to understand how it works and its working fine.

now to generate the invoice, i wrote something like this in abc.php

<?php
/*
codes to save the user input values to db(this works fine)
*/
require('../fpdf.php');
class PDF extends FPDF
{
//some functions
}
$pdf=new PDF();
//
//more pdf codes
//
$pdf->Output("myfile.pdf");
if($result == true)

echo "<script>window.location.href=xyz</script>";	
	
else

die("error");

?>

if i run this pdf separately in genratepdf.php it runs fine, however i have lots of user input to display in the pdf, and i could not figure out how to pass the values to different file, cos i already have a submit button and action=abc.php,so i wrote it in the same abc.php file and this doesnt generate pdf.

so my doubts are is it ok to redirect the page after "$pdf->Output();" ??

if not, will i have to keep this code in different file?? then how do i pass all the user input values(minimum 15- max depends on the no. of products)to that file?? i dont think using href is a good option, at the same time i dont know any other alternative.:@

please suggest some alternative for this..

Also, i wanted some guidance on how to get the dialogue box asking to open the file or save the file, and user can choose to save the file to download to the system.

thanks for your time and suggestion.

Recommended Answers

All 12 Replies

$pdf->Output() writes a PDF file. This cannot contain JavaScript. The browser will not understand your code.
For passing variables between scripts use sessions and the $_SESSION array.

add one more parameter to following function call

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

THIS WILL AUTOMATICALLY ASK USER TO SAVE/OPEN

hey smantscheff and urtrivedi thanks for your replies.

i had a doubt abt _session though. will all the user entered data be in session automatically(like _POST) or do i have to explicitly assign to the session before passing it?

thanks

I THINK YOUR ORIGINAL QUESTION HAS NOTHING TO DO WITH SESSIONS.

Any how You must set and use session variables there is no link between post and session.


logincheck.php

<?php
session_start();
if($validuser) //check login details if valid then set session variables
{
   $_SESSION['username']=$loggeduser;//logged user may be userid, email or whatever you like to set.
   $_SESSION['on']="1";
   $_SESSION['xyz']="pqr";
}
?>

anyfile.php

<?php
session_start()
if(!$_SESSION['on'])  //if user not logged then send to login page
{
   header('location:login.php');
   exit;
}

echo "welcome {$_SESSION['username']}";
?>

hey urtrivedi, i agree, my 1st question was not concerning session, sorry for that, anyways thanks for the help.

i did something like this
abc.php

<html>
<head>
</head>
<body>
<form method="post" action="abc.php">
First name: <input type="text" name="firstname" id="fname" /><br />
Last name: <input type="text" name="lastname" id="lname" />
<input type="submit" name="submit" value="save"/>
</form>
</body>
</html>
<?php
session_start();
$fname = $_POST['fname'];
$lname = $_POST['lname'];

$_SESSION['fname'] = $_POST['fname'];
$_SESSION['lname'] = $_POST['lname'];
session_write_close();

//code to save the data into db

header(location:generatepdf.php);
?>

in generatepdf.php, i am using $_SESSION to display the user input.
but when i run this abc.php i get this error

"Warning: require(../fpdf.php) [function.require]: failed to open stream: No such file or directory in /mnt/opt/lampp/htdocs/GEMS/web/generatepdf.php on line 4"

but i have kept all the required files like fpdf.php makefont.php in the same folder as the generatepdf.php

can anyone help me to get this straight.

is it because i am using header to redirect to generatepdf.php?? why is this happening i wud like to know..

You must also use session_start() in your generatepdf.php before using session variable. (this is not something causing problem, it is addtional hint)

Now I never merge all folders in one following is my directory structure

webroot
|--myphpfiles(folder)
|--generatepdf.php
|--fpdf(folder)
|--fdpf_support_folders
|--fpdf.php

so when I refer fpdf in my generatepdf.php, i will write

require(../fpdf/fpdf.php)

Diretory structure is attached

Check how your files are stored

thanks a ton urtrivedi, now that error is gone but i got another one grrrrr....

its

"Warning: fopen(../images/gemsheader.png) [function.fopen]: failed to open stream: No such file or directory in /xyz/fpdf/fpdf.php on line 1226
FPDF error: Can't open image file: ../images/gemsheader.png"

i ve kept the gemsheader.png in /xyz/web/images
generatepdf.php is in /xyz/web

in generatepdf.php i ve written like this

function Header()
{
	//Logo
	$this->Image('../images/gemsheader.png',10,8,190);
	//Arial bold 15
	$this->SetFont('Arial','B',15);
	//Move to the right
	$this->Cell(60);
	//Line break
	$this->Ln(40);
}

what could be the issue here...phew..i ve been stuck with this issue since morning..

thanks

Your image directory structure must be like attached file

forget my preovis post

give only single dot instead of double dot

$this->Image('./images/gemsheader.png',10,8,190);

yiiiiipeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee

all thanks to you i got to see the output, before the end of todays working hours...
what a relief..

thank you sooo much..

@Laura if you want to be helped then open a new thread, explain the problem and attach the relevant code. Bye!

Sorry Sir. But I think nothing wrong with the code. But, it cant be export to pdf using fpdf. I dont know where to start explain. Here is the code, first is ultrasonictr.php and the second is pdf.php.



<form action="merging.php" name="visultra" method="POST">
<script type="text/javascript">
$(function ()  
{
   var dataSource = [
 //data ini ambil dari rata2/average ny yang dari yg ultrasonic.php
    { thick: "Average Thickness 10-20 Below Surface", prec1: <?php echo $has->av1 ?>},
    { thick: "Average Thickness Above Surface Level", prec2: <?php echo $has->av2 ?> },
    { thick: "Average Thickness 10-20 Above Surface", prec3: <?php echo $has->av3 ?> },
    { thick: "Average Thickness 50-100 Above Surface Level", prec4: <?php echo $has->av4 ?> },
    { thick: "Average Thickness 150-200 Above Surface Level", prec5: <?php echo $has->av5 ?> }
    ];
    $("#chartContainer").dxChart({
    dataSource: dataSource,
    commonSeriesSettings:{
        argumentField: "thick"
    },
    panes: [{
            name: "firstPane"
        }],
    series: [{ 
            type: "bar",
            valueField: "prec1",
            name: "Numb1",
            label: {
                visible: true,
                customizeText: function (){
                    return this.valueText  + " mm";
                }
            }
        },
        {
            type: "bar", 
            valueField: "prec2",
            name: "Numb2",
            label: {
                visible: true,
                customizeText: function (){
                    return this.valueText + " mm";
                }
            }
        }, {
            type: "bar",
            valueField: "prec3",
            name: "Numb3",
            label: {
                visible: true,
                customizeText: function (){
                    return this.valueText  + " mm";
                }
            }
        }, {
            type: "bar",
            valueField: "prec4",
            name: "Numb4",
            label: {
                visible: true,
                customizeText: function (){
                    return this.valueText  + " mm";
                }
            }
        }, {
            type: "bar",
            valueField: "prec5",
            name: "Numb5",
            label: {
                visible: true,
                customizeText: function (){
                    return this.valueText  + " mm";
                }
            }
        }
    ],    
    valueAxis: [{
        pane: "firsPane",
        grid: {
            visible: true
        },
        title: {
            text: "Thickness Result, mm"
        }
    }],
    legend: {
        verticalAlignment: "bottom",
        horizontalAlignment: "center"
    }
});
}
);
</script>
        <div class="line"></div>      
        <div class="title">
            <h1>Charts</h1>&nbsp;&nbsp;&nbsp;<h2>Ultrasonic Thickness</h2>
        </div>
        <div class="content">
            <div class="pane">
                <div class="long-title"><h3></h3></div>
                <div id="chartContainer" style="width: 100%; height: 440px;"></div>
                <div class="credits">Dimensi Barumas Perdana</div>
            </div>
        </div>

<table border=1 width="30%">
    <tr>
    <th>Average 1</th>
    <th>Average 2</th>
    <th>Average 3</th>
    <th>Average 4</th>
    <th>Average 5</th>
    <th>Category</th>
    <th>Remark</th>
    </tr>
    <tr>
        <td><center><b><?php echo $has->av1?></b></center></td>
        <td><center><b><?php echo $has->av2?></b><center></td>
        <td><center><b><?php echo $has->av3?></b><center></td>
        <td><center><b><?php echo $has->av4?></b><center></td>
        <td><center><b><?php echo $has->av5?></b><center></td>
        <td><center><b><?php echo $has->cat?></b><center></td>
        <td><center><b><?php echo $has->remark?></b><center></td>
    </tr>
    </table>
<p><p><p>&nbsp;
<table border=1 width="30%">
    <tr><td><center><input type="submit" name="merging" value="Merging"></center></td></tr>
</table>

<!-- this is pdf.php-->
<?php
$mysqli = new mysqli('localhost', 'root', '1234', 'dbparea');
if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $mysqli->connect_error);
}

define('FPDF_FONTPATH','fpdf/font/');
require('fpdf/fpdf.php');

class PDF extends FPDF {
function Header() {
    $this->Image('logo.jpg',1.1,1.05,2.1);
    $this->SetFont('Arial','B',20);
    $this->Cell(19,1,'Data Komentar',1,0,'C');
    $this->Ln();
}

function Footer() {
    $this->SetY(-1.5);
    $this->SetFont('Arial','I',8);
    $this->Cell(0,1,'Halaman '.$this->PageNo().'/{nb}',0,0,'C');
}
}

$pdf=new PDF('P','cm','A4');
$pdf->Open();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Arial','B',24);
$pdf->SetTextColor(220,50,50);
$pdf->Ln();
$pdf->MultiCell(0,2,$subjek,0,'C');
$pdf->SetFont('Arial','B',12);
$pdf->SetTextColor(0,0,0);
$pdf->SetX(8.2);
$pdf->Write(1,'Oleh: ');
$pdf->SetFont('Arial','BIU',12);
$pdf->Write(1,$nama,$website);
$pdf->SetFont('Arial','B',12);
$pdf->Ln(2);
$pdf->MultiCell(0,1,$komentar);
$pdf->Output();
?>

When I click "SAVE", it call pdf.php and built a doc into pdf. But, it displayed : "Failed to load PDF Document". I dont know what wrong.
Thanks for the help. God bless you.

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.