ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

PDF and HTTP Headers?

May 05, 2011 5:57am

Subscribe [3]
  • #1 / May 05, 2011 5:57am

    crossfade

    31 posts

    Hi,

    I’ve been playing around with creating dynamic PDF files using fpdf with EE.

    The sample code below will work if it is a stand-alone php file, but when it’s an expression engine template, it will just send the raw PDF source to the web browser.

    The following code:

    <?php
    require('fpdf16/fpdf.php');
    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',72);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    ?>

    Creates:

    %PDF-1.3 3 0 obj <> endobj 4 0 obj <> stream x�3R��2�35W(�r Q�w3T07�30PISp   �Z*���뙛+��(hx����+���(j*�d���\ endstream endobj 1 0 obj <> endobj 5 0 obj <> endobj 2 0 obj << /ProcSet [/PDF /Text /ImageB /ImageC /ImageI] /Font << /F1 5 0 R >> /XObject << >> >> endobj 6 0 obj << /Producer (FPDF 1.6) /CreationDate (D:20110505184026) >> endobj 7 0 obj << /Type /Catalog /Pages 1 0 R /OpenAction [3 0 R /FitH null] /PageLayout /OneColumn >> endobj xref 0 8 0000000000 65535 f 0000000230 00000 n 0000000418 00000 n 0000000009 00000 n 0000000087 00000 n 0000000317 00000 n 0000000522 00000 n 0000000597 00000 n trailer << /Size 8 /Root 7 0 R /Info 6 0 R >> startxref 700 %%EOF

    If I save that page and then change the extension to pdf and open it, it works as a PDF.

    Anyone with any ideas of how to create the pdf so it opens in the browser as a pdf?

    I’m guessing that it might have something to do with altering the HTTP headers?

    Thanks 😊

  • #2 / May 05, 2011 11:28am

    Brett Gullan

    70 posts

    Hi Crossfade,

    Try something like this in your template (before any other output):

    <?php
    header('Content-type: application/pdf');
    ?>

    Cheers,

    Brett

  • #3 / May 09, 2011 9:09pm

    crossfade

    31 posts

    Thanks for the suggestion.  Unfortunately it didn’t work.  If I find a solution I’ll post it back here in case anybody else is looking to do the same thing.

    I think what I need the server to do is to see the page as a PHP script, rather than a HTML page containing a PHP script.  That way it will run the PHP script, which in turn produces the PDF file.

    In other words, when it is just a file on the server, the HTTP headers are sent as output from the PHP script.

    When it is an expression engine template, the HTTP headers are sent for HTML, which is why the script output is written as plain text within that HTML file.

    I have toggled the HTTP headers option in EE both ways and it doesn’t make a difference.

    The other difficulty will then be getting it to parse EE tags within the template at the right time.

    Fun 😉

  • #4 / May 10, 2011 2:59am

    lincolnpixel

    83 posts

    Crossfade,
              have you tried the TCPDF Plugin? You can get it directly on devot-ee ..http://devot-ee.com/add-ons/tcpdf/.

    ciao

  • #5 / May 11, 2011 8:47pm

    crossfade

    31 posts

    Thanks.  I’ll have a look at that and report back on how it goes 😊

  • #6 / Aug 01, 2013 8:38am

    Dennis Bruijn's avatar

    Dennis Bruijn

    1 posts

    Dear crossfade,
    You need to add an exit at the end of your code.

    <?php
    require('fpdf16/fpdf.php');
    $pdf=new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',72);
    $pdf->Cell(40,10,'Hello World!');
    $pdf->Output();
    exit;
    ?>
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases