Changing the default filename when using mPDF - php

I'm currently using mPDF to generate a pdf from HTML (which was generated by PHP).
All works as expected but I'd like to be able to change the default filename. Currently, I have:
$payStub=new mPDF();
$payStub->SetTitle('My title');
$payStub->WriteHTML($pcTableRows);
$payStub->Output();
When I save the pdf that opened in my browser it defaults to mpdf.pdf.
Is it possible to change mpdf.pdf to something of my choosing?
I tried
$payStub->Output('myFileName.pdf');
and
$payStub->Output('myFileName.pdf', 'F');
but those want to save it to the server, I'm trying to have it for when the user saves it locally.

Try the I flag in the Output function, which will output the PDF to the browser, and use the filename from the first argument:
$payStub=new mPDF();
$payStub->SetTitle('My title');
$payStub->WriteHTML($pcTableRows);
$payStub->Output('yourFileName.pdf', 'I');

You can try as:
$file_name = 'yourFileName.pdf';
$mpdf->Output($file_name, 'D');
Help:
'D': download the PDF file
'I': serves in-line to the browser
'S': returns the PDF document as a string
'F': save as file $file_out

Modify mdpdf.php
form.setAttribute("action", "'._MPDF_URI.'includes/out.php/'.$name.'");
for downloading with other name

Related

Saving PDF file as "data" - using PHP MPDF

IM trying to save a PDF file, generated from HTML, into the user's local using MPDF. Here is the part of the code responsable for this:
$mpdf = new \Mpdf\Mpdf();
$html = $this->load->view('gestao/relatorios/relatorio_cargo_cidades', $data, true);
$mpdf->WriteHTML($html);
$mpdf->SetFooter(relatorio_footer());
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
At first I was trying to show the PDF using the "I" param from the output function, than the user could just see the PDF and choose to download it or not. But when I tried to submit a file into another website, it says that the file is not a PDF. Than I used my linux to see if the file was actually a PDF, here's what I got:
As you can see, the file is being saved as "data" for some reason. I've already tried to use the 'F' param, also from the output function, and than it worked, I saved as PDF. But the F param only save the file inside the code folder, so it was not very usefull for me.
Can anyone tell me how could I save the file as an actually PDF using MPDF?
EDIT
I think that the problem is being caused by CODEIGNITER, not by the MPDF. When I'm setting the last param from the load->view as TRUE, the returned HTML is in a data (string) form, and MPDF is not converting this properly.
Adding ob_clean() might solve your problem:
ob_clean();
$mpdf->Output('relatorio_cargos_cidades_seletivo_' . $seletivo_id .'_'.date("Ymd_his").'.pdf', 'D');
You can simply use the D parameter for downloading:
$mpdf -> Output ('FILENAME.pdf', 'D');
or generate your own header and then output the content as string:
header ("Content-type: application/pdf");
header ("Content-Disposition: inline; filename=FILENAME.pdf");
$mpdf -> Output ('', 'S');

Open generated pdf in browser in another tab

