简体   繁体   中英

Cannot read pdf generated through dompdf in chrome

I'm just testing dompdf. And found out that what I'm generating can't be red by chrome. Tried to open it up with sumatra pdf reader, and it opened. Tried opening some random pdf file from my ebooks in chrome and it red it. Is there something wrong with my code that chrome wasn't able to read it:

<?php
require_once("dompdf/dompdf_config.inc.php");

$dompdf = new DOMPDF();

$d_r = array(array('name'=>'ab', 'course'=>'bsit'), array('name'=>'yoh', 'course'=>'bscs'));

ob_start();
put_table($d_r);
file_put_contents('sample.html', ob_get_contents());
generate_pdf('sample.html');

function put_table($raw_data){
?>
<table border="1">
    <tr>
    <th>Name</th>
    <th>Course</th>
    </tr>

<?php foreach($raw_data as $data){ ?>   
    <tr>
    <td><?php echo $data['name']; ?></td>
    <td><?php echo $data['course']; ?></td>
    </tr>
<?php } ?>
</table>


    <?php
}


function generate_pdf($filename){
    global $dompdf;

    $dompdf->load_html(file_get_contents($filename));
    $dompdf->render();
    $dompdf->stream($filename. ".pdf");

}
?>

The problem could be with the fact that by default dompdf dumps the contents as an attachment, forcing a download. In order to allow the browser to view it inline, it has to be told not to, by setting an additional parameter to the stream method.

$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));

If that doesn't make sense, try cleaning up the output buffer after generating (and writing on disk) the desired html output, without putting it onscreen. Do a header debug on the request/response and see if you have any blanks or spaces in it. If you do, the headers will not be read by the client and you may not be able to see the resulting PDF.

The PDF may have been rendered correctly but, depending on your server configuration, some non-PDF content may have been included in the PDF source. Some PDF readers are more lenient than others and will discard the non-PDF content. The easiest way to check for this is to open your PDF in a text editor and look for things like strings of HTML text.

If that's not the problem, it would help to see the PDF itself.

The Chrome pdf viewer is terrible leave it to the experts. Install Acrobat Reader if you do not have it. In Chrome address bar type about:plugins and click to disable Chrome pdf viewer. Then click to enable Acrobat Reader---pdf viewing in Chrome will work!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM