I am trying to generate some contents dynamically from a PHP page for print. Currently, I am using a file to store contents and then send that file to the printer. Is there a way, I can store contents in a variable or buffer and send to printer directly with out saving them in the file.
My current code:
ob_start();
include 'printcheck.php';
$result = ob_get_clean();
file_put_contents('/var/www/prints/rt/rtorder.txt', ob_get_contents());
// end buffering and displaying page
ob_end_flush();
Print it.
exec("cat "/var/www/prints/rt/rtorder.txt" | lp -d "TSP100");
Related
I am currently attempting to write a web service that generates a QR code for a given string. I am using PHP QR Code (http://phpqrcode.sourceforge.net/) to generate the QR code, like this:
QRcode::png('PHP QR Code :)');
Result: a PNG stream of the QR Code is displayed on the screen.
As this is a void function that outputs directly to the browser, this prevents me from properly returning the image.
I would like to capture the PNG stream with PHP's output buffer and have my web service return this captured image.
I have set up the following test code on a plain php page for testing purposes:
include('/vendor/phpqrcode/qrlib.php');
$scancode = "testcode";
ob_implicit_flush(false); //just in case
ob_start();
QRcode::png($scancode);
$output = ob_get_contents();
ob_end_clean();
echo "--" . $output . "--";
die();
However, when I run the code the buffer does not work as I had planned. No matter what I do, a broken image is always displayed as soon as I call the function. When I remove the output buffer code the QR code is rendered on the screen.
Why is some of my output not being captured?
Edit: related: phpqrcode library return image as a string
Remove header("content-type:image/png");
to get it worked with output buffering.
Later you can echo
header("content-type:image/png"); and your captured output to show the image
I have a list of data compiled from a mysql recordset when I click a button on one of my pages. The data is stored in a variable $list.
It's a site activity log, and the button is a backup button.
Is there any way that I could make it open a SAVE AS dialogue box so I can save that data to a text file on my local comp?
when you click your "back up" button, you should get the user to a new script: this script should take the $list variable from the DB again and format it into a text file, then in order to make it available to the user's browser as a downloadable file, you should use headers (look at http://php.net/manual/en/function.header.php) like this:
<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
// The PDF source is in original.pdf
readfile('original.pdf');
?>
In this case this is a pdf file (example is from the above link). Changing the content-type to the proper mime-type ("Content-Type: text/plain" for example) and setting the right file name, all that you echo will be sent to the browser as an attached file.
If any question, ask :)
after generate that file just set the physical path of that file and throw header so it will be download at your local system
Cheers
I have built an estimating application where I type in some fields hit submit and the data is saved to my database and creates an on the fly estimate with everything that I typed in. Now they issue I am having is I want to save the output page as a file to my webserver for later viewing, but I can only find examples that save all of my code including the ECHO $Field1. How can I save the outputted results to a file. The same results one would see if they right clicked on the outputted page and choose view source. I was hoping to do it only using PHP.
I tried this already.....
<?php
ob_start();
// all your logic and code for displaying
$output = ob_get_contents();
file_put_contents($uniquehtml,$output);
// save output page to the html file
?>
use something like this in your output page
//buffer output
ob_start();
//process form
//your code goes here
//save & flush buffer in a file
$buffer = ob_get_flush();
file_put_contents('buffer.txt', $buffer, FILE_APPEND);
The FILE_APPEND keeps adding output to the same file. Otherwise, create a new file name each time it writes. You could append time() to the file name, for instance.
I want to copy the file http://searchr.us/Testing/web-search.phtml?search=SEARCHED+TEXT to
http://searchr.us/Testing/search/SEARCHED+TEXT.html
How do i do this?
NOTE:The source of http://searchr.us/Testing/search/SEARCHED+TEXT.html should be the same as http://searchr.us/Testing/web-search.phtml?search=SEARCHED+TEXT
Indirectly I'm just saving a query so that I can keep a track of them!
Hope i've get you right: you want to store output of any query into file. You can manage this with output buffering.
In the begining of script wright:
ob_start(); // start buffering
This turns buffering on.
At the eng of page wright something like this ($query is text of query):
$html = ob_get_contents();
ob_end_flush(); // this turns off buffering and sends buffered content to output
$fp = fopen("{$query}.html","w");
fwrite($fp,$html);
fclose($fp);
I am having a php file which executes some code to generate a html file.Its like I m having a form from which some data will be posted to x.php file, which gives a output(a webpage). I want to save that output in a html file.What the most efficient way of doing this.?
EDIT
I want to save it on sever side. Actually the thing is i want to create pdf file for that.. I wrote everything else.Now the thing is i want to save the output in a html page.So that i can convert it into pdf file..
Try something like this:
// Start output buffering
ob_start();
// run code in x.php file
// ...
// saving captured output to file
file_put_contents('filename.htm', ob_get_contents());
// end buffering and displaying page
ob_end_flush();
If you cannot use the ob_* functions, you can also write the form to a variable and then save that variable.
Look at ob functions (see http://php.net/manual/en/function.ob-start.php) that allows you to capture every output (echo, print, etc...) from the page.
using ob_* functions such as ob_get_contents(), a php script can catch it's output.
probably with ob_start and output_callback see http://php.net/manual/en/function.ob-start.php