pdf download html2pdf - php

i am using html2pdf class to generate pdf. in my problem its generate pdf for the html code but it not give the dialog box option to download that pdf. plz help my cose is following.
<?php
ob_start();
include(dirname(__FILE__).'/res/pdf_demo.php');
$content = ob_get_clean();
// conversion HTML => PDF
require_once(dirname(__FILE__).'/../html2pdf.class.php');
try
{
$html2pdf = new HTML2PDF('P','A4', 'fr', false, 'ISO-8859-15');
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->writeHTML($content, isset($_GET['vuehtml']));
$html2pdf->Output('pdf_demo.pdf');
}
catch(HTML2PDF_exception $e) { echo $e; }
?>

From the documentation, method Output
/**
* Send the document to a given destination: string, local file or browser.
* Dest can be :
* 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
* true => I
* false => S
*

Change this line $html2pdf->Output('pdf_demo.pdf'); to
$html2pdf->Output('pdf_demo.pdf', 'D'); and it will force browser to automatically download the pdf file.

Send PDF to browser with a specific name
$html2pdf->Output('document_name.pdf');
$html2pdf->Output('document_name.pdf', false);
$html2pdf->Output('document_name.pdf', '');
$html2pdf->Output('document_name.pdf', 'I');
Force the browser to download the PDF file with a specific name
$html2pdf->Output('document_name.pdf', 'D');
Write the contents of a PDF file on the server
Attention, this writing on your server must be used with caution. No verification is made on the existence of the file
$html2pdf->Output('directory/filename_xxxx.pdf', 'F');
Retrieve the contents of the PDF and then do whatever you want
$content_PDF = $html2pdf->Output('', true);
$content_PDF = $html2pdf->Output('', 'S');

To offer download from your browser u need add the header for being attachment...
header("Content-Disposition: attachment; filename=sample.pdf");
Add the above code at the start of the page and then proceed with the html2pdf conversion..

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');

PHP add a logo to existing PDF that stored in database (MySQL)

I am doing a basic system that the staff uploads a pdf file with some description and this data stored in database MySQL.
The admin will view this pdf and click on approval if everything is ok.
An image will be inserted in pdf file with approve logo.
I use fpdf and fpdi class to do this, I manage to do this if PDF file stored on the actual path as shown in the code below.
<?php
use setasign\Fpdi\Fpdi;
require_once('fpdf/fpdf.php');
require_once('fpdi2/src/autoload.php');
// initiate FPDI
$pdf = new Fpdi();
// add a page
$pdf->AddPage();
// set the source file
$pdf->setSourceFile('PdfDocument.pdf');
// import page 1
$tplIdx = $pdf->importPage(1);
// use the imported page and place it at position 10,10 with a width of 100 mm
$pdf->useTemplate($tplIdx, 10, 10, 100);
// now write some text above the imported page
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(255, 0, 0);
$pdf->SetXY(30, 30);
$pdf->Write(0, 'This is just a simple text');
$pdf->Output();
BUT when I try to use $pdf->setSourceFile($string) or other that actual file for example (PDF $content from string (database) or URL) I cannot manage to do that.
// set the source file
//$pageCount = $pdf->setSourceFile("http://localhost/pdf/getpicture.php?fid=2");
$stream = fopen('data:text/plain,' . urlencode("http://localhost/pdf/getPDF.php?fid=2"), 'rb');
//$reader = new SetaPDF_Core_Reader_Stream($stream);
$pageCount = $pdf->setSourceFile($stream);
My question is how can I import PDF from MySQL string to be edited by fpdf and fpdi or any other free PDF classes.
Note: I try to use stream_wrapper_register with no luck so far. as in this link
https://www.setasign.com/support/faq/miscellaneous/using-a-pdf-from-a-php-variable-instead-of-a-file/
Please help me with a simple example as I am not really familiar with PDF classes.
Thank you.
I think the source of your trouble is in the line:
$stream = fopen('data:text/plain,' . urlencode("http://localhost/pdf/getPDF.php?fid=2"), 'rb');
PHP's fopen function returns a file pointer, and is not giving you the name of the PDF file you want.
So later when you call
$pageCount = $pdf->setSourceFile($stream);
$stream is not a string with a PDF filename.
If your http://localhost/pdf/getPDF.php?fid=2 URL is returning the filename of the PDF, try getting that value with file_get_contents like so:
$pdf_file = file_get_contents('http://localhost/pdf/getPDF.php?fid=2');
and then call
$pdf->setSourceFile($pdf_file);
You should not use an additional HTTP request to access a file from a database!
FPDI 2 allows you to read from any source through a StreamReader class:
// use a resource
$fh = fopen('a/path/to/a.pdf', 'rb');
$pdf->setSourceFile(new StreamReader($fh));
// same as
$pdf->setSourceFile($fh);
// don't forget to call fclose($fh);
// use a path
$path = 'a/path/to/a.pdf';
$pdf->setSourceFile(StreamReader::createByFile($path));
// same as
$pdf->setSourceFile($path);
// use a string
$pdfString = '%%PDF-1.4...';
$pdf->setSourceFile(StreamReader::createByString($pdfString));
So do not call an external script but query your database for the PDF and pass it e.g. as a string.
PS: You cannot edit a PDF with FPDI!

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

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

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