How to "print" to a paper in PHP? - php

What I mean is, lets say I have a content:
"Stackoverflow is the best, bla bla bla..."
I want to print the content in a paper, not computer monitor, how to do that?
I saw a lot of CMS using php, have a print icon, and able to print it? (Well, I am not creating a CMS, just wondering how the printing works in php)

Those CMS are probably just calling the JavaScript method window.print to pop up the print dialog:
<span onclick="window.print()" class="pseudo-link">print this document</span>
The rest is then handled by the browser and operating system.

A better way to do it is like this:
Print Me
Adding the return false; to the onclick event will stop the browser following the link, and using the <a> tag with the href will cause the link cursor to appear on mouseover. Without it there would just be an arrow cursor which doesn't always make it obvious it's a link.

Do you mean printing from the Web server or from the client? If from the client window.print() in JavaScript will do the trick- http://www.javascriptkit.com/howto/newtech2.shtml.
I ask because I have seen Web based systems that actually do the printing from the server!

Printing on a client browser cannot be done by php. Its done by javascript.
<form>
<input type="button" value="Print This Page" onClick="window.print()" />
</form>
Its best to describe a print css for the page.

Use javascript for this
window.print();
window.print(): Opens the Print Dialog to print the current document.

Related

Find and store all innerhtml of div to php variable

