can create pdf with file_put_contents but not using stream

2,357 views
Skip to first unread message

Mike Heavers

unread,
Jan 8, 2014, 5:02:48 PM1/8/14
to dom...@googlegroups.com
Hi - I'm using DOMPDF to successfully create PDFs on a local server using its

    file_put_contents("filename.pdf", $output);

function. However, if I attempt to use the stream function, in order to bring up a save or print dialog, no pdf is created, and no dialog occurs.

This is my code (triggered from code igniter):

    <?php
    class APP_benefits extends CI_Controller {
    
        public function pdf()
        {
    
            require_once "application/libraries/dompdf/dompdf_config.inc.php";
    
            
            $html = 'html';
            $dompdf = new DOMPDF();
            $dompdf->load_html($html);
            $dompdf->render();
            $output = $dompdf->output();
            //file_put_contents("file.pdf", $output); //works
            $dompdf->stream('file.pdf'); //does not work
    
        }
    }
    ?>

and this is the code that gets echoed back to javascript (I'm using an ajax form to submit the content of the pdf, not illustrated above).

    %PDF-1.3
    1 0 obj
    << /Type /Catalog
    /Outlines 2 0 R
    /Pages 3 0 R >>
    endobj
    2 0 obj
    << /Type /Outlines /Count 0 >>
    endobj
    3 0 obj
    << /Type /Pages
    /Kids [6 0 R
    ]
    /Count 1
    /Resources <<
    /ProcSet 4 0 R
    /Font << 
    /F1 8 0 R
    >>
    >>
    /MediaBox [0.000 0.000 612.000 792.000]
     >>
    endobj
    4 0 obj
    [/PDF /Text ]
    endobj
    5 0 obj
    <<
    /Creator (DOMPDF)
    /CreationDate (D:20140108225645+01'00')
    /ModDate (D:20140108225645+01'00')
    >>
    endobj
    6 0 obj
    << /Type /Page
    /Parent 3 0 R
    /Contents 7 0 R
    >>
    endobj
    7 0 obj
    <<
    /Length 70 >>
    stream
    
    0.000 0.000 0.000 rg
    BT 34.016 734.579 Td /F1 12.0 Tf  [(html)] TJ ET
    endstream
    endobj
    8 0 obj
    << /Type /Font
    /Subtype /Type1
    /Name /F1
    /BaseFont /Times-Roman
    /Encoding /WinAnsiEncoding
    >>
    endobj
    xref
    0 9
    0000000000 65535 f 
    0000000008 00000 n 
    0000000073 00000 n 
    0000000119 00000 n 
    0000000273 00000 n 
    0000000302 00000 n 
    0000000416 00000 n 
    0000000479 00000 n 
    0000000599 00000 n 
    trailer
    <<
    /Size 9
    /Root 1 0 R
    /Info 5 0 R
    >>
    startxref
    708
    %%EOF

I'm running this on localhost, through MAMP. Do I need to do something in htaccess? Am I missing some sort of headers? Thanks for any insight.

BrianS

unread,
Jan 8, 2014, 6:45:45 PM1/8/14
to dom...@googlegroups.com
There's not really an easy way to retrieve a binary object via AJAX and do something with it on the page. The browser doesn't get the PDF, your script's callback function does. You then have to do something with the data before the browser can know anything about it.

For simplicity what most people do is save the PDF as you have done then redirect the user to that file. Or just direct the browser to the page that generates the PDF. e.g. http://stackoverflow.com/a/14559186/264628

If you really want to do something more complex you should capture the PDF output, encode as a data-uri, and send that to the browser. Then on the client you can create an iframe with the src set to the data-uri. e.g. http://stackoverflow.com/a/14153251/264628
Reply all
Reply to author
Forward
0 new messages