TinyMCE editor to MPDF page content limit - php

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
}

Related

How to check if the assets loading in to a HTML page returns 400 status using jquery

I am creating a web-based HTML rendering platform using PHP. Here I will be showing an online IDE-like interface and add HTML, JS, CSS, Video and Image files. Clicking on a button will load these items on a new tab as a complete HTML website.
I need some validations here. So that it will return the number of assets added in the HTML page which returns 400 status (files that don't exist).
I can't add the check inside the Jquery file which is inside the HTML package. Because that is created by the user.
What is in my mind is,
Click on a button named "Validate"
It will load the index.html file from the one user-created.
Some functions will track the loading of all asset files which are added to the index.html file
Return the number of assets that return the 400 error.
This is the flow in my mind. I am able to load the HTML file which the user has created using my platform. But having no idea how to validate how many of the asset files returning 400 status.
Can someone help me with some ideas on this or some sample code?
For PHP based solution, see file_exists().
$filename = '/path/to/foo.extension';
if (file_exists($filename)) {
// file exists
} else {
// file DOES NOT exist
}
Also you may consider to use is_readable() with file_exists().

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

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.

How to use a html email signature that is hosted online?

for my email signature I want to use a hosted HTML code that is generated through a PHP script, since I may want to change information of the signature I really want that hosted solution to be sure that not only mails in the future, but also sent mails have the "new" signature.
I have a PHP script behind "tld.com/signature/my#mail.com" (yes, no .php ending) waiting for a call. If i open that domain it will output valid HTML code.
But how can I embed the output in my mails as html signature?
Iframes are not an option, since they dont work everywhere.
Html img tags dont work since that url doesnt output an image.
Any ideas? :-) - Thanks!
As far as I can see image and iframe are the only way of achieving this, though you could possibly use dynamic css too; either way, your signature generation API would need to change, or at least be wrapped.
A blog post here details how the author went about doing just this for posting dynamic scores on a user's Facebook wall.
This is the workflow they used:
As the score had to be dynamic, what they did was create an API to take a snapshot image of the HTML content which the client could then show.
They created an API endpoint which would
Query the score HTML generator endpoint (in your case signature generation) to generate the HTML you want the client to display.
Turn the HTML into PDF using the HTML2PDF utility for PHP 5.2.
Render the PDF into an image which can then be returned to the user for displaying using PHP::ImageMagick.
They did this with the following code:
$html2pdf = new HTML2PDF('P', 'A4');
$html2pdf->writeHTML($html_content);
$file = $html2pdf->Output('temp.pdf','F');
$im = new imagick('temp.pdf');
$im->setImageFormat( "jpg" );
$img_name = time().'.jpg';
$im->setSize(800,600);
$im->writeImage($img_name);
$im->clear();
$im->destroy();
The image returned by this API can then be rendered into an image tag for display to the client. There is a demo of this workflow in action available here.

How to dynmically draw picture in php gd

Hi I have searched the web for 2 days but did not accomplish what I am looking for.
I have an apache server which will be accessed by 146 students. the user picks an angle from dropdown lets say 45 degress, then user clicks CALCULATE button. Then user clicks DIAGRAM button to see how the sine graph looks like.
Works like charm when i write the image to a file e.g: imagepng($img,"diagram.png");
Now the problem is that the diagram.png will always get overwritten by the last user. So for example if another user logs in and calculates the Sin 135. Both users will see Sine 135 because filename is hardcoded since there is conflict of filename.
I have searched the web on how to create the image dynamically instead of writing to a file and then reading the file. I have come across the following but not working:
base64_encode and decode
What would I have to do to my code of imagepng(...., ...) mentioned above to make use of base64 so I can actually draw the picture of already processed data. Let assume if I comment out the imagepng(..) code, then what do I replace it with. I hope I don't have to change my code a whole lot.
Please help
thanks
Amit
The filename argument to imagepng is optional. From the manual:
filename
The path to save the file to. If not set or NULL, the raw image stream will be outputted directly.
You would just need to send a png header at the top of the script and you would get the image as output for that script.
It's hard to tell without seeing you code how it is structured
but if once the user submits the form all you do is show the image by itself, then you can do something like this.
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
If you embed the image into an html page as the result, then your best best would be to change the url of the image on the success page to something like this.
<img src="/path/to/file.php?deg=45" />
Then in the file.php
$deg = $_GET['deg'] + 0; // make sure it is a number
$img= function_render_graph($deg);
// make sure nothing else is out put before this otherwise it will stuff up the header
header('Content-Type: image/png);
imagepng($img);
By using a GET request, rather then a POST request then the image will likely be cached by the browser, so it doesn't need to be rendered each time. (Given that you have a drop list of angles, there must be a limited number of graphs that can actually be drawn)
Draw_Resultant_Prism_Graph (parameters)
{
$img = imagecreatetruecolor(800,750);
....
....
...
the following lines captures the data from output buffer and displays on same screen
***some version of IE have some issues mostly the dumb terminals where IE update is ADMIN
***restricted
ob_start();
header("Content-type: image/jpeg");
imagepng($img);
$output = ob_get_contents();
ob_end_clean();
imagedestroy($img);
echo img src="data:image/jpeg;base64,'.base64_encode($output).'"
user tags around img above and semicolon af
}

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