I'm generating pdf through FPDF. Now all I need is to open this generated pdf in browser.
Searched lot for it, but all am getting is solution for existing pdf where as here we need solution for generated pdf through fpdf.
Following is my code:
<?php require('../pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,10,'This is demo');
$pdf->Output();
?>
A quick browse of the FPDF documentation shows that you can add a couple of parameters to the Output() function call to provide display in browser or download functionality
string Output([string dest [, string name [, boolean isUTF8]]])
See more here.
For example:
<?php require('../pdf/fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',12);
$pdf->Cell(40,10,'This is demo');
$pdf->Output('I');
?>
The above example uses 'I' for inline. The other options are:
I: send the file inline to the browser. The PDF viewer is used if available.
D: send to the browser and force a file download with the name given by name.
F: save to a local file with the name given by name (may include a path).
S: return the document as a string.
It is all available in the documentation.
For people still looking, adding target="_blank" to your form tag will open the PDF in a new window.
HTML:
<form method="GET" action="/target-destination/" id="pdfForm" target="_blank">
<!-- Content goes here -->
</form>
mPDF:
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Hello World');
$mpdf->Output('filename.pdf', 'I');
The FPDF inline method demonstration will attempt to insecurely open an About:Blank which should not be relied on as a method for Inline Display, it simply triggers a security response in the browser to auto download to AV check in sandbox. Use one of the traditional methods like A Href=download I will trust that more and can run a URL checker on your link first...

How to get the output of a php file into a variable using mPDF

I'm using mPDF library to provide pdf files.
Assume that I have a file named facture.php that contains :
an image in the header
a table in the body
an image in the footer of the file
And another file named convert_HTML2PDF.php that will provide the pdf output of the first file.
I would like to get the output of the facture file that is interpreted into a variable and output it as a pdf file without visualising in the browser.
How can I do that and use the output inside the convert_HTML2PDF.php?
$pdf = $mpdf->Output('', \Mpdf\Output\Destination::STRING_RETURN)
will output the PDF into a string - no need to create temp file.
Use file_get_contents() to get html content of your facture.php :
$output = file_get_contents('http://www.example.com/facture.php');
$mpdf->Output('filename.pdf', 'F');
will send the output to a file on your server instead of a browser.
See:
http://mpdf1.com/manual/index.php?tid=125
For more output variations.

FPDF - generated PDF is not displayed properly in browser

I am generating a PDF document with a FPDF library.
I am trying to display a generated document in browser (Firefox, Chrome, Opera) but all i get are strange characters starting with:
%PDF-1.3 3 0 obj <>...
This is my code. I am trying to add two images to the document.
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->Image('./image.png',10,10,0,0,'png');
$pdf->Image('./image.png',10,120,0,0,'png');
$pdf->Output('file.pdf','I');
If I use the output with the 'D' option:
$pdf->Output('file.pdf','D');
and save the file on the disc, the pdf document is fine.
You probably need to add the correct content type header. When the web server serves up a file, it tries to give the correct content type header it guesses from the file, but here, as you're generating it with PHP, the default header will be sent, which is usually text/html. Try adding this to the start of your code:
header('Content-type: application/pdf');
And it should send with the correct content type header.
I run into the same problem and I add ob_get_clean(); before $pdf->OutPut() and I got the correct result.
It was because the header was not correct, but even I set the header before outputting the pdf, it still not correct.
change D to I
$pdf->Output('file.pdf','I');
send the file inline to the browser. The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
from there documentation
fpdf documentaion
Add exit as follows :
$pdf->Output();
exit;
Reference
Try:
ob_start (); //used before output.
$pdf->Output();
?>
I don't know if i should put it as a comment or answer, i had same issue on my localhost, but once i placed my code on live server the issue resolved automatically. Try cleaning all cookies on your local system and it may help too.
I had applied all the above options with different sequences and this is how finally it works. Thanks
$str = iconv('UTF-8', 'windows-1252', $custom_data);
$final = html_entity_decode($str ,ENT_QUOTES, 'windows-1252');
$pdf = new FPDF();
header('Content-type: application/pdf');
$pdf->AddPage();
$pdf->SetFont('Courier','',16);
$pdf->MultiCell(190, '10', $final, '', 'L');
ob_get_clean();
$pdf->Output($afterDash.".pdf", "I");
exit;

TCPDF output without saving file

How to use TCPDF to output pdf file in browser without saving like in ezpdf?
Use I for "inline" to send the PDF to the browser, opposed to F to save it as a file.
$pdf->Output('name.pdf', 'I');
This is what I found out in the documentation.
I : send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
D : send to the browser and force a file download with the name given by name.
F : save to a local server file with the name given by name.
S : return the document as a string (name is ignored).
FI : equivalent to F + I option
FD : equivalent to F + D option
E : return the document as base64 mime multi-part email attachment (RFC 2045)
If You want to open dialogue window in browser to save, not open with PDF browser viewer (I was looking for this solution for a while), You should use 'D':
$pdf->Output('name.pdf', 'D');
Hint - with a saving file:
$pdf->Output('sandbox/pdf/example.pdf', 'F');
Print the PDF header (using header() function) like:
header("Content-type: application/pdf");
and then just echo the content of the PDF file you created (instead of writing it to disk).
I've been using the Output("doc.pdf", "I"); and it doesn't work, I'm always asked for saving the file.
I took a look in documentation and found that
I send the file inline to the browser (default). The plug-in is used if available. The name given by name is used when one selects the "Save as" option on the link generating the PDF.
http://www.tcpdf.org/doc/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
Then I think you have to use a plugin to print it, otherwise it is going to be downloaded.
It works with I for inline as stated, but also with O.
$pdf->Output('name.pdf', 'O');
It is perhaps easier to remember (O for Open).
$filename= time()."pdf";
//$filelocation = "C://xampp/htdocs/Nilesh/Projects/mkGroup/admin/PDF";
$filelocation = "/pdf uplaod path/";
$fileNL = $filelocation."/".$filename;
$pdf->Output($fileNL,'F');
$pdf->Output($filename, 'S');

Categories