Zend framework 3 - DOMPDF - php

I'm trying to use DOMPDF module with ZF 3.
I've installed it with composer composer require dino/dompdf-module and looks ok.
Then i've imported the dompdf module to my order controller
use DOMPDFModule\View\Model\PdfModel;
After that, i've created a new action called printOrderPdfAction() and a new view, here's the following code:
Controller:
public function printOrderPdfAction()
{
$this->layout(FALSE);
$pdf = new PdfModel();
$pdf->setOption('filename', 'order-pdf');
$pdf->setOption('paperSize', 'a4');
$pdf->setOption('paperOrientation', 'portrait');
$pdf->setVariables([
'message' => 'This is a test message'
]);
return $pdf;
}
View:
<html>
<body>
<h2><?= $this->message?></h2>
</body>
</html>
This should download automatically the pdf, however it's returning the view and it's not downloading the pdf file.
Like this:
Anyone can help me to solve this?

You may be experiencing an issue where some browsers need quotes around the file name in the Content-Disposition header, otherwise the browser ignores the header and the file is not downloaded.
Example:
Content-Disposition: attachment; filename=report.pdf
Should really be:
Content-Disposition: attachment; filename="report.pdf"
In DompdfModule the quotes were in fact missing. This has since been fixed in https://github.com/raykolbe/DOMPDFModule/pull/67 and is slated to be released in v0.5.0.
You can try dev-master or the specific commit 7f11506a22ebabb3aac5e800eada1d8e85e22916 to see if this solves the problem you're experiencing.

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

laravel print preview using laravel-dompdf

I am using Laravel 5.4 and using
"barryvdh/laravel-dompdf": "^0.8.0",
https://github.com/barryvdh/laravel-dompdf
i have follwoing code in controller
$pdf = PDF::loadView('print.print', $data);
return $pdf->download('invoice.pdf');
As it will download pdf properly but i am looking to preview the content in the view to print.
i have googled lot still not able to get it
You can use stream() function as stated in documentation here.
According to documentation of dompdf,
When you use stream like this:
$dompdf->stream('filename.pdf');
In default, you will see a download dialouge box.
But, you can add parameter Attachment value to false so that you can view pdf in browser.
Here is what you need to do:
$dompdf->stream("filename.pdf", array("Attachment" => false));
This might help you.
I've struggled with this and manage to make it work with the following snippet:
$html = view('desktop.bill', $data)->render();
return #\PDF::loadHTML($html, 'utf-8')->stream(); // to debug + add the follosing headers in controller ( TOP LEVEL )
Besides the upper command, you will need to add on a TOP LEVEL, like first lines in the controller, the following hearders:
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="document.pdf"');
header('Content-Transfer-Encoding: binary');

HOW TO: convert HTML to PDF with PHP/DOMpdf?

I need your help to fix this:
My current code is this:
<?php
require_once '../dompdf/autoload.inc.php';
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$html = 'Insert full HTML content';
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();
$dompdf->stream("codexworld",array("Attachment"=>0));
?>
This is the error a get:
Why if I have exactly the same code like the basic example, am I getting this error? What am I missing? I don't find in my folder the Autoloader.php, where do I have to get that file?
Well apparently it's an issue after domPdf has moved to Github. It seems that php-font-lib library doesn't exist. So one solution is to manually download it:
Go to https://github.com/PhenX/php-font-lib and download the library.
In the zip file, take the contents of the src/FontLib/ folder and paste that into the folder lib/php-font-lib.
or you can check this answer here

Generate PDF from HTML/CSS/PHP in Symfony 1.4

I am working on a Symfony 1.4 project. I need to make a PDF download link for a (yet to be) generated voucher and I have to say, I am a bit confused. I already have the HTML/CSS for the voucher, I created the download button in the right view, but I don't know where to go from there.
Use Mpdf to create the pdf file
http://www.mpdf1.com/
+1 with wkhtmltopdf
I'd even recommand the snappy library.
If you use composer, you can even get the wkhtmltopdf binaries automatically
Having used wkhtmltopdf for a while I've moved off it as 1) it has some serious bugs and 2) ongoing development has slowed down. I moved over to PhantomJS which is proving to be much better in terms of functionality and effectiveness.
Once you've got something like wkhtmltopdf or PhantomJS on your machine you need to generate the HTML page and pass that along to it. I'll give you an example assuming you use PhantomJS.
Initially set what every request parameters you need to for the template.
$this->getRequest->setParamater([some parameter],[some value]);
Then call the function getPresentation() to generate the HTML from a template. This will return the resulting HTML for a specific module and action.
$html = sfContext::getInstance()->getController()->getPresentation([module],[action]);
You'll need to replace the relative CSS paths with a absolute CSS path in the HTML file. For example by running preg_replace.
$html_replaced = preg_replace('/"\/css/','"'.sfConfig('sf_web_dir').'/css',$html);
Now write the HTML page to file and convert to a PDF.
$fp = fopen('export.html','w+');
fwrite($fp,$html_replaced);
fclose($fp)
exec('/path/to/phantomjs/bin/phantomjs /path/to/phantomjs/examples/rasterize.js /path/to/export.html /path/to/export.pdf "A3");
Now send the PDF to the user:
$this->getResponse()->clearHttpHeaders();
$this->getResponse()->setHttpHeader('Content-Description','File Transfer');
$this->getResponse()->setHttpHeader('Cache-Control','public, must-revalidate, max-age=0');
$this->getResponse()->setHttpHeader('Pragma: public',true);
$this->getResponse()->setHttpHeader('Content-Transfer-Encoding','binary');
$this->getResponse()->setHttpHeader('Content-length',filesize('/path/to/export.pdf'));
$this->getResponse()->setContentType('application/pdf');
$this->getResponse()->setHttpHeader('Content-Disposition','attachment; filename=export.pdf');
$this->getResponse()->setContent(readfile('/path/to/export.pdf'));
$this->getResponse()->sendContent();
You do need to set the headers otherwise the browser does odd things. The filename for the generated HTML file and export should be unique to avoid the situation of two people generating PDF vouchers at the same time clashing. You can use something like sha1(time()) to add a randomised hash to a standard name e.g. 'export_'.sha1(time());
Use wkhtmltopdf, if possible. It is by far the best html2pdf converter a php coder can use.
And then do something like this (not tested, but should be pretty close):
public function executeGeneratePdf(sfWebRequest $request)
{
$this->getContext()->getResponse()->clearHttpHeaders();
$html = '*your html content*';
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED, 'whatever_name.pdf');
throw new sfStopException();
}

Zend_pdf document throws error in magento due to presence of html contents

In magento1.7, just write following lines in app\code\core\Mage\Wishlist\Helper\Data.php (Data.php) file
public function getpdf()
{
$pdf = new Zend_Pdf();
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
for($i=0; $i<5; $i++) {
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$page->setFont($font, 24) ->drawText('Hello World page '.$i, 72, 720);
$pdf->pages[] = $page;
}
$pdfString = $pdf->render();
header("Content-Disposition: attachment; filename=myfile.pdf");
header("Content-type: application/x-pdf");
echo $pdfString;
}
Call this function
<?php Mage::helper('wishlist')->getpdf();?>
in app\design\frontend\base\default\template\wishlist\view.phtml (view.phtml)file just before <div class="my-wishlist"> line. Now run magento in browser and login. Then click on My wishlist link and it will show myfile.pdf in downlods folder. When it is opened it throws error message.Now replace these lines
$pdfString = $pdf->render();
header("Content-Disposition: attachment; filename=myfile.pdf");
header("Content-type: application/x-pdf");
echo $pdfString;
with
$pdf->save("mydoc.pdf");
Then open mydoc.pdf in magento1.7 folder, it's opened with no error and displays "Hello World" on each pages of Pdf document. Now compare these two files "myfile.pdf" and "mydoc.pdf" in text editor, you will see presence of html contents in "myfile.pdf".
My problem is that I am not able to find a way for removing these html contents from generated PDF document. Can anybody help me in resolving this issue.
Please note that above code is just a demostration of what I did.
There is more than one way to skin a cat. I take it you want a nice PDF of the wishist. Consider using wkhtml2pdf to make it. This will involve styling up the PRINT version of the page - there is a CSS that gets added for that if media=print, which is the case with wkhtml2pdf.
The reslts of wkhtml2pdf are vastly better than anything 'hand drawn' with Zend PDF stuff.
To get started install wkhtml2pdf and simply run it on the command line, tinkering with the options until it comes out half way decent.
The only downside to wkhtml2pdf is you have no control on the page breaks.
Add this code at the end of your Mage_Wishlist_IndexController::indexAction():
$sResponse = $this->getResponse()->getBody();
$sNewResponse = strstr($sResponse, '%PDF-1.4');
$this->getResponse()
->clearBody()
->appendBody($sNewResponse);
right behind the $this->renderLayout() call.
In order to send a PDF string from Magento and have it rendered on the browser you need to do the following:
$pdfString = $pdf->render();
$this->getResponse()->setHeader('Content-Type', 'application/pdf');
$this->getResponse()->setBody($pdfString);

Categories