how to open print window after creating pdf in dompdf? - php

i converted my html to Pdf using dompdf and i want to print it. My problem is how to open my pdf file in print window instead of opening the download dialog?
include('dompdf/dompdf_config.inc.php');
$savein = 'pdfdirectory/';
$html = " my htmlcode here "
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$pdf = $dompdf->output();
file_put_contents(($savein.'file.pdf'), $pdf);
$dompdf->stream('file.pdf');

You can't force the user to open the file in their browser, but you can specify options for the stream() method that hint to the browser whether or not to download the file. The default is to tell the browser that the file is an "attachment" which usually translates to a download. But if you modify the last line of your code as follows most browsers should display the PDF in the browser:
$dompdf->stream( 'file.pdf' , array( 'Attachment'=>0 ) );

Related

How to save generated pdf in server using dompdf php lib

I am using dompdf php lib for generating pdf.
Right now it ask user to save the file in their machine.
Code i tried so far:
<?php
$dompdf = new Dompdf();
$dompdf->loadHtml($html);
$dompdf->render();
$dompdf->stream($filename.'.pdf');
$output = $dompdf->output();
file_put_contents($filename.'.pdf', $output);
?>
Give "write" permission to the directory you need to put the file.

dompdf stream pdf in browser using localhost, but not in network ip

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..
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.
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
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

dompdf generates a pdf empty / corrupted

I have a problem with dompdf. I'm trying to generate a PDF file through the values of a form.
A user enters the website, fills out the fields and clicks submit. In my email I must attach a PDF with all the form values.
I was able to integrate it into my code, but there is something wrong.
To submit, I get the mail with the PDF, but it is damaged. (Of 0BYTE.)
The code I've entered in includes / ajax.php.
This is the code:
// Generate PDF here
require_once("../dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->load_html($message);
$dompdf->render();
$pdf_content = $dompdf->output();
file_put_contents('../dompdf/ordine.pdf',$pdfoutput);
data users write in the form are in $message
this is my ajax.php
Try this
// Generate PDF here
require_once("../dompdf/dompdf_config.inc.php");
spl_autoload_register('DOMPDF_autoload');
$dompdf = new DOMPDF();
$dompdf->load_html($message);
$dompdf->render();
$pdf_content = $dompdf->output();
file_put_contents('../dompdf/ordine.pdf',$pdf_content); //there was a typo here...
You pdf was correctly 0 bytes because you write an undefined variable to it.

generate the pdf on newtab in dompdf

Is there any way to open the created pdf by dompdf to new browser tab ?
I tried these.
When I click the generate button (now it is a submit button) the controllers action
is given below
Controller:
function generatePdf()
{
require_once("dompdf/dompdf_config.inc.php");
$data="this is a sample pdf";
$dompdf = new DOMPDF();
$html = $this->load->view('report/modelpdfview', $data, true);
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("mypdffile.pdf",array('Attachment'=>0));
$this->load->view('view/mysiteView', $data);
}
But it open on the same location which leads the user will lose control from the site. (I know there is back button in browser)
Create a PHP script that creates the PDF content you want to display. If appropriate use some command line parameters to control what's created, or you can store your content in a session variable. Let's call it makemypdf.php
From Javascript, open up a pop-up window with that as a url:
window.open(makemypdf.php, "mypdfwindow","...other window specs");
The new window will contain your PDF file, with your existing page still open.
Your question is a bit light on content, so you'll have to fill in some of the mechanics yourself.
In your main page:
session_start();
$_SESSION['pdfcontent'][0] = "First PDF content";
$_SESSION['pdfcontent'][1] = "Second PDF content";
Your window opener becomes:
window.open("makemypdf.php?pdfcontent=1", "mypdfwindow","...other window specs");
And in makemypdf.php
session_start();
$pdfcontent = $_SESSION['pdfcontent'][$_GET['pdfcontent'];
// render your PDF document here

Code does not execute after creating a PDF file with DOMPDF

After creating a PDF file in PHP Using DOMPDF, I need to redirect user to different page.
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($fileName . '.pdf', array("Attachment" => 0));
header('location:newpage.php');
This code creates the PDF file but does not redirect. How can I fix this problem?
Note : This is my previous question. Still I'm trying to get this fixed, still no luck.
The $dompdf->stream() call starts outputting data to the client. Once content is being sent, it is impossible to modify the headers, and thus perform the redirection you want. You can see this by turning on the warnings in PHP (error_reporting).
A way to do this is to open a new window, but it requires Javascript.
function downloadPDF(){
window.open("get-pdf.php?id=12345");
location.href = "newpage.php";
}

Categories