Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing rendered content to printer - How can I do this?

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
US
I searched around the forum and came across this thread


I followed every suggestion but it is not working for me.

My objective:
I have written a PHP script which produces a nice looking page filled with content extracted from MySQL tables. I would like to send the rendered content to a printer.

Everything I've tried so far has resulted on printing the code not the rendered page.

For now, I am doing this
Code:
<script type="text/javascript">
	window.print();
</script>
after rendering the page which in turn will open the printer dialog box.

I want to print the document and close it right off PHP.

Thank you all in advance for you assistance!
 
As PHP runs on a webserver and does not even know where its output is rendered, it can not send anything to a printer either, unless that printer is connected to the web server.

If you want to supply something "in print", you could consider creating PDF files (for instance, with the open source fpdf library).

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
Well, if that is the case, I guess I best get back to HPGL as I normally do.

I was hoping I could just render the HTML and flush it out to a printer (which is installed on the server).

Thanks!
 
I was hoping I could just render the HTML and flush it out to a printer (which is installed on the server).

If it is installed on the web server, you may just do this. If you generate the HTML, it should not be that hard to store it in a file also for printing. Such files could then be printed by a CRON job calling a browser with these files, for example.

+++ Despite being wrong in every important aspect, that is a very good analogy +++
Hex (in Darwin's Watch)
 
webdev007, that is the exact same thread I posted alone with my question.

I use HPGL and have been using jpadie approach for months. HPGL is just a bit involved and I figure that I could just render HTML and print it all at once.

Using what is described on this thread, all that prints is the code itself. If I use eval() as metnioned, I get syntaxt errors from it.

I am working on a class to generate HPGL pages. Funny thing is that I have never written a class before :)
 
southbeach, Sorry I did not pay enough attention
guess I did shoot from the hips!
Please, post your class, I'll love to see it
and possibly borrow it from you :)
 
webdev207,

Here is the first draft for the PHP Class to render HPGL code.

The class itself is very simple (I am proud of myself)
Code:
<?

class page {
	var $body;
	var $image;

	function pageImage($datafile) {
		// TODO: Will work on loading images - Actually, PCL converted images ... Need to use MKPCL
		$this->image = file($datafile);
	}
	function pageInit() {
		// Lets initialize the page settings - This is defaulting to portrait.
		// TODO:  Option to set to landscape
		$this->body = chr(27).'E'.chr(27).'&12a0o6d3e60F'.chr(27).'*p0x,0Y'.chr(27).'*c5760x7920y0T'.chr(27).'&a0R'.chr(27).'%0BINSP1IP0,11176,8128,0SC0,2400,0,3300PA0,0;'.chr(27).'%0A'.chr(27).'%0B'.chr(10);
		$this->body .= 'PD;PR2400,0,0,3000,-2400,0,0,-3000;';
	}
	function pageBox($x,$y,$w,$h) {
		// Draw a box or line
		// $x = X coordinage
		// $y = Y coordinate
		// $w = Width of box / line
		// $h = Height of box - Zero (0) if drawing a line
		//
		$this->body .= 'PU;PA'.$x.','.$y.';PD;ER'.$w.','.$h.';';
	}
	function pageText($x,$y,$p,$text) {
		// Place text on document
		// $x = X coordinage
		// $y = Y coordinate
		// $p = Pitch in Points
		// $text = String to place on document
		// TODO: Ability to change font, rotation, ...
		$this->body .= 'PU;PA'.$x.','.$y.';PD;DT#;SD1,41,2,1,4,'.$p.',5,1,6,3,7,4148;LB'.trim($text).'#;';
	}
	function pageClose() {
		// Must be used to close a page 
		$this->body .= chr(27).'%0BPU;PA0,0;'.chr(27).'%0A'.chr(27).'&a0R'.chr(27).'&k2G';
	}
}

?>

Here is the PHP Script I ran to scan through my MySQL table and produce a nice document.
Code:
<?
//
/* Author:  Jose Lerebours		*/
/* Date..:  06/25/2008			*/
//

session_start();
include("HPGL_Class.php");		// Include HPGL Class
include("common/mySQL.php");

$page = new page;				// Start Page Object
$page->pageInit();

$sysid	= $_GET['sysid'];
$module = $_GET['module'];
$number = $_GET['number'];

Open_DB('demo');				// Open DB

