I want to use dompdf for generation pdf document.
i got pdf response with follwing code by using DOMPDFModule.But my question is how can i pass variable to the phtml file in order to get print on pdf file my code is as follows
use DOMPDFModule\View\Model\PdfModel;
...
..
public function printAction()
{
$campaignsList=$this->getcampaignTable()->getCampaignList();
$model = new PdfModel();
$model->setOption('paperSize', 'a4');
$model->setOption('paperOrientation', 'landscape');
return $model;
}
How can i print that $campaignList array in print.phtml file
Thanks in advance
I'm not 100% sure what you're asking, you can create the PDF entirely within your action without involving the view(presume this is what you meant when reference your phtml file).
Some example code on PDF generation with DOMpdf :
<?php
// Create a new DOMPDF object
$dompdf = new \DOMPDF();
// Set the paper size to A4
$dompdf->set_paper('A4');
// Load the HTML
$dompdf->load_html($html);
// Render the PDF
$dompdf->render();
// Set the PDF Author
$dompdf->add_info('Author', 'I am the Author');
// Set the PDF Title
$dompdf->add_info('Title', 'My PDF Title');
/**
* 'Attachment' => 1 or 0 - if 1, force the browser to open a
* download dialog, on (1) by default
*/
$dompdf->stream($name,array('Attachment'=>1));
?>
And the documented usage - DOMPDF LINK
Related
A client of mine had a problem merging two pdf documents. When I looked into it, and ran the PDF file rendered by DOMPDF in a PDF validator, it outputs:
1.1: Header Syntax error, Second line must begin with '%' followed by at least 4 bytes greater than 127
Looking at a valid pdf, this is line 2: %ÓôÌá
The error does not originate from the html, as it occurs even with the default Hello World! example by DOMPDF:
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('hello world');
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
Edit: Question is whether there is a way to make Dompdf create the missing line 2?
Should be said that the file can be opened and viewed but error arises when merging with another pdf file.
I am currently generating some pdfs with the dompdf library but every time a pdf is being generated my temp folder is being filled with this file and the images associated with it. I am trying to unlink or delete the pdf and any temp files generated with it but the following code is not giving the desired result. Does anyone know what else I can do in this situation?
I am using PHP and have installed the library with composer.
pdf code:
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
//$dompdf->loadHtml('hello world');
$dompdf->set_option('isHtml5ParserEnabled', true);
$dompdf->set_option('isRemoteEnabled', true);
$dompdf -> loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('A4', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
unlink($dompdf);
I have installed tangerine in my dompdf. Now what is happening is when i try on my windows machine it works fine. But in ubuntu when i put the same code in xampp the generated pdf is massed up. I used the tangerine font with only my h3 tags but in the generated pdf the font style is placed in whole body. Here is my dompdf settings
<?php
// Include autoloader
require_once 'dompdf/autoload.inc.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf(array('isPhpEnabled' => true));
// Load content from html file
ob_start();
include_once 'pdfcontent.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
//$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('b4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("funeral",array("Attachment"=>0));
?>
Well the the problem was basically with the cached dompdf file. I have deleted the dompdf folder and uploaded again. Now it works fine.
I am created PDF using DOMPdf Every thing showing properly but image is not
showing.
<?php
$data1.='<p>Peace,</p><image src="http://localhost/manresaorg/im_Steve-Raymond-1.jpg"></p>Steve Raymond
<br>Associate Director';
$data1.='</main></div>';
?>
<?php
require_once 'dompdf/autoload.inc.php';
// reference the Dompdf namespace
use Dompdf\Dompdf;
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml($data1);
// (Optional) Setup the paper size and orientation
//$dompdf->setPaper('A4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
//$dompdf->stream("manresaorg.pdf");
$dompdf->stream("manresaorg.pdf", array("Attachment" => false));
exit(0);
?>
I tried absolute path like:->
<image src="E:\xampp\htdocs\manresaorg\im_Steve-Raymond-1.jpg">
But it is not showing image on PDF.I am using Xampp Server on window.
I have the following code which uses DOM PDF Library for converting Html content to PDF file.
$dompdf = new DOMPDF();
$dompdf->set_paper(array(0,0,450,306));
$dompdf->load_html($html);
$dompdf->render();
$output = $dompdf->output();
file_put_contents($pdf_path, $output);
But it generates 2-3 pages based on the html content.
I wanted to limit it to single page only and skip the remaining content.
The easiest way is to render to image. The GD adapter allows you to specify the page you want to render.
dompdf 0.6.2 or earlier: set DOMPDF_PDF_BACKEND to "GD" which only renders the first page of the document.
dompdf 0.7.0 or later: $dompdf->set_option('pdfBackend', 'GD');. This release renders all pages and allows you to specify the page to output/stream (default is to send the first page).
If you really need to render only the first page to a PDF that's a bit more difficult. Though technically possible to do this only with dompdf I'd actually be inclined to fully render the PDF then use an external library to pull out just the first page.
For example, with libmergepdf you could do it like this:
use iio\libmergepdf\Merger;
use iio\libmergepdf\Pages;
use Dompdf\Dompdf;
$m = new Merger();
$dompdf = new Dompdf();
$dompdf->load_html('...');
$dompdf->render();
$m->addRaw($dompdf->output(), new Pages('1'));
file_put_contents('onepager.pdf', $m->merge());
(specific to dompdf 0.7.0, but code is nearly the same for 0.6.2)