Class or function to automatically generate PDF and Print - php

Anyone know any Javascript or PHP function to generate PDF and print (printer) automatically.
Excuse my ignorance, I searched on google about it and can not find sufficient documentation.
Many many Thanks Guys

What are you generating your PDF from? I presume that what you want to do is generate a PDF from e.g. a form submission, then print it on the user's computer?
You cannot print from PHP (well there are horrible ways of doing it, but don't) but I doubt it would help you even if you could - it would be printed on the server side, and I imagine you would be wanting to print on the client (i.e. browser) computer.
You can generate PDF's in PHP (have a look at FPDF) and send them to the browser, and you can print a web page in javascript, but to combine the two would be tricky, if it is possible at all. You certainly can't do it without prompting the user.
If you were to generate the PDF, then open it in an iframe, you could maybe call something through javascript to prompt the user with the standard printing options dialog, but that would be as far as it goes. It wouldn't work everywhere, if it worked anywhere, which I somehow doubt.

to create PDFs, theres the great FPDF-library.
printing automatically fortunately isn't possible - just imagine this would be possible and every f***g website could (in addition to the annoying popups and stuff) print out something (advertisements most of the time) on your printer.
EDIT :
if you have control over the clients, you could write a little batch-script like (not tested)
AcroRd32.exe /t %1 printername
and then set pdf-files in your browser to open automatically with this "programm" wich should then print the file without a print-dialogue.
note that you need access to the clients for this and it isn't tested. in theory this works: i did something very similar once to print out labels directly from the browser, but this was a few years ago using WinXP, don't know if this still works on Win7 (or whatever you're using).

Related

PHPExcel - sending to browser issue

I have a PHP script with HTML forms for user to pick several things, after that it communicates with database and prints out the result in the form of a table. Everything is formatted with CSS.
Now I would like to make those results also saved in an xls file, ideally using some sort of a "Save file" button appearing after the script finish its work. For that to work though I have to use PHPExcel write commands in several different parts of the script. But those parts are also responsible for the output on the website.
I have tried using PEAR and recently also PHPExcel and I think I can make it work with saving the file on the server, but I couldn't do it with sending to browser option (so popping up message with 'Save file as...'). When I try to do it that way, it prints out all kinds of gibberish signs on the website and doesn't pop up with save file option at all. I found out this is because I am printing and echoing all sort of things in between the PHPExcel write commands, but I am not sure how to do it differently.
So I guess my question is: Is it possible in PHPExcel or any other library to use its write commands all over the PHP script with all sort of echos and HTML code in between while using the send to browser option?
Thanks you in advance for answers and help.

Convert HTML to Image in PHP without shell

I want the option of converting HTML to image and showing the result to the user. I would be creating an $html variable with PHP, and instead of displaying using echo $html, I want to display it as an image so the user can save the file if they needed to.
I was hoping there would something as simple as $image = convertHTML2Image($html); :p if that exists?!
Thanks!!
As #Pekka says, the job of turning HTML code into an image is the job of a full-blown web browser.
If you want to do this sort of thing, you therefore need to have a script that does the following:
Opens the page in a browser.
Captures the rendered page from the browser as a graphic.
Outputs that graphic to your user.
Traditionally, this would have been a tough task, because web browsers are typically driven by the user and not easy to automate in this way.
Fortunately, there is now a solution, in the form of PhantomJS.
PhantomJS is a headless browser, designed for exactly this kind of thing -- automated tasks that require a full-blown rendering engine.
It's basically a full browser, but without the user interface. It renders the page content exactly as another browser would (it's based on Webkit, so results are similar to Chrome), and it can be controlled by a script.
As it says on the PhantomJS homepage, one of its target use-cases is for taking screenshots or thumbnail images of websites.
(another good use for it is automated testing of your site, where it is also a great tool)
Hope that helps.
This is not possible in pure PHP.
What you call "converting" is in fact a huge, non-trivial task: the HTML page has to be rendered. To do this in PHP, you'd have to rewrite an entire web browser.
You'll either have to use an external tool (which usually taps into a browser's rendering engine) or a web service (which does the same).
It is possible to convert html to image. However, first you must convert to PDF. see link
You may have a look at dompdf which is a php framework to convert a html file to a pdf.
use WKHTMLTOPDF. works like a charm. it converts to any page to PDF ..
a jpeg can be obtained by performing later operation.
http://code.google.com/p/wkhtmltopdf/

Print multiple uploaded document from a web application

I want to know if this is possible to print all attached pdf from an object.
(i'm afraid not, but better asking ;D)
context:
User uploads multiple pdf file in his account, then later, he clicks on "print all the pdf attachements", instead of downloading and then one by one clicking on "print"
Thanks.
There are several ways you can go about this with either just JS or a combination of PHP and js (and perhaps server-side programs that can be called by PHP):
Have the target document be a php file which programatically join all the PDFs into a single document (for instance, with pdftk) and then outputs the joined file with PDF headers. This file would be loaded into an iframe which you could call window.print() on with JavaScript.
Use javascript's window.print() function to target several hidden iframes each of which has one of the PDFs loaded in it. The major drawback of this approach is that it will generate multiple print dialogue boxes.
Both of these approaches rely on the user having the necessary settings and plugins to actually load PDFs within the browser window, but if they don't they'll be prompted to download the large file/multiple files.
This can not be done with PHP. Why? Well, PHP is a server-side script. It has no possibilities to access the user's printer.
To my knowledge there is no solution with javascript either. Mike made a good point with the printer dialog being accessible through a javascript command but that is where it stops. This is a good thing though, would you want a page you enter to be able to use your printer without your permission?
Best way is probably to concatenate all the pdf into a single document server side then allow the user to download and print that. This is what manuscriptcentral and probably other online academic peer review systems do.

Get a screenshot of a page [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Website screenshots using PHP
I have an application where people watch a stream of content (a video stream) and need to click a button each time something happens (suppose they see a red light).
I want to somehow screenshot the stream at the moment where the user clicks the button.
The problem is that the stream is not mine and I am using an IFrame to another page with the stream. The stream is a flash object.
I need to screenshot the page at the moment of click with the flash content using PHP/Javascript and save it on the server.
I saw something that seems similar to what I need but the solution is using C# and .NET.
Programmatically get a screenshot of a page
EDIT:
Idea, if anyone can explain it best here, how could I do that using a plugin/java applet or something that the user might install when entering the site. (The easier the better).
You can't get a screenshot client-side, and certainly not with PHP. PHP runs on your server, not on the client.
The only way to do this would be to write a browser plugin of some sort, or utilize Java.
See this post: Take Screenshot of Browser via JavaScript (or something else)
After a request to view your website has been made by a browser, your PHP code is run and generates the content of the page. By the time your user sees the page, the PHP has finished running, and the content of the page is static as far as that is concerned (although of course can be changed by other means, like ajax or javascript).
That's why this isn't possible at all with PHP, and it's not possible by another means for security reasons (for example, it could reveal any client-side scripts the user is running, etc.).
The only way a screenshot could be taken would be to render it yourself. The only way I can think of offhand would be to get the time on loading of the page, then the time when the button is pressed, and render the page for this long, but this is by no means foolproof and not really a valid suggestion.
I'm afraid that you'll probably have to redesign this portion of your site.
Since you say that the stream is a Flash object, you could simply read it into a Flash enviroment. Flash would allow you to do what you want - saving screenshots, or, better said, images generated by that Flash, i.e. still frames. However, I am not sure if Flash would allow you to make a screenshot of the display incl the OS enviroment.

Generate PDF automatically and automatically upload to server

I am working on improving a daily tasks which could be sped up if what I need exists. It could save me about 2 hours of work a day if it can be done. My current way of generating reports is long. Here is my current flow.
I click generate report and the report is automatically generated using PHP, CSS, & HTML.
I work on a Mac, so in order to save the report as a PDF, I click on "print". The print window appears and I then click on "Save As PDF".
I save this report to my desktop. I then upload it to my server.
The 3 steps above is my current process. What I would like to be able to do is this:
I click generate report and the report is automatically generated using PHP, CSS, & HTML.
If the report looks good, I click on "save". The save button will automatically generate a PDF and will upload it to my server.
Is this possible? If so, can someone point me in the right direction. I prefer to do this with PHP, but if there's another way I can do this, that is fine as well.
If you'd like to learn how to do this, and well, there are great resources available to you.
In fact, Welling/Thomson's PHP and MySQL Web Development has two chapters that are perfect you. Chapter 19 deals with files and Chapter 32 is entitled "Generating Personalized PDF Documents." I recommend you give it a thorough read.
If you encounter problems along the way, then you should come back here and post them in discrete components.
you could use MPDF, a great tool to generate pdfs.
check: http://www.mpdf1.com/mpdf/ download the pack and check the examples. :D sorry about the english
PHP doesn't have a built-in PDF generator, although there are libraries you can add that will do it. Creating the PDF by outputting the correct formatting is non-trivial.
Instead, you can use an HTML to PDF converter - I've used them on Linux, such as wkhtmltopdf, so they should work okay on a Mac, too. You would just generate the report as normal, but output it to a file rather than the browser. Then run the converter on the HTML and presto, a PDF version.

Categories