Automatically Print webpage with PHP - 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>

Related

Changed php code with $(#selector).html doesn't execute, [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
So now i now this was a dumb question but does anyone now the best way to solve it without an ajax request, becaus I think it would be stupid to make a new file for just one line of code
I am making a gallery and I want two types of views one grid-view and a slide view. My slide and my grid view both work but they stand in different files. So I wrote a simple jquery function to load some php code into the html code. So that when I click a button the html code dissappears and there is php code. The jquery funtion works but the problem is that when the code is changed it doesn't recognise it as php code but just as a string.
This is what I see, you can see the text but it needs to execute that code:
Here is the jquery function:
$(document).ready(function(){
$("img.grid_icon").click(function() {
$(".werkvenster").html("&lt?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?&gt");
})
});
I use the &lt and &gt, otherwise the browser sees it as php code it has to execute instead of seeing it as a string.
Here is the html code it has to be in:
<body>
<section class="werkvenster">
<img src="img/website/icon_grid.png" height="30px" class="grid_icon">
<div class="galerij" id="galerij">
</div>
<button class="omlaag" id="galerij_knop">Terug</button>
<button class="omhoog" id="galerij_knop">Verder</button>
</section>
</body>
So everything between the section tags with class "werkvenster" needs to be changed by
<?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?>
When someone clicks on the grid icon.
Thanks a lot in advance and please don't mark this as a duplicate because I have been searching for over an hour to find an answer to this question.
It seems you need some clarification how PHP and JS work.
a) PHP-code is executed on the server-side. all of it. and nothing else
b) The output, completely free of PHP, is sent to the browser
c) JS-code is executed on the client-side. all of it. and nothing else
If you use JS to write PHP-code, it only happens inside the browser, which doesn't know what to do with it. and long after every bit of PHP-execution.
You do NOT write anything back to the server with this.
what you CAN do is to do an AJAX-request to a specific PHP file that returns your desired output. but you can't just mix JS and PHP like that. it just doesn't work.
edit: in response to your edit about how to solve the problem without using AJAX
to put it simply: not at all. you have to get the data from the server, the best way to do this is AJAX (it was made for this and nothing else) and on the server you have to generate the data somehow. and the cleanest way is a new file.
it doesn't recognise it as php code but just as a string
That's because it is a string:
"&lt?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?&gt"
PHP doesn't run in the browser, it runs on the server. By the time the page is delivered to the browser, the PHP code has executed and completed and produced its output. PHP is no longer involved after that point.
If you want to execute code client-side, that's what JavaScript is for. If you need code to execute server-side, the browser will have to make a request to the server to make that happen. A page load, a form post, AJAX, etc. They all take the form of an HTTP request (potentially with some data sent to the server) which invokes a PHP script and receives that script's output as the response.
You could do what you are trying to do without ajax. You just have to create a hidden <section> that contains the php code (which you have to do on the server-side before it gets sent to the browser). And then in the browser, use jquery to get the contents from the hidden <section> (or just swap the sections)
Here is an example:
Server-side PHP:
<body>
<section class="werkvenster">
<img src="img/website/icon_grid.png" height="30px" class="grid_icon">
<div class="galerij" id="galerij">
</div>
<button class="omlaag" id="galerij_knop">Terug</button>
<button class="omhoog" id="galerij_knop">Verder</button>
</section>
<section id="hiddenStuff" style="display:none">
<?php include_once('resources/UberGallery.php'); $gallery = UberGallery::init()->createGallery('img/gallerij/'); ?>
</section>
</body>
jQuery:
$(document).ready(function(){
$("img.grid_icon").click(function() {
$(".werkvenster").html($("#hiddenStuff").html());
})
});

Get data from HTML and save it as a PHP variable

