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.
Related
I upgraded mPDF to version 7 and I noticed that progress bar functionality was removed.
I'm looking for something simple: show some html information to the user (i.e. "Processing file...") while mPDF processes the output file.
This is a simplified version of the code and it is working properly:
$mpdf = new Mpdf();
$html_for_pdf = "<p>Hi, this is the html content to render the PDF file</p>";
$mpdf->WriteHTML( $html_for_pdf );
$mpdf->Output();
In order to get what I want, so far I tried:
a) Echo the "Processing file..." html info to screen, but (due to buffering I guess) nothing shows during the processing until the output PDF file finally comes up. The code I tried is this:
$mpdf = new Mpdf();
$html_for_pdf = "<p>Hi, this is the html content to render the PDF file</p>";
$html_processing = "<p>Processing file...</p>";
echo $html_processing;
$mpdf->WriteHTML( $html_for_pdf );
$mpdf->Output();
b) Handle the output buffering with ob_flush(), which shows the "Processing file..." html on screen but the mPDF file rendering breaks and the output file neves shows. The code I tried is this:
$mpdf = new Mpdf();
$html_for_pdf = "<p>Hi, this is the html content to render the PDF file</p>";
$html_processing = "<p>Processing file...</p>";
echo $html_processing;
ob_end_flush();
ob_flush();
flush();
ob_start();
$mpdf->WriteHTML( $html_for_pdf );
$mpdf->Output();
I googled a lot to get any kind of workaround for this without any luck.
Is there any chance to make this work?
Thanks!
Trying to do this during PDF generation in the same document does not make much sense.
Even the ProgressBar in mPDF 6.x updated the information with javascript.
The easiest way would be to process the HTML in a separate request with "Ajax". Any of options to send a request is possible, Fetch API, XMLHttpRequest, jQuery.ajax()…
You would then update content of the original HTML document with javascript:
On sending the request you would set "Processing file..." text,
On request completion you would either change this text to link to
the generated PDF, or send the PDF to the user directly to download.
A simplified example using the Fetch API:
<script>
// this code would be executed on a link click or form submit
document.getElementById('status').innerText = 'Processing file...';
fetch('http://example.com/pdf.php')
.then((response) => {
// check response.ok
document.getElementById('status').innerText = 'File processed';
// handle contents of the document
})
</script>
I have installed tangerine in my dompdf. Now what is happening is when i try on my windows machine it works fine. But in ubuntu when i put the same code in xampp the generated pdf is massed up. I used the tangerine font with only my h3 tags but in the generated pdf the font style is placed in whole body. Here is my dompdf settings
<?php
// Include autoloader
require_once 'dompdf/autoload.inc.php';
// Reference the Dompdf namespace
use Dompdf\Dompdf;
// Instantiate and use the dompdf class
$dompdf = new Dompdf(array('isPhpEnabled' => true));
// Load content from html file
ob_start();
include_once 'pdfcontent.php';
$output = ob_get_clean();
$dompdf->loadHtml($output);
//$dompdf->loadHtml($html);
// (Optional) Setup the paper size and orientation
$dompdf->setPaper('b4', 'landscape');
// Render the HTML as PDF
$dompdf->render();
// Output the generated PDF (1 = download and 0 = preview)
$dompdf->stream("funeral",array("Attachment"=>0));
?>
Well the the problem was basically with the cached dompdf file. I have deleted the dompdf folder and uploaded again. Now it works fine.
I have a FlipBook jquery page and too many ebooks(pdf format) to display on it. I need to keep these PDF's hidden so that I would like to get its content with PHP and display it with my FlipBook jquery page. (instead of giving whole pdf I would like to give it as parts).
Is there any way i can get whole content of PDF file with PHP?
I need to seperate them according to their pages.
You can use PDF Parser (PHP PDF Library) to extract each
and everything from PDF's.
PDF Parser Library Link: https://github.com/smalot/pdfparser
Online Demo Link: https://github.com/smalot/pdfparser/blob/master/doc/Usage.md
Documentation Link: https://github.com/smalot/pdfparser/tree/master/doc
Sample Code:
<?php
// Include Composer autoloader if not already done.
include 'vendor/autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('document.pdf');
$text = $pdf->getText();
echo $text;
?>
Regarding another part of your Question:
How To Convert Your PDF Pages Into Images:
You need ImageMagick and GhostScript
<?php
$im = new imagick('file.pdf[0]');
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
?>
The [0] means page 1.
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 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');