I've got this code to export some data into a pdf. And I would like to add css from an external css file (which is not mentionned in the html used)
/*********************************** Export PDF ****************************************************/
if($request->query->get('exportPDF')!= null){
// Configure Dompdf according to your needs
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
// Instantiate Dompdf with our options
$dompdf = new Dompdf($pdfOptions);
// Retrieve the HTML generated in our twig file
$html = $this->renderView('dashboard/user_table.html.twig', [
'users' => $users
]);
// Load HTML to Dompdf
$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation 'portrait' or 'portrait'
$dompdf->setPaper('A3', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser (force download)
$dompdf->stream("exportUsers.pdf", [
"Attachment" => true
]);
}
The user_table.html is only a file with a <table>
who has some class from a css file loaded in an other template. That means for DomPDF the file who contains the css is unkown and, as a result I have a table with no css in my pdf.
I've tried to add the stylesheet in my html directly but the import isn't working like that neither. But I don't want to add it in the html anyway, the css is loaded is a more hight level template.
How to add external files (like bootstrap etc etc) from this structure ? I do not know if this is even possible. Thanks for the help ;)
the css file must be referenced in the HTML you give to DomPDF.
If you don't want to change your twig template, you can use a workaround like this :
$dompdf = new Dompdf();
$html = $this->renderView('dashboard/user_table.html.twig', [
'users' => $users
]);
$html .= '<link type="text/css" href="/absolute/path/to/pdf.css" rel="stylesheet" />';
$dompdf->loadHtml($html);
Note that adding a link tag to the body is not valid according to HTML specifications. With the current Dompdf version, it works but it may not work in future versions.
You can always use solutions like this Including a non-twig file from twig read external file contents right into templates attribute, so css will be rendered inline and will be compatible with mpdf.
Related
I haven't been able to find a way to display on the Browser's tab a Title (like meta title) when generating a PDF suing domppdf.
In some posts they suggest adding this to the HTML code:
<html>
<head><title> My Title </title>
</head>
<body>
/** PDF Content **/
</body>
But, maybe it is due to version that I am using, but adding these tags breaks my code and dompdf returns a long error. I only have the body and it works great.
Unfortunately I can't upgrade my version of dompdf, so I am trying to find another solution.
I don't think it is possible by using PHP headers, but I am open to suggestions.
I have this so far:
$dompdf = new Dompdf();
ob_start();
include("pdf_HTML.php");
$fileName = "download.pdf";
$content = ob_get_clean();
$dompdf->loadHtml($content);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('Letter', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
header("Content-type:application/pdf");
header("Content-Disposition: attachment; filename=$fileName.pdf'");
$dompdf->stream($fileName,array('Attachment'=>0));
After much looking I found the way:
$dompdf->add_info('Title', 'Your meta title');
It took me some time to find the answer so I am sure this will be helpful for someone else.
I am genearting a pdf file using DOMPDF. My pdf file is admit card which holds the information about the particular student.
In this admit card I have wo images to show. So in my twig file,
Profile Image:
<img src="{{ response['display_image'] }}" alt="Profile Pic" width="100px" height="100px" class="img-thumbnail" />
Company Image:
<img src="images/webkul.png" alt="Webkul"/>
Both of these images are not generated in my PDF file. I gives 'image not found or type unknown'.
I am fetching profile image from database while company image is
stored in project file that is,
public/assets/images/xyz.png
Controller:
$pdfOptions = new Options();
$pdfOptions->set('defaultFont', 'Arial');
$pdfOptions->setIsRemoteEnabled(true);
$dompdf = new Dompdf($pdfOptions);
$html = $this->renderView('download_pdf/index.html.twig', [
'title' => "Admit-Card",
'response' => $response,
'data' => $data,
]);
$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
I have attached the screen-shot.
How should I give path in my twig file so that image can be rendered in PDF file?
Put the whole path from the disk:
https://github.com/dompdf/dompdf/issues/1523
EX:
use Dompdf\Dompdf;
use Dompdf\Options;
require_once 'dompdf/autoload.inc.php';
$options = new Options();
$options->set('isRemoteEnabled', TRUE);
$options->set('tempDir', '/home2/directory/public_html/directory/pdf-export/tmp');
$dompdf = new Dompdf($options);
Actually problem exists in the header part of HTML file. I was extending head of my project which was containing lots of CSS and JS. I simply removed the head and used a simple and clean head instead and my problem was solved.
Note:
Do not extender header/footer of your project into Html of pdf file, instead use a new one for the same.
I am working with laravael,I have an HTML view that has CSS integrated. I want to convert into a PDF. If I open the view (it doesn't mather if I open it via my Documents or by a link in my app) it works fine, everything looks ok. But if I get that file and generate a PDF with dompdf, when I open it the css for the background and the images are in their places but the texts change places and have another size.
Here is how I convert it to a PDF
$file = file_get_contents("../resources/views/panel/historial/pdfs/otros.html");
$dompdf = new DOMPDF();
$dompdf->loadHtml($file);
//$dompdf->load_html_file($html);
$dompdf->render();
$dompdf->stream("otros.pdf", array("Attachment" => 0));
return $dompdf;
enter image description here
I think that is not working in that way to except then the DOMPDF-Renderer has not the full CSS functionality.
https://github.com/dompdf/dompdf/wiki/CSSCompatibility
Here is a list of elements that are supported. So in your case i would suggest that you render a new template and make it with a different style for your PDF.
Another good solution is wkhtmltopdf which has a better support but is a command line tool which you have to call over php or if you don't need PHP then run it directly from your command line.
https://wkhtmltopdf.org/
Good Day,
I am trying to render a calendar to pdf using dompdf,
heres my html
and my php
<?php
// require .'vendor/dompdf/autoload.inc.php';
require("vendor/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf();
// Load HTML content
$dompdf->loadHtml($_REQUEST['html']);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('LETTER', 'portrait');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();
?>
the html is is good I think, just without head and html tag.
and heres the result.
can you please help me figure what wrong? the html displays fine on browser too.
Might be too late to this answer, but anyone looking for this issue, please try display: inline, it solves this issue but still need to specify width and height and not auto calculate.
i am using codeigniter i am working on invoice and i want to print a PDF file of my invoice page here is my controller.
$pdfFilePath ='invoice.pdf';
$data=$this->singleinvoice($invoiceId);
ini_set('memory_limit','32M');
$html = $this->load->view('invoice/invoicetopdf', $data, true);
$this->load->library('pdf');
$pdf = $this->pdf->load();
$pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822));
$pdf->WriteHTML($html);
$pdf->Output($pdfFilePath, 'D');
redirect("invoice");
when i echo $html veriable is show fine but when it output the pdf .it has no css styling i mean it convert the HTML without any of my css Style.
i also tried the following but no luck.
$style=file_get_contents(base_url().'_assets/css/bootstrap.css');
$pdf->WriteHTML($style,1);
$pdf->Output($pdfFilePath, 'D');
Now i want to convert my HTML page to PDF as it is shown to the public with complete styling and markups.
does it possible any suggestion or solution ?
you have to convert your Page Structure from DIV to Traditional Table structure.
arrange your elements contents etc.into table or tables.
then try some basic CSS.
because most of CSS are not supported in mpdf or any other pdf converter.
Click here
to know what(CSS) is supported and what is not in mpdf.