I can't really find a topic on how to print a file with the printer.
First off, I dont't mean print like echo, but a real printer.
I want to have the user click on a button, and than it starts printing the page.
The same as javascript:window.print() does, but than with PHP. Is there such option?
Thanks!
Do you mean printing on server-side or client side?
For client-side you can include a javascript snippet which calls the JS function you mentioned:
<?php
print '<input type="button" value="Print this document" onclick="window.print();" />\n';
?>
For server-side: it is definitely more complicated. You could either find a PHP interface, e.g. to CUPS (the standard UNIX printing system) or call a system program that prints the document. Svish posted a link to a CUPS interface as far as I see an interface to printing in MS Windows (useful only if the server is running under Windows, of course).
Edit: If you expect to find a solution which prints the page PHP outputs rendered by the client browser and but not printed on the client, but on the server -- this should be impossible. I guess you could implement sending the output of PHP the a system program that renders the page itself but it will never look 100% like on the client. Maybe it's acceptable to call a printing program on the server, pass all necessary information to it and it renders some document of its own, e.g. an RTF text file?
PHP works server-side and therefore cannot print anything on the client-side. It can't do anything on the client-side actually, other than via generated HTML, CSS, JavaScript, etc. PHP can talk to a printer though, but it would be a server-side printer as far as I know.
SO, if you want the user to click a button to print a page, on their printer, it would have to be something client-side. For example JavaScript.
As PHP is a server-side scripting language, this wouldn't be possible.
You cannot have the user print a document from his printer using PHP.
If the design of the printed document is undesirable, you can either add a print stylesheet.
Or have a printer friendly page.
For example; passing a get variable called printer will have your php script echo another design of your page.
If you are talking about a server-side printer, you can.
Related
<?php
include 'components/server.php';
Is it possible to make it include server.php for desktops and server-mobile.php for mobile devices?
While technically possible, it's absolutely not the best way of doing things.
Why?
Because PHP runs on the server and only the output of that PHP execution is given to the browser. You would probably be wanting something using javascript which can load and then seamlessly react to the browser conditions, such as screen size and/or dimensions.
If you're trying to change which PHP script is running based on the browser criteria (as mentioned above) this sounds very much like your programming logistics are simply wrong.
If you somehow really do need to change PHP script execution based on end-client (browser) characteristics you could do this by calling a script based on javascript AJAX or using mechanisms mentioned in comments above, but as said, you're almost certainly "doing it wrong".
Alternative
It would be far better to load everything you need in PHP and then pass all of that content to the browser (as output; HTML, CSS, Javascript, etc.) for the Javascript in the browser to then decide which parts of the data it needs to use and ignoring the others.
I am currently trying to load an HTML page via cURL. I can retrieve the HTML content, but part is loaded later via scripting (AJAX POST). I can not recover the HTML part (this is a table).
Is it possible to load a page entirely?
Thank you for your answers
No, you cannot do this.
CURL does nothing more than download a file from a URL -- it doesn't care whether it's HTML, Javascript, and image, a spreadsheet, or any other arbitrary data; it just downloads. It doesn't run anything or parse anything or display anything, it just downloads.
You are asking for something more than that. You need to download, parse the result as HTML, then run some Javascript that downloads something else, then run more Javascript that parses that result into more HTML and inserts it into the original HTML.
What you're basically looking for is a full-blown web browser, not CURL.
Since your goal involves "running some Javascript code", it should be fairly clear that it is not acheivable without having a Javascript interpreter available. This means that it is obviously not going to work inside of a PHP program (*). You're going to need to move beyond PHP. You're going to need a browser.
The solution I'd suggest is to use a very specialised browser called PhantomJS. This is actually a full Webkit browser, but without a user interface. It's specifically designed for automated testing of websites and other similar tasks. Your requirement fits it pretty well: write a script to get PhantomJS to open your URL, wait for the table to finish rendering, and grab the finished HTML code.
You'll need to install PhantomJS on your server, and then use a library like this one to control it from your PHP code.
I hope that helps.
(*) yes, I'm aware of the PHP extension that provides a JS interpreter inside of PHP, and it would provide a way to solve the problem, but it's experimental, unfinished, would be still difficult to implement as a solution, and I don't think it's a particularly good idea anyway, so let's not consider it for the purposes of this answer.
No, the only way you can do that is if you make a separate curl request to ajax request and put the two results together afterwards.
I have a small script that pulls HTML from another site using Javascript.
I want to include that static HTML that gets pulled in a PHP page without any of the Javascript code appearing in the final PHP page that gets displayed.
I tried doing an include of the file with the Javascript code in the PHP page, but it just included the actual Javascript and not the results of the Javascript.
So how would I go about doing this?
You would need to fetch the page, execute the JavaScript in it, then extract the data you wanted from the generated DOM.
The usual approach to this is to use a web automation tool such as Selenium.
You simply can't.
You need to understand that PHP and Javascript operate on different places, PHP on the server and Javascript on the client.
Your only solution is to change the way all this is done and use "file_get_contents(url)" from PHP to get the same content your javascript used to get. This way, there is no javascript anymore and you can still pre-process your page with distant content.
You wouldn't be able to do this directly from within PHP, since you'd need to run Javascript code.
I'd suggest passing the URL (and any required actions such as click event, etc) to a headless browser such as Phantom or Zombie, and capturing the DOM from it once the JS engine has done it's work.
You could also use a real browser, but of course you don't need a UI in your case, and it might actually get in the way of what you're trying to do, so a headless browser might be better.
This sort of thing would normally be used for automated testing of a site (ie Functional Testing).
There is a PHP tool named Mink which can run these sorts of scripts from within a PHP program. It is aimed at writing test scripts, but I would imagine you could use it for your purposes.
Hope that helps.
I heard people said php server site script cannot communicated with html and javascript client-side script. But when I test it, seem php can tell html and javascript what to do. Here is my codes :
<?php if ((isset($username1))&&($username1 == $username2)){ ?>
<div style="position:relative;">
<img src="http://plekz.com/images/layouts/theme.png" onClick="showThemeDiv(); hideThemeTip();" style="margin-bottom:3px; margin-left:1px; position:relative;" onMouseOver="showThemeTip();" onMouseOut="hideThemeTip();" />
</div>
<?php } ?>
I tested it on IE, Firefox and Chrome, all works perfectly. But I still worry code this way will lead to problem when I move all the file to other online webhosting/server... Do I need to put all html and javascript codes into php echo? Or I can just code like this without having any problem in future? Is it standard way to code like this?
What you are doing is just PHP, in your code there is no actual communication between "html and javascript", which is on the client's side. And PHP, which is on the server side.
What people mean with communication is that the user can change something on the webpage dynamically without reloading the webpage. Such a thing can be done using AJAX.
In short, the code you are using will work on any webserver and with any browser.
It's a bad way when html and php code are in one file and much better when separate. But you can code like this and should not put all html to php function 'echo'. It's the same instead you don't need to trigger the php function. And more much understandable to read a code, IMHO.
PHP is simply a scripting language which gets interpreted at runtime whenever a request is made; people say it cannot communicate with HTML/Javascript because it is interpreted on the server side, it is not visible/accessible to the user agent once the page is rendered.
It is however possible to communicate with PHP scripts through AJAX calls.
Is there a PHP function to print something from a web page (inside the browser)?
I was searching in Google but the result is the print() function. Or any ideas, or any web programming language that you can share. I need a code to print a list of subjects from my database. Sorry for this stupid question. I don't know if that is possible.
If you are talking about a web application, you can bring up the print dialog box with JavaScript using the window.print() function.
Without using activex or other such trickery, there is no way to automatically send something to the printer.
This is a good thing, imagine a malicious web site printing thousands of pages on your machine without your consent.
If you are running a command line php that will depend on the operating system.
If the intention is a print preview, where the user can click an “Print this” button and the server will print the document you can use CUPS if you use a Linux/Apple/BSD server. http://www.cups.org/articles.php?L545+TNews+Q
try this, you need to add php_printer.dll, then enable that extension (after restarting apache)
http://in2.php.net/manual/en/function.printer-write.php