Open generated pdf in browser in another tab - php

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

Related

cache page before create fpdf

I need to show a new page after the creation of a pdf whit fpdf. My idea is create the page before pdf, cache it in browser, create pdf and than flush the cache and show page but I don't know how to do.
<?php $txt = "hallo";
//My page to be cached
header("..........");
// my pdf
require('fpdf.php');
$pdf = new FPDF();
$pdf->AddPage();
$pdf->Cell(40,10,"$txt");
$pdf->Output();
// after pdf I want to show page in the cache
?>
Many thanks
$pdf->Output("Filename.pdf","D");
The second parameter D in double quotes. This will download the file into the user's computer instead of saving it in your server.
I think this is the better way for creating the pdf. :)
http://www.fpdf.org

Changing the default filename when using mPDF

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

Moodle download a created PDF

I create a PDF in Moodle using the following code
$pdf = new pdf;
$pdf->AddPage();
$pdf->Write(1, "Test");
$pdf->Output();
How would I make this download in the browser instead of opening in browser?
// Force the browser to download the output
$pdf->Output('filename.pdf','D');
Moodle wraps the TCPDF library for PDF generation (the wrapper mostly just handles the locations for temporary files and accessing embedded images which are in the Moodle Files API).
You can find documentation about the TCPDF Output() function online at http://www.tcpdf.org/doc/code/classTCPDF.html#a3d6dcb62298ec9d42e9125ee2f5b23a1
The important param is the second one, calling $pdf->Output('filename.pdf', 'D') will cause it to download.

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;

Export a html page to pdf with everything that's written on it, after submit button

I need to export html page to pdf file with everything that's written in it, after I press submit button. It will open new page, with info, and I need for script to automatically make .pdf file (already uploaded to webserver), and get the link from file. Could you give me some easy example (if available, without any plugins, or other features that I must download, I would prefer clean PHP).
Just try this
HTML to PDF with PHP
Using open-source HTML2FPDF project
This solution uses HTML2PDF project (sourceforge.net/projects/html2fpdf/). It simply gets a HTML text and generates a PDF file. This project is based upon FPDF script (www.fpdf.org), which is pure PHP, not using the PDFlib or other third party library. Download [HTML2PDF][1], add it to your projects and you can start coding.
Code example
require("html2fpdf.php");
$htmlFile = "your link";
$buffer = file_get_contents($htmlFile);
$pdf = new HTML2FPDF('P', 'mm', 'Letter');
$pdf->AddPage();
$pdf->WriteHTML($buffer);
$pdf->Output('test.pdf', 'F');

Categories