I am trying to generate a PDF file using codeIgniter and dompdf.
The script below creates the PDF file, but the css are not shown. The script below generates only the HTML structure.
public function create_pdf() {
$this->load->view('invoice');
$html = $this->output->get_output();
$this->load->library('dompdf_gen');
//Convert to PDF
$this->dompdf->load_html($html);
$this->dompdf->render();
$dompdf = new DOMPDF();
$this->dompdf->stream("welcome.pdf",array('Attachment'=>0));
}
How can I include following css in the script?
<link href="<?php echo base_url('assets/css/bootstrap.css');?>" rel="stylesheet" />
<link href="<?php echo base_url('assets/css/style.css');?>" rel="stylesheet"/ >
I use CodeIgniter and dompdf together and I include my CSS file by simply adding a link tag into the head of the view of I am printing i.e.
<link rel="stylesheet" type="text/css" href="<sitename>/full/path/to/print.css">
I do only link a basic CSS file in though so I would try a basic CSS file to start with (a file that just puts all the text in bold or something) and then if that works gradually add more rules as the issue could be your CSS files contain something which dompdf cannot handle.
You must have figured out this issue since it was asked X years ago.
But I found the solution via this github issue.
Maybe, someone who's looking for the issue, this might be useful.
Tried & Tested by me. 💹
Worked for me. 💹
Get the Instances of dompdf to your controller as
$dompdf = new Dompdf\Dompdf(['isRemoteEnabled' => true]);
Related
In Adobe Acrobat there is an option to add a "background" to a PDF file and set the default settings that this image should be visible when opening the document but should not be printed out. I want to automate the process by a PHP script.
I checked all the popular PHP PDF libs (TCPDF, FPDF, mPDF, ...) but none of them seems to provide such an option. All I found is adding images by the ->Image method and place it behind the text. This does work when viewing the document, but of course it is also printed out.
A second approach is to render plain HTML and include custom stylesheets. I created the simple HTML
<h1>Simple text.</h1>
<div>
<p>Should be printed.
<img src="..."></p>
</div>
<div class="no-print">
<p>Should NOT be printed.
<img src="..."></p>
</div>
and saved the CSS in print.css
.no-print {
display: none;
}
and included it by:
<link rel="stylesheet" media=“print” type="text/css" href="print.css">
The result does not show the second div. I guess the PDF libs do not evaluate the media in the link tag. To be honest this approach does not feel right, especially because PDF !== HTML.
Nevertheless I cannot imagine that this is so difficult. How do all the big companies manage this? I'm grateful for every hint!
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.
i designed the invoice using bootstrap css, i used tcpdf to design invoice. I called bootstrap css file but its not working, Kindly help me.. My coding is
$baseurl = "http://localhost/erp";
$html = '<style>'.file_get_contents($baseurl.'css/bootstrap.min.css').'</style>';
$html = '<style>'.file_get_contents($baseurl.'css/style.css').'</style>';
First of all - Bootstrap use js scripts for working; As You understand, scripts could not be working in tcpdf file;
More than that - tcpdf not allowed all CSS attributes; I can't find supported css list in official documentation, but this can help;
So I am using the codeigniter pdf library from: https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library
And it works wonders except, It doesn't keep the style sheet when the pdf is generated.
I am trying to use bootstrap to make it look nice, But when I run the script and download the pdf it doesn't have the styling anymore. What do I need to do to keep the stylesheet linked?
My Controller:
public function AdminPracticeSheetLateReport()
{
$this->load->view('pdf/practiceLateReport');
$this->pdf->load_view('pdf/practiceLateReport');
$this->pdf->render();
$this->pdf->stream("welcome.pdf");
}
I am loading the bootstrap stylesheet like so in my view:
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
Is this even possible with this plugin? If not it's quite the crappy plugin if you ask me.
BTW: This library for codeigniter uses the DomPDF
I just checked the source code of codeigniter and noticed that it does not itself make a PDF, but uses DOMPDF in turn (which is another PHP library). codeigniter does not properly send the html and css to DOMPDF, so in my suggestion you should remove codeigniter and use DOMPDF instead. This should correct your problem and also speed up the conversion by a few miliseconds.
I want to generate a PDF of a webpage but apply an alternate, print-type stylesheet to it instead of the styles it uses now. Say, for example, I have a button on http://eorailway.co.uk to generate a PDF of the same page (which is run and administered by me, so therefore I can include any PHP/JS necessary to each page) but I want to apply alternate styling to it before generating the PDF.
At the moment I am using the dompdf PHP library to generate the PDF using the normal/default stylesheet, but cannot for the life of me think how to apply the alternate stylesheet to the page when clicking the "Generate PDF" button.
Any advice is most appreciated.
Since the site is under control, you could dynamically decide which stylesheets to include based on a query string parameter. i.e. http://example.com/page.php?stylesheet=print would have your template output only the alternate stylesheet, and your PDF library would fetch that page to generate.
I would recommend making an alternate page with the "print" stylesheet applied and point to it using the print meta tag. (e.g. <link rel="alternate" media="print" href="<? ECHO $_SERVER['PHP_SELF'].'?print'; ?>""> )
Then have PHP determine the stylesheet to use based on the presence of that GET variable.
You can use the DOMXML stuff in php to apply a specific XSLT file to some XML:
$stylsheet = "Example.xsl";
$xsldoc = domxml_xslt_stylesheet_file($stylsheet);
$htmldoc = $xsldoc->process($xmldoc);
$results_page = $xsldoc->result_dump_mem($htmldoc);
That's something I did in php4, might be an easier way in 5.
In the 0.6.0 release of DOMPDF you can specify the stylesheet to use by modifying the DOMPDF_DEFAULT_MEDIA_TYPE configuration constant.
http://code.google.com/p/dompdf/source/browse/trunk/dompdf/dompdf_config.inc.php?r=336#234