I’m trying to store the content of a div to a variable.
Example:
<div class="anything">
<p>We don't know the content of this div</p>
</div>
I want to search for <div class="anything"> and store everything between opening and the end tag.
We also want to avoid using absolute pathnames, so that it only searches the current HTML/PHP file for this div where the code is present.
Is this possible with PHP, or is this only possible with JavaScript ?
PHP is not that intelligent. He doesn't even know what he says.
PHP is a server-side language. It has absolutely NO clue about what the DOM (ie. what is displayed in your browser's window) is when it delivers a page. Yeah I know, PHP rendered the DOM, so how could it not know what's in there?
Simply put, let's say that PHP doesn't have a memory of what he renders. He just knows that at one particular moment, he is delivering strings of characters, but that's all. He kind of doesn't get the big picture. The big picture goes to the client and is called the DOM. The server (PHP) forgets it immediately as he's rendering it.
Like a red fish.
To do that, you need JavaScript (which is on the client's computer, and therefore has complete access to the rendered DOM), or if you want PHP to do this, you have to retrieve an full-rendered page first.
So the only way to do what you want to do in PHP is to get your page printed, and only then you can retrieve it with an http request and parse it with, in your case, a library such as simpleHtmlDom.
Quick example on how to parse a rendered page with simpleHtmlDom:
Let's say you know that your page will be available at http://mypage.com/mypage.php
$html = file_get_html('http://mypage.com/mypage.php');
foreach($html->find('div.anything') as $element)
echo $element->src . '<br>';
you probably need a combination of those.
In your Javascript:
var content = document.getElementsByClassName("anything")[0].innerHTML();
document.getElementByID('formfield').value(content);
document.getElementByID('hiddenForm').submit();
In your HTML/PHP File:
<form id="hiddenForm" action="path/to/your/script">
<input type="hidden" name="formfield" value="" />
</form>
In the script you defined in the form action:
if(!empty($_POST)){
$content = $_POST['formfield'];
// DO something with the content;
}
Alternatively you could send the data via AJAX but I guess you are new to this stuff so you should start slowly :)
Cheers!
steve
You could use JS to take the .innerHTML from the elements you wan and store them in .value of some input fields of a form and then use a submit button to run the PHP form handling as normal. Use .readOnly to make the input fields uneditle.

Automatically Print webpage with PHP

I have a php script that takes an invoice number and generates a PDF invoice with all relevant information, when the script has finished the PDF is then displayed on the screen in the users browser. I am curious to know how I can then automatically print the PDF? I realize that there probably isn't a way with php or javascript because of security/spam issues.
Unfortunately manually printing the page is not really an option because the users currently have two printers, in the current Access based system one invoice is sent to the colour and two invoices are sent to the black and white printer. This is all done automatically, but now due to certain circumstances I would like to use PHP as a large part of the system is PHP based already.
I have thought about using a Linux based machine and trying to use a python webserver to get the PDF and print it, but I have no idea what this would require or how to do it. Any feedback would be great!
IMO your best option, given what you've said, is going to be to use Linux to print it.
wget http://1.2.3.4/invoice.pdf
pdf2ps invoice.pdf invoice.ps
lpr -Pcolor invoice.ps
lpr -Pbw -#2 invoice.ps
Something like that, perhaps, should work.
Try this
php pdf library available
http://php.net/manual/en/pdf.examples-basic.php
http://sanjoyinfoworld.blogspot.in/2012/03/how-to-generate-pdf-in-php.html
refere this sites
Try to this code on java script
Click to Print This Page
You can set it to print off of an image:
<A HREF="javascript:window.print()">
<IMG SRC="print_image.gif" BORDER="0"</A>
And yes, you can set it to trigger off a button:
<FORM>
<INPUT TYPE="button" onClick="window.print()">
</FORM>
you can't getting any idea view this sites
http://www.htmlgoodies.com/beyond/javascript/article.php/3471121/Print-a-Web-Page- Using-JavaScript.htm
you can do with vbscript try this
<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>
<form>
Printing webpage without prompt window. As of now it's possible with VBScript(IE).
<br/>
<button onclick="Print()">Print Now</button>
<br/>
</form>
<script language="VBScript">
Sub Print()
OLECMDID_PRINT = 6
OLECMDEXECOPT_DONTPROMPTUSER = 2
OLECMDEXECOPT_PROMPTUSER = 1
If DA Then
call WebBrowser1.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
Else
call WebBrowser1.IOleCommandTarget.Exec _
(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","","")
End If
End Sub
</script>

How to dynamically change images on a loaded page with a link attached to each images

I see this on most website.whereby once the page is loaded, they have a kinda image advert whereby it changes, maybe it has a set timer or something. And if you hover the cursor around the images, there's always a different link attached to it, that leads you to the page where it has been instructed to.How can i achieve similar thing. Am thinking Php or maybe javascript, but would prefer php.
You can use some javascript.
Let's say, you have this markup:
<a id='ad_link' href="http://url1.com">
<img src='img1_url' />
</a>
And you receive this data from somewhere (for example, from your server via AJAX).
{"link": "url2.com", "img": "img2_url"}
Then you update your ad with this (using jQuery):
function updateAd(ad_data) {
$('#ad_link').attr('href', ad_data['link']);
$('#ad_link img').attr('src', ad_data('img'));
}
Now, you can request new information in a various ways. setTimeout() or setInterval() are easy and probably the most obvious ways.
php is server side language not client side, so if you already have images in browser php can't do anything with it, you can just use javascript (client side) to call to server for images, or another alternative to AJAX is that you will render all images you want to switch between but displayed will be just one and with javascript you will hide that image after some time and reveal another.
html:
<img id="first" src="first.jpg"/>
<img id="second" src="second.jpg" style="display:none"/>;
javascript:
setTimeout('change',3000);
function change() {
$('first').hide();
$('second').show();
}
something like this but better

Open link in iframe and then open another link in same window

I have this code:
echo 'Send map to 2nd screen';
Id like to be able to press this link, which opens the case_util.php in another iframe and then goes to another link in its own iframe.
Sort of like a refresh/going back. However, it is a form, and pressing back retains the data in the form. I dont want this data in the form, hence I'd like it to navigate to the page.
Thanks :)
Syntax issues. document.location is not a function and is deprecated anyway.
Here is a better version
echo 'Send map to 2nd screen';
or easier to read since you want the href in the iframe named second .
...?> <a href="case_util.php?status=green&adr=<?php echo $adr; ?>" rel="nofollow" target="second"
onClick="window.location='create.php'" >Send map to 2nd screen</a>
Best answer:
Don't use iframes.
However if you are determined to use something as silly as an iframe, use something as silly as a meta redirect.
<meta http-equiv="refresh" content="0;url=http://example.com/"/>
;) seriously, just read about AJAX+jQuery instead:
http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/

open window with dynamic content

Is it possible to open a window from PHP that has predefined content? It's obvious how you can open a window from a javascript link that frames an existing page, or just do a target=_blank from a regular a tag that references an existing page. But I am generating a bit of content, and want that content to be opened in a new link (or streamed to the viewer)--
something like (clearly psuedo code!):
$content = "Hello World. <br />Nice to meet you!";
<a href="#" target="_blank" content=$content>Open up!</a>
Is this possible?
Thanks!
Well, the direct answer to your question is that you can't really do it from PHP directly, because it's the browser that's going to open the window. You can, however, have your page open a window, get the document object, and write to it:
var w = window.open("Surprise", "#");
var d = w.document.open();
d.write("<!DOCTYPE html><html><body>Hello World</body></html>");
d.close();
Instead of the simple string "Hello World" of course your PHP script can put together whatever it wants. Additionally, if desired, the Javascript code itself can dynamically generate the content based on page status, form fields, etc.
Note that you can't guarantee that the new window won't be a new tab, which is no different than what happens with "target" in <a> or <form> tags.
edit — oh also: if you try to use window.open outside of code that's running in response to a "click", browsers will probably think you're trying to show a pop-up ad and will block it.

Categories