user861020
user861020

Reputation: 1

How to embed dynamic PDFs?

I have a website that dynamically generates PDFs using FPDF (from fpdf.org) based on an HTML form passed to a PHP script. As of right now, it all works fine to the point where the information passes through and the FPDF generates the PDF. However, I've been looking into embedding options and can't quite get it to work properly. I have other areas of my page that embed PDFs, but they are ones that I generate and save to the server. My question is, is it possible to dynamically generate a PDF and output it directly to the browser ($pdf->Output();) inside an emedded area of an HTML page? Right now it just generates and takes up the entire window, but I would like to include other information along with the PDF, such as instructions and what not. I attempted to output the pdf as a string into a variable, via:

$output = $pdf->Output('','S');

This did output the information as a string into the $output variable; however, I wasn't sure if I was able to embed that. I tried specifying a MIME type (as application/pdf), but the only other available attribute is src, so I wasn't sure if I could use the string anywhere. I have used 2 different techniques for embedding PDFs in the past. tags and an with google document viewer, but after toying with them for a while, I wasn't able to get this to work =( Anyone have any ideas?

Upvotes: 0

Views: 3016

Answers (3)

Nick
Nick

Reputation: 81

When you save fpdf as a string, you can use php to encode it to base64 and then pass that to whatever you are using to embed your pdf in your html document. PHP:

$output = $pdf->Output('','S');
$output = base64_encode($output);

In your html document:

<embed src="data:application/pdf;base64,<?php echo $output ?>" type='application/pdf'>

I know this will work in at least chrome. I have not tested it in other browsers. You may need to implement other way to embed pdf's into html pages in order to achieve cross-browser support. I assume that you can figure this out from this point. Hope this helps somebody.

Upvotes: 2

Grizly
Grizly

Reputation: 161

If you are using the same FPDF I am, then you already have all you need!

Simply change your 'I' into 'D' to force downloading, rather than inline.

from:

function Output($name='', $dest='')
{
    //Output PDF to some destination
    if($this->state<3)
        $this->Close();
    $dest=strtoupper($dest);
    if($dest=='')
    {
        if($name=='')
        {
            $name='doc.pdf';
            $dest='I';
        }
        else
            $dest='F';
    }
    switch($dest)
    {
        case 'I':
            //Send to standard output
            if(ob_get_length())
                $this->Error('Some data has already been output. Can\'t send PDF file');
            if(php_sapi_name()!='cli')
            {
                //We send to a browser
                header('Content-Type: application/pdf');
                if(headers_sent())
                    $this->Error('Some data has already been output. Can\'t send PDF file');
                header('Content-Length: '.strlen($this->buffer));
                header('Content-Disposition: inline; filename="'.$name.'"');
                header('Cache-Control: private, max-age=0, must-revalidate');
                header('Pragma: public');
                ini_set('zlib.output_compression','0');
            }
            echo $this->buffer;
            break;
        case 'D':
            //Download file
            if(ob_get_length())
                $this->Error('Some data has already been output. Can\'t send PDF file');
            header('Content-Type: application/x-download');
            if(headers_sent())
                $this->Error('Some data has already been output. Can\'t send PDF file');
            header('Content-Length: '.strlen($this->buffer));
            header('Content-Disposition: attachment; filename="'.$name.'"');
            header('Cache-Control: private, max-age=0, must-revalidate');
            header('Pragma: public');
            ini_set('zlib.output_compression','0');
            echo $this->buffer;
            break;
        case 'F':
            //Save to local file
            $f=fopen($name,'wb');
            if(!$f)
                $this->Error('Unable to create output file: '.$name);
            fwrite($f,$this->buffer,strlen($this->buffer));
            fclose($f);
            break;
        case 'S':
            //Return as a string
            return $this->buffer;
        default:
            $this->Error('Incorrect output destination: '.$dest);
    }
    return '';
}

Upvotes: 0

Mihai Iorga
Mihai Iorga

Reputation: 39724

the PDF embedded is based on the browser capabilities. You can use frames and show the instructions on the upper or left side of the page and the pdf on the rest.

and as you said you can use Google docs, but it transforms the whole document into images for embedding.

Upvotes: 0

Related Questions