$sql = "SELECT doc FROM file_loc where file_id='1'";

while($results = mysql_query("$sql") or die("Invalid query: " . mysql_error());
{
    // set the header for the image
    header('Content-type: application/pdf');
    header('Content-Disposition: inline; filename = "' .$results . '"');
    header('Content-Transfer-Encoding: binary');

    if (isset($results))
    {
        $pdf = new FPDI();

        // get the page count
        $pageCount = $pdf->setSourceFile($results);

        // iterate through all pages
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            // import a page
            $templateId = $pdf->importPage($pageNo);

            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

             // create a page (landscape or portrait depending on the imported page size)
            if ($size['w'] > $size['h']) {
                $pdf->AddPage('L', array($size['w'], $size['h']));
            } else {
                $pdf->AddPage('P', array($size['w'], $size['h']));
            }

            // use the imported page
            $pdf->useTemplate($templateId);

            if ($size['w'] > $size['h']) {
                $pdf->SetFont('Helvetica');
                $pdf->SetXY(500, 189);
                $pdf->Write(0, 'A complete document imported with FPDI landscape');
            } else {
                $pdf->SetFont('Helvetica');
                $pdf->SetXY(5, 260);
                $pdf->Write(0, 'A complete document imported with FPDI portrait');
            }
      }

      // Output the new PDF
      $pdf->Output('newpdf.pdf', 'D');
}

using these codes the pdf is not displaying in my browser error message (failed to load pdf). when i use comment the fpdi section my pdf display normally and then i uncomment the fpdi section it gives message i am new to fpdf and fpdi libraries please help!!

Recommended Answers

All 9 Replies

while($results = mysql_query("$sql") or die("Invalid query: " . mysql_error());

That's rather messy. Try:

$result = mysql_query($sql) or die('Invalid query: ' . mysql_error());
if ($result && $row = mysql_fetch_assoc($result))
{
    $doc = $row['doc']; // This is your filename, not $results
}

still it does not work.. i have added these line of code and it does not preview the pdf at all.

still it does not work

Be more specific.

Show your new code.

$sql = "SELECT doc FROM file_loc where file_id='1'";

$result = mysql_query($sql) or die('Invalid query: ' . mysql_error());
if ($results && $row = mysql_fetch_assoc($results))
{ 
$doc = $row['doc']; // This is your filename, not $results
}

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename = "' .$doc . '"');
header('Content-Transfer-Encoding: binary');
echo mysql_result($doc, 0);

i have insert the piece of code you have given as you can see in the code. its still not displaying the pdf

Member Avatar for diafol

Below:

$result = mysql_query($sql) or die('Invalid query: ' . mysql_error());
if ($results && $row = mysql_fetch_assoc($results))

You use $result and $results - use one or the other.

hello,,

Actually i have change some lines of code

    <?php

include('connection.php');

require_once('fpdf/fpdf.php');

require_once('fpdi/fpdi.php');

// get the image from the db

$sql = "SELECT doc FROM file_loc where file_id='1'";

$results=mysqli_query($dbconn,$sql);

$row=mysqli_fetch_array($results);

header('Content-type: application/pdf');

header('Content-Disposition: inline; filename = "' .$row . '"');

header('Content-Transfer-Encoding: binary');

header('Accept-Ranges: bytes');

//using FPDI library

if (isset($row[0]))

{

    $pdf = new FPDI();

// get the page count

    $pageCount = $pdf->setSourceFile($row[0]);

// iterate through all pages

    for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {

    // import a page

        $templateId = $pdf->importPage($pageNo);

    // get the size of the imported page

        $size = $pdf->getTemplateSize($templateId);

    // create a page (landscape or portrait depending on the imported page size)
        if ($size['w'] > $size['h']) {

            $pdf->AddPage('L', array($size['w'], $size['h']));

            } else {

            $pdf->AddPage('P', array($size['w'], $size['h']));

            }

    // use the imported page

        $pdf->useTemplate($templateId);

        if ($size['w'] > $size['h']) {

            $pdf->SetFont('Helvetica');

            $pdf->SetXY(500, 189);

            $pdf->Write(0, 'A complete document imported with FPDI landscape');

            } else {

            $pdf->SetFont('Helvetica');

    //$pdf->SetY(-15);

            $pdf->SetXY(5, 260);

            $pdf->Write(0, 'A complete document imported with FPDI portrait');

            }

    }

// Output the new PDF

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

}
Member Avatar for diafol

Ok. So does it work?

no it does not work. i am still stuck with the same problem any help?

Hi,

according to FPDF manual the Output() arguments must be reversed, instead of:

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

Do:

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

Documentation:

Also, if you want to force the download, then change the Content-Disposition header to attachment.

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.