I have started learning php and I have a question.Let's say I have the following html code:
<p id='tobeChanged'>I wil be changed throughout the execution<p>
This paragraph is not static.Its content can be changed from the user with a button which will produce a random number and will replace the paragraphs html.
E.g. from
p id='tobeChanged'>I wil be changed throughout the execution<p>
to
<p id='tobeChanged'>42<p><!--changed with a button-->
Now my question.Is it possible to pass the new produced value to a php variable?If possible i would like a long explanation.
Also i would like not to use forms(if possible).
Thanks In advance
You need to fire an AJAX request on that button click, that will send that value to server making php to read it.
You can do something like this (you need to include jQuery on page):
$.post("/saveVariable.php",{randNum:randomNum},function(data){alert("Data saved successfully");})
At PHP end, you will get the value in
$_POST['randNum']
Maybe that will help.

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.

strategy to split up php output into smaller outputs (ajax, php) (nested ajax calls, ajax within ajax?)

I don't know the correct terminology to search for the solution. Please suggest a strategy to break up the php output into small chunks and pass them stepwise to ajax's responseText.
The project is an ajax webpage that takes a text string (lastname) and passes it to a php program. The php code takes the last name and randomly fetches 3 people with different first names, and puts it into an array. Once that is done, the php code will contact outside servers to retrieve info associated with each name, and output the info to a div of the webpage. The process of getting data from the outside servers is very slow.
This code is basically done, but the whole process takes a very long time to generate the output on the screen. Is there a way (a strategy) to output each step of the php code immediately instead of having to wait for the complete code?
My pseudo php code is like this:
<?
get 3 names; //output this immediately
foreach name { get phone number }
?>
Alternatively, I could get a name and the phone#, and output it immediately before moving to the next name.
Are there php or ajax codes/functions/strategies that would achieve this? Please suggest solutions or search keywords.
Addition/Edit:
Thanks for the suggestions. Is it possible to execute another ajax call after the parent ajax call? I initially went that route, but my testing of nested js/ajax call did not work. It could be due to syntax errors, please look over the code.
The test code in the testajax.php (or testajax.html) file for the ajax call XHR.responseText is
<div id="name" >JAM <div id="numa" >
<br />
<br />text holder >>
<script type="text/javascript">
var pid=document.getElementById("numa").parentNode.id;
alert (pid);
document.getElementById('numa').innerHTML += 'append text>> ';
document.write(' docwrite');
</script>
</div>
</div>
<br />
<br />ending text
If I view the file testajax.php (or testajax.html) directly, I would see
JAM
text holder >> (an alert window) append text>> docwrite
ending text
but if I do an ajax call of the testajax.php file, all I would see is
JAM
text holder >>
ending text
The code inside the <script> </script> tags does not run after the ajax call
can someone explain this, and offer a fix?
TIA
Without knowing the actual code and code-based answer is hard. But, here's an idea.
When you get the three names, return them to the page and display them. Then, for each one, in a different AJAX call, call the phone info. I'm not positive if javascript will make all three calls independently of each other, but that would at least display all 3 names, and then each phone info one at a time.
Edit
Workflow:
Javascript sends a name to php via ajax.
PHP returns 3 names to js
js appends 3 divs to the page, one with each name.
js makes 3 requests to php, sending 1 name per request.
php returns phone info / whatever else to js
js takes info and adds it to the respective div
In theory, yeah, you can call flush() (and ob_flush() as necessary) to ensure output is sent from PHP.
However, the web server may add buffering of its own outside of the scope of PHP (most commonly, if mod_deflate is in use on Apache); and you'd have to be careful about delimiting your response chunks so they're not read by the browser until a chunk is complete.
In any case not all browsers can read the responseText from an XMLHttpRequest until the request is fully complete. So for it to work on all clients, you'd have to try a different mechanism, such as the old-school HTML-iframe-containing-multiple-<script>s.
Summary: it's a bunch of hassle, and perhaps not really worth it. A simpler-to-deploy possibility would be separate AJAX requests for each name.

How to "print" to a paper in 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.

Categories