问题
i need to know why dompdf doesn't stream a pdf in browser before download on network ip like 192.168.1.77 , but it works on localhost.
when using network ip, it just immediately download the file , it won't show pdf preview in browser
i'm using dompdf version 0.6.2 this is my code
$dompdf = new DOMPDF();
ob_start();
$this->load->view('report_me',array_merge($this->sesi,$this->session->userdata('printdata')));
// header('Pragma','public');
$html = ob_get_contents();
ob_end_clean();
// $this->session->unset_userdata('printdata');
// $dompdf->set_option('setIsHtml5ParserEnabled', true);
$dompdf->load_html($html);
// (Optional) Setup the paper size and orientation
// $dompdf->setPaper('A4', 'potrait');
// Render the HTML as PDF
$dompdf->render();
$canvas = $dompdf->get_canvas();
$font = Font_Metrics::get_font("helvetica", "bold");
$canvas->page_text(44, 760, "Page {PAGE_NUM} of {PAGE_COUNT}", $font, 8, array(0,0,0));
//ERASE SESSION
// Output the generated PDF to Browser
$dts = date('Y-m-d');
// $dompdf->output();
// $dompdf->stream();
$dompdf->stream('inv_'.$this->Master_model->getInvoiceNumber().'_'.$dts.'.pdf',array('Attachment'=>0));
i had enabling the
ENABLE_REMOTE
to TRUE
Seems like i'm the only one that face the problem, hope someone can help me through this..
回答1:
Encountered a similar problem on Laravel and tried adding headers. Doesn't work.
I added exit;
after stream()
and it started displaying. I am guessing the framework may be doing something after stream()
causing headers to revert to text/html
.
回答2:
You need to set the header at the top:
header("Content-type: application/pdf");
This should display it in the browser.
If you want it to download set the header like this:
header('Content-disposition: attachment; filename='path/to/file/name.pdf');
- this can be set after you have created the pdf and saved it somewhere
- NB: path/to/file/name.pdf is on the server not on your local machine
回答3:
Try to remove some spaces or line breaks between html
head
body
starting and closing tags. Like this <html><head>
</head><body>
</body><html>
.
Also remove thead
tbody
tfoot
tags from your html (sometimes this is not necessary, but in mycase I needed to remove).
And if you are calling the DOM class multiple times. Change the assigned variables (dont let them to be same variable which is in my case was $pdf = new DOMPDF
. In second class call I was using same variable. I changed it to $pdf2
)
It has taken from here; https://github.com/dompdf/dompdf/issues/902#issuecomment-197655763
来源:https://stackoverflow.com/questions/36597866/dompdf-stream-pdf-in-browser-using-localhost-but-not-in-network-ip