简体   繁体   中英

Output Buffer + Pdf - PHP

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

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');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM