Is there an undo, reset or clear capability in mPDF? - php

Is there a way, after calling $mpdf->WriteHTML( $html ) to either call "undo" or reset it back to a blank state?
I've looked here and found nothing so far.

You can use the DeletePages() function to delete / reset the content

I'm using 1.7.5 version of Mpdf and now I am confused and devastated. I was trying to use same Mpdf instance for multiple documents (config dependant). I was trying to make it this way:
// for each document
$this->mpdf->WriteHtml($content);
$this->mpdf->Output($path, Destination::FILE);
// below does pretty much nothing O.o
$this->mpdf->DeletePages(0, $this->mpdf->PagesNo());
$this->mpdf->Reset();
Without DeletePages() on second document content of PDF file was bizarre. Text was from first file, title from second, file size suggested two files merged and content was sum of pages, where after content of first document pages were blank (not empty as white, but blank, as transparent). The only difference after using DeletePages() was that content was only first document with title from second (in metadata).
For me, the only solution was not to inject Mpdf into my Service, but 'generator': function without arguments that hid initiation of Mpdf and on call provided new 'fresh' Mpdf instance.

Related

TinyMCE editor to MPDF page content limit

I have a site which uses Tiny MCE to allow users to input content in a page layout. Sort of like Microsoft Word but the pages are tabbed.
When the user is happy with their content they can publish it as a PDF using MPDF.
The problem I'm facing is that if a user enters too much text or images into a "page" tab, the PDF will produce 2 pages for that page.
What I'd like to do is somehow detect if this was going to happen and either restrict the user or at least show an error.
I'm guessing maybe there is some way to tell before publishing, whether it will render as 1 page or 2? Maybe it could ajax when the content is changed and do some sort of MPDF check? Would this be a good way to solve it?
In mPDF, there is no way of telling the number of pages resulting document will have until Output() method is called.
You could do something like this:
$string = $mpdf->Output('', 'S');
if (count($mpdf->pages) > 1) {
// Display error
} else {
// return PDF from $string variable containing PDF contents
// or call plain $mpdf->Output() which will also handle correct content-type headers
}

PDF Generation using Dompdf in codeigniter redirects to a 404 page in case of large pdf files

I am trying to generate pdf file in codeigniter using Dompdf library. Currently the PDFs are being generated for html with small content without any issues, But when the size of the content increases over 20000 characters, the script breaks and redirects to a 404 page.
The following is the Codeigniter controller function that i am using to generate the pdf.
public function generatePDF(){
$this->load->model('SmartGraduatesApplicationmodel');
$result['result']=$this->SmartGraduatesApplicationmodel->smartGraduateApplicationPDF($this->uri->segment(3));
$this->load->helper(array('dompdf', 'file'));
$html = $this->load->view('smart_graduates_application/smartGraduateApplicationPDF', $result, true);
pdf_create($html, 'SMART_Graduate_Application');
}
I have already tried increasing the Max execution time, Max input time and Memory Limit with no luck.
Can you help me with your ideas?
Thanks
I managed to sort out the above issue. When using Dompdf with nested tables and large content in html, this issue seems to happen. To fix the issue, we need to use DIV instead of TABLE incase of large content in html.

using PHP to fill in a PDF

I need to fill in a PDF, in the fly, using PHP - have no idea where to start.
I'm currently doing quizzes on line using PHP - once a series of quizzes are passed the client wants to let the user download a 'certificate of completion'
The PDF of the certificate has blank lines for the users name and the area of study.
My thought is - add 2 form elements to the PDF, and have PHP pill them in when I pull of the certificate.
BUT HOW?
Is there a different, better way?
Things I need to be 'cautious' of - installing third party stuff is not reliable, UNLESS I can just drop a lib in the the site root. I can't guarantee the hosting provider will let me change a PHP config.
Any help is appreciated - the more specific, the better.
Sorry I have no code at the moment - other than how I'm currently displaying the PDF -
// show cert
echo '<iframe src="1_cert.pdf" width="1000" height="700">';
Thanks.
OK - using TCPDF - as suggested - I've installed, and example pages work...
I've placed a file in the example folder.... I've included a call to import...
require_once('../tcpdf_import.php');
// create new PDF document
$pdf = new TCPDF_IMPORT('1_cert.pdf');
...other boiler plate copied form other examples...
$pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');
// set font
$pdf->SetFont('times', 'B', 20);
$pdf->setPage(1, true);
$pdf->SetY(50);
$pdf->Cell(0, 0, 'test text', 1, 1, 'C');
$pdf->lastPage();
The error I'm getting "TCPDF ERROR: Wrong page number on setPage() function: 1"
Ok, if you're not showing code, then neither will I. ;)
I would recommend doing it with TCPDF.
Import the PDF with TCPDF_Import
In a PDF document, you can navigate to any x/y position, (NB: 0/0 is bottom left). Therefore, simply set your “cursor” to the position to each field, and insert a text with either the Cell or the writeHTMLCell method.
Save the PDF document.
Display it to the user.
Voilà.
By the way, both FPDI and TCPDF are common PHP libraries, so you can just put them somewhere in your base folder, no additional tools should be required on a common web server.

How to add graph to website?

I'm a beginner here and need help. I have this code, which works and outputs a graph in my browser (if this is the only code in php file). I don't know how to add text below or above just like any other site. When I try, it returns my whole code in the browser. How do I go on about this?
<?php
// content="text/plain; charset=utf-8"
require_once ('jpgraph/src/jpgraph.php');
require_once ('jpgraph/src/jpgraph_line.php');
// Some data
$ydata = array(11,3,8,12,5,1,9,8,5,7);
// Create the graph. These two calls are always required
$graph = new Graph(350,250);
$graph->SetScale('textlin');
// Create the linear plot
$lineplot=new LinePlot($ydata);
$lineplot->SetColor('blue');
// Add the plot to the graph
$graph->Add($lineplot);
// Display the graph
$graph->Stroke();
?>
Thanks in advance!
What you need is quite simply:
<img src="graph.php">
Put that in a separate HTML file or PHP script. You cannot output the image and text in the same script / web page. It needs to be separated.
Don't worry about the .php extension for the image src= attribute. It will display despite the lack of .jpeg extension. (The Graph class already outputs the correct MIME type I assume.)
I'm a bit rusty on PHP, but I believe that all "require" are supposed to be made before any content is output. Otherwise, the normal HTML/XHTML syntax and formatting take precedence.
I haven't actually used this library, but I'm assuming the image is output directly to the browser. It's probably easiest to create a new HTML document (or whatever the rest of your site is powered by) and include this as an image, with the image's src being the name of this script.

HTML2FPDF print page results as pdf

I'm trying to user HTML2FPDF (http://html2fpdf.sourceforge.net/) to create a PDF of a page, but I can't seem to get it to work properly.
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
http://portal.flyingstartlifestyle.360southclients.com/leanne/leanne.php <- the graph with the html2fpdf code at the bottom of the page.
HTML2FPDF code:
function createPDF() {
define('ABSPATH', dirname(__FILE__).'/');
require(ABSPATH.'classes/pdf/html2fpdf.php');
$pdf = new HTML2FPDF();
$pdf->AddPage();
$html = ob_get_contents();
//$html = htmlspecialchars($html);
if ($html) {
$fileName = "testing.pdf";
$pdf->WriteHTML($html);
$pdf->Output("pdfs/".$fileName);
echo "<p>PDF file is generated successfully! Click here to open it.</p>";
} else {
echo "<p>There has been an error in creating your PDF.</p>";
};
};
If I unhide the line "$html = htmlspecialchars($html);" it prints the pdf the text of the page, otherwise it creates an empty PDF. Any ideas how I can transfer my graph to a PDF?
Cheers
A few years back, I've been beating my head against the wall trying to convert HTML into PDF for days. What I wanted to do was really simple - make an invoice for customers into a PDF file. An image (logo) up on top, a few paragraphs, and a table with a list of charges.
The hole shaped like my head on the wall is still there. All of the free libraries that convert things to PDF - they all suck. I found one that sucks the least, it's DomPDF. At least that one ended up doing the job, after a week of suffering and debugging. It's not fast by any means, though (if you want to generate a complex PDF, you might want to do it off-thread).
My page consists of jQuery to show a graph. I want the graph and other text on the page to be exported as a PDF.
jQuery is interpreted by the browser and not by the server. When you send the HTML to be rendered into PDF, it will not run the Javascript. You'll need to find a way to actually generate the image some other way.
I guess I could see a situation where you could use ajax to make a remote call and send all of the html that the js sees.
The remote call then would write a file of that html. The remote call would send back a file name for the pdf to be generated.
Your js then could provide a link to the processing page of the html2pdf that references the file created from the remote call.
This would work, but it might be a bit much.
Regards.

Categories