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');
Related
I have a piece of code which polls a CRM system and finds a specific document via a URL..... from the document which is found, this is then added to a PHP variable - however if I then print the PHP variable it's just garbage. How can I take the PHP variable and out the contents to PDF (it is a PDF already stored in the variable).... so it can be either downloaded or opened to screen?
Here is the code being used:
**$url = "accounting/invoices/55120.pdf?template=30&api_key='My-API-Key'";
$pdf = $thisbooks->get($url, array(), array('decode_json' => false));
// Ensure the response looks like a PDF
if (!preg_match('/^\%PDF\-1\.4\s/', $pdf)) {
$thisbooks->log('ERROR: Unexpected response: is it a PDF?', $pdf, 'error');
}
$thisbooks->log('Fetched PDF', $pdf);
print $pdf;**
Now when I print the $pdf variable I can see the script code, but it doesn't do anything else.....
Thanks in advance
Damian
did you try to use file_get_contents to read your variable?
or use this header: header('Content-type: application/pdf');
If you send your code maybe we can help more.
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
I am using an API which provides me base64 encoded content. It is mentioned that we can download the PDF file by saving the content to a .pdf file.
In the API documentation, it's clearly mentioned
A PDF that contains the contents of the invoice. You encode the
content with a Base64 string to create a Base64-encoded .pdf file.
You can query this field to download the invoice PDF for a specific
invoice. After you query it, you can save the string to a .pdf file,
then view the file in any PDF reader.
I am using TCPDF to write the content in PDF file. But it generates a blank PDF file.
$PDF = new PDF('Test Title', 'Test Subject');
$PDF->GetTCPDF()->write(0, 0, '', '',
base64_decode($this->Get(self::Body)), 0, 1, 0, true, '', true);
$PDF->Download();
Am I doing anything wrong?
I think you should use this code to get your pdf file :
$data = base64_decode($this->Get(self::Body));
file_put_contents('mypdf.pdf',$data);
Then you can open it.
Or you can echo the content to your page like this :
$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;
Good luck
The last time I get a blank PDF I forgot adding a new Page with AddPage().
Apart from this I think M2sh is right.
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;
i want to make a pdf file with the help of php code if suppose i make a register form after click the submit button that form information must come to pdf file. is there any way to do. i got a one link but that PDF is not working properly
i have been searching but still i can't get the demo.
you guys can any one help me.
thanks
Try FPDF library. I am posting part of code I used for generating pdf dynamically in php.
require_once('fpdf/fpdf.php');
$frontpdf = new FPDF('P', 'mm', 'A4');
$frontpdf->AddPage();
$frontpdf->SetFont('Arial','',12);
$frontpdf->Cell(210, 230.19, $frontpdf->Image('./images/cover.jpg', 0, 0, 205, 230.19));
$frontpdf->Ln(218);
$frontpdf->Cell(110,6,$fname.' '.$lname,0);
$frontpdf->Ln();
$frontpdf->Cell(110,6,date('d.m.Y'),0);
$frontpdf->Output('./pdfs/your_data.pdf', 'F');
For details see http://www.fpdf.org/
Use 'I' or 'D' instead of 'F' in last line if you want to send pdf file instead of saving it. You can use FPDF in php4 as well.
Copied from here:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Set the action url of your form to a php file like the one above.
You will need a pdf generator and i recommend you to use tcpdf:
http://www.tcpdf.org/
It is open source and when you download the library you have more than 50 or 60 how to use example scripts.
Unlike other open/source libraries i know that this one actually supports utf-8 (depends on the font used). I tested fpdf, dompdf and few others becouse i needed these characters in my documents: č, ć, ž, š, đ and that is the only library i could use for that.
If you need a specific example show me your code and i'll help you :)