/* Search for selected printer */
//$sql = 'SELECT * FROM printers WHERE prtID = ' . $_GET['printer'] . ';';
//$query = DoQuery($sql); $prtrec = mysql_fetch_assoc($query);
//$printer = $prtrec['prtName']; $xbase = $prtrec['prtXbase'];

$printer = "\\\\printServer\printer_name";

if (strtoupper($module) == 'REL') { $title = 'Release Number'; } else { $title = 'Receipt Number'; }

/*                X   Y  Pitch  */
$page->pageText(1550,100,26,$title);
$page->pageText(1750,230,26,$number);
$page->pageText(740,400,32,'Charges Listing');

$page->pageText(120,560,14,'Code');
$page->pageText(320,560,14,'Description / Definition of Charges');
$page->pageText(1280,560,14,'Quantity');
$page->pageText(1700,560,14,'Rate Per');
$page->pageText(1960,560,14,'Charge Amount');

$page->pageBox(0,500,2400,75);
$page->pageBox(310,500,0,2330);
$page->pageBox(1270,500,0,2330);
$page->pageBox(1540,500,0,2330);
$page->pageBox(1950,500,0,2330);

$sql = 'SELECT * FROM charges WHERE chSysID = "' . $sysid . '" AND chModule = "' . $module . '" ORDER BY chDesc DESC;';
$query = DoQuery($sql); $total=0.00; $y=645;
$linesPer = 15; $lineCount=1; $FullDoc='';
while ($row = mysql_fetch_assoc($query)) {
	if ($lineCount > $linesPer) { 
		$page->pageText(1800,3100,16,'Continues ...');
		$page->pageText(900,2980,22,'TOTAL CHARGES $');
		$page->pageText(1900,2980,22,number_format($total, 2,".",","));
		$page->pageClose(); $FullDoc .= $page->body; $page->pageInit(); $lineCount=1;

		$page->pageText(1550,100,26,$title);
		$page->pageText(1750,230,26,$number);
		$page->pageText(740,400,32,'Charges Listing');
		
		$page->pageText(120,560,14,'Code');
		$page->pageText(320,560,14,'Description / Definition of Charges');
		$page->pageText(1280,560,14,'Quantity');
		$page->pageText(1700,560,14,'Rate Per');
		$page->pageText(1960,560,14,'Charge Amount');

		$page->pageBox(0,500,2400,75);
		$page->pageBox(310,500,0,2330);
		$page->pageBox(1270,500,0,2330);
		$page->pageBox(1540,500,0,2330);
		$page->pageBox(1950,500,0,2330);

		$y = 645;
	}
	$page->pageText(120,$y,14,$row['chCode']);
	$page->pageText(320,$y,14,$row['chDesc']);
	$page->pageText(1280,$y,14,$row['chQty']);
	$page->pageText(1610,$y,14,$row['chRate']);
	$page->pageText(2000,$y,14,number_format($row['chAmount'], 2,".",",")); $y += 75;
	$page->pageText(320,$y,10,$row['chDefOfCharges']); 
	$boxY = $y+10;
	$page->pageBox(0,$boxY,2400,0);
	$y += 75;
	$total += $row['chAmount'];
	$lineCount++;
}

$page->pageText(600,2980,22,'TOTAL CHARGES $');
$page->pageText(1800,2980,22,number_format($total, 2,".",","));
$page->pageClose(); $FullDoc .= $page->body;

echo $FullDoc;  //  Show on screen the HPGL Code

ob_start();
// include $filename;
echo $FullDoc;
$contents = ob_get_contents();
ob_end_clean();
///////
$handle = printer_open($printer);
printer_set_option($handle, PRINTER_MODE, "raw");
printer_write($handle,$contents);
printer_close($handle);

?>

Not exactly a big deal but it really gives me a starting point to a fast and easy way to produce my HPGL documents.

Hope you find it useful!

Let me know if you have questions and/or suggestions ...

Regards,
 
That's not your first class :)
I am wondering what jpadie and the other regulars will say
I am not a php guru, many here are!
 
It truly is. I have been developing PHP for a short time and so I am not a GURU myself. Since I developed in Clipper and dBase III and dBase IV back in the early 90s, I grew used to using procedures or UDFs.

So, when I started to learn PHP (not that I am done with that), it was natural for me to stick to using procedures. I then started to see more and more of these classes and I have been banging my head against the wall trying to figure out why are they better than old fashion function. I even posted the question here but got no replies :|

Any ways, I'll love to hear back from you once you give this a try and let me know what you think.

I mind is racing and I am already putting a TODO list to add more functionality.

Regards,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top