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/
Related
I'm using mpdf to create PDF files on the fly, and the files open fine in a browser but Adobe gives me an error:
Adobe Acrobat Reader DC could not open 'example-filename.pdf'
because it is either not a supported file type or because the file
has been damaged (for example, it was sent as an email attachment
and wasn't correctly decoded).
I looked at other questions about this (another mpdf + adobe error), and checked out the pdf in a text editor. I found that the first part of the file looked like this:
<!DOCTYPE html>
<head>
<title>
CapstoneDB
</title>
%PDF-1.4
%âãÏÓ
After I removed everything up to %PDF-1.4 (including the tab), the file opened fine in Adobe, which is great, except that I need to be able to get the generated pdfs to open in Adobe without manually fiddling with the code every time.
Here's my wrapper function that calls mpdf with the html and css:
include('../mpdf/mpdf.php');
function user_download_pdf($html, $css_file, $filename) {
$mpdf = new mPDF();
$stylesheet = file_get_contents($css_file);
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output($filename, "D");
}
I never feed mpdf a full html page, usually just an h3 and one or more tables. Maybe I need to be giving mpdf an entire html page, including <head>, <body>, etc? Is there any way to change mpdf configuration or the way I call mpdf in php that would get rid of the html junk at the beginning of the pdf file that's gunking everything up?
Place
ob_clean();
immediately before
$mpdf->Output();
Without this mpdf sometimes includes the website page HTML and not just the HTML that you want in the PDF, probably because headers have already been sent elsewhere in the code. That can mess up your PDF so Adobe won't open it.
I'm using FPDF to create an A4 sized PDF, I can add text/images and this is working fine. I have a dynamically generated string which contains some HTML tags (BR etc) and was wondering how I can have that output onto the PDF as opposed to literally printing the tags out.
$centre = urldecode($row_CertDetails['centre_name']);
$pdf->Text(10, 110, $centre);
I've seen on some forums people mentioning WriteHTML, however my version of FPDF doesn't seem to include that.
Is it an add-on?
Yes, it is an Add-On:
Write HTML
Author: Clément Lavoillotte
License: FPDF
This is an enhancement of the WriteHTML() method from tutorial 6. Supported tags are: ...
Please see Write HTML FPDF Script #42 which I think is the latest version, but feel free to browse the FPDF scripts/Add-Ons.
I've solved it anyway.
Because I'm also using FPDI, what I did was extract the contents of the class in http://www.fpdf.org/en/script/script42.php and put it inside the FPDI.php file.
Here is the dompdf rendering of my SO profile page....
https://docs.google.com/file/d/0B-I8istAg8Z6UmJDVHEtUkZOUDQ/edit
here's the code
<?php
require_once("dompdf/dompdf_config.inc.php");
$html = file_get_contents('http://stackoverflow.com/users/1461078/samidh-t');
$base_path = 'http://'.$_SERVER['HTTP_HOST'] ;
$dompdf = new DOMPDF();
if ( isset($base_path) ) {
$dompdf->set_base_path($base_path);
}
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream('file.pdf' , array("Attachment" => 0));
?>
the rendering is not even close to the profile page.
What can be done to improve this rendering ?
Dompdf - from memory, renders pages ok when the css and html are simple - lots of floats do not work - and you will find rendering web pages as pdf's using dompdf difficult enless you are writing the html and css yourself. (or parsing the input files first and modifying them).
What is your overall goal?
I have used dompdf to create pdf reports of data from a database and have had success but not using 'float' or any other advances css rules.
Dompdf info on css rules it can handle are here:
From this page:
https://github.com/dompdf/dompdf/blob/master/README.md
This text:
Limitations (Known Issues) not particularly tolerant to poorly-formed HTML input (using
Tidy first may help). large files or
large tables can take a while to render CSS float is not supported
(but is in the works). If you find this project useful, please
consider making a donation.
In html page some tags are dynamically created using jquery and contents are loaded from msql db using jquery and php.
I want convert this dynamic page to pdf.
I have tried following code but it generate pdf of static part of html page.
<?php
ob_start();
?>
//html code and internal css, javascript used in external file with jquery library
<?php
include('dompdf/dompdf_config.inc.php');
$contents = ob_get_contents();
ob_end_clean();
$dompdf = new DOMPDF();
$dompdf->load_html($contents);
$dompdf->render();
$dompdf->stream('file.pdf');
?>
So how to store contents of dynamic html page in a php variable after processing it ( using javascript/php ) and convert it to pdf usin dompdf or other converter.
I'd suggest you take a look at wkhtmltopdf. I've had good results with getting it to render google charts, which are built dynamically from a javascript api.
http://code.google.com/p/wkhtmltopdf/
As Marc said, you have to read generated DOM with javascript.. something like $('html').html() and then post it to php to generate pdf
This may not meet exactly what you are looking for, but wkhtmltopdf could be what you need. Since PHP is a server-side technology, you will have a difficult time getting it to process any client-side javascript. wkhtmltopdf scans the page, javascript and all, then generates a pdf file on the server. Hope this helps you out!
Consider using a tool like wkhtmltopdf to directly generate a page as a PDF. It will run javascript and generate the page as WebKit would render it.
A client of mine has a food blog hosted on WordPress. Each post entry contains some text and a div called "recipes" with some more text inside it. They would like to add to this div a link that generates a PDF of the recipe, dynamically, for saving or printing, as the user sees fit.
I have seen quite a few Wordpress plugins that offer the conversion of entire posts to PDF but not anything that's customizable enough to select a given portion of a post, the way we'd like to.
Any suggestions on how to do this? I'm comfortable with PHP, Javascript, CSS but am new to the various PDF libraries.
Take a look at dompdf It's pretty easy to work with :) This is from the documentation:
<?php
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Put your html here, or generate it with your favourite '.
'templating system.</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
If you need more control than dompdf you could always use PHP's XML/XSL methods to convert the HTML to XSL:FO and use FOP on the commandline to generate the PDF. It's a little long-winded but you get complete control of the output styling/structure, the ability to "lock" the PDF, provide metadata, etc.