I have a page that uses the glob function and file_get_contents to have a few html files and store them in the buffer.
So I want to convert this buffer ob_get_contents() to an pdf file.
What is the best way to do that? how?
Thanks in advance.
For creating PDF files from HTML and CSS, check out DOMpdf.
While this solution doesn't support the full range of HTML and CSS and its rendering can be a pain sometimes, it has one advantage: it does not require any special binaries to be installed (like wkhtmltopdf). It should run on your average shared PHP hosting.
Usage example:
<?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");
?>
why using the outputbuffer for this? you have it in variables using file_get_contents and can simply create your pdf with the data from the variables. when using ob_get_contents all it does is return the outputbuffer and what you normally do with the result is saving into a variable...
btw. you do want to convert html into pdf? If yes have a look at wkhtmltopdf
If ob_get_contents contains html files they are so many solutions out there that can achieve what you want. I think you should look at the following
PrinceXML
FPDF
TCPDF
HTML to PDF converter (PHP5)
wkhtmltopdf
Example using Simple HTML 2 PDF using PHP
$html = ob_get_contents();
ob_end_clean();
$pdf = new HTML2FPDF();
$pdf->SetTopMargin(1);
$pdf->AddPage();
$pdf->WriteHTML($html);
$pdf->Output('test.pdf','D');
Related
Because assigning a value to the "$html" with whole code which is written with php and html is tedious. Is there any way? or can we make whole page as pdf directly?
It really depends on your use case. For example, if the page you want to render to PDF can be accessed via a web server then you can just load that page in your Dompdf script:
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html_file("http://example.com/document.php");
$dompdf->render();
$dompdf->stream();
If the content and Dompdf logic needs to all be on the same page then you can use output buffering to capture the HTML and feed it to Dompdf:
ob_start();
// HTML + PHP to create output
$html = ob_get_clean();
ob_end_clean();
// include dompdf then ...
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream();
I am using mPDF through PHP in order to generate an exact replica PDF of an HTML page. This PDF is then being saved to the server so that it can be automatically printed for the user through PHP socket programming.
When I print preview the HTML page, it looks exactly as I would want it printed. When I convert the HTML to a PDF with mPDF, it becomes wide and distorted. I just want it to look exactly like the HTML print preview with no changes.
Here is my PHP code:
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf();
ob_start();
include 'cert.html';
$html = ob_get_contents();
ob_end_clean();
$mpdf->WriteHTML($html);
$mpdf->Output();
Ditch mPDF and use a browser based library such as wkhtmltopdf.
I switched from TCPDF to domPDF because it seems more convenient to handle when creating invoices from html to pdf (I am rather a low pro on PHP :)). Now that I created the html file as a PDF file I recognized it does not output any PHP in the PDF - since the data from my sql databanks should fill the PDF it is kinda a problem.
I saw that you can enable PHP in the options.php included in the src-folder and I tried to do like it is written in the manual (and also tried various other code lines) but it just doesn't want to work:
$root = realpath($_SERVER["DOCUMENT_ROOT"]);
require_once ("$root/../xxx/dompdf/autoload.inc.php");
use Dompdf\Dompdf;
use Dompdf\Options;
$options = new Options();
$options->setIsPhpEnabled('true');
$dompdf = new Dompdf($options);
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
$dompdf->stream("bla",array("Attachment"=>0));
The PDF is shown but without the input from any PHP code.
If someone would be so kind, I would also be interested in knowing why and in how far enabling PHP is a security risk since I actually want to use that for my business. Would it be more advisable to wrap it all up in the main php file without loading external html and css files?
Thanks a lot in advance!
You could do something like this (not tested the code). Replace
$dompdf->loadHtml(file_get_contents("testdomhtml.php"));
With
ob_start();
include 'testdomhtml.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
More options How to execute and get content of a .php file in a variable?
Your file_get_contents("testdomhtml.php") will get actual content of file and will not execute any code inside it. Instead make it web accessible and pass URL to this page:
$dompdf->load_html_file('http://yourdomain.ext/testdomhtml.php');
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)
I try to convert dynamic php database file to pdf.
I try with DOMPDF, but I have a problem with defining the string. I'll explain:
Here is a 'hello world' script for dompdf:
require_once("dompdf_config.inc.php");
$html =
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>';
$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("hello_world.pdf");
The thing is that instead of a simple
'<html><body>'.
'<p>Hello World!</p>'.
'</body></html>'
I have a long php file full of functions and sql queries. because of it I have inside it many ",',; e.t.c signs. I also have a javascript dynamic chart (jqplot) in this page.
So instead of pdf file I get errors errors errors...
Does anyone has a solution for this?
I will really appreciate any answer, and will be really really thankful for a solution...
First you'll need to generate HTML from your PHP, then pass it to DOMPDF:
<?php
require_once("dompdf_config.inc.php");
ob_start();
require_once("path/to/input/file.php");
$dompdf = new DOMPDF();
$dompdf->load_html(ob_get_clean());
$dompdf->render();
$dompdf->stream("file.pdf");
?>
You can also do a regular HTTP request:
<?php
require_once("dompdf_config.inc.php");
$dompdf = new DOMPDF();
$dompdf->load_html_file('http://example.com/file.php');
$dompdf->render();
$dompdf->stream("file.pdf");
?>
If you need JavaScript support, try wkhtmltopdf, it's based on Webkit and does it's work perfectly.
You can use ob_start and ob_get_contents to run PHP code and capture the output as a string.
For the JavaScript chart, though, you're out of luck. DOMPDF is pretty smart, but it's not that smart. You'll need to either use a non-JavaScript chart solution, do without the charts, or use a web browser to generate the PDF.
I am not sure why you need to generate HTML to build a PDF in the first place but as others have suggested, build out your PHP script and then use something like FPDF or TCPDF.
They both build PDFs just fine and can take HTML input.
Try This ...
<?php
ob_start();
require_once("dompdf_config.inc.php");
$file = file_get_contents('http://example.com/file.php');
$dompdf = new DOMPDF();
$dompdf->load_html($file);
$dompdf->render();
$dompdf->stream("filename.pdf");
?>