PDA

View Full Version : Auto print page without the print dialog opening.


smitho
01-22-2008, 10:27 AM
I have found a way to auto print a page without the print dialog appearing. My problem is how would I track it.

Here is the situation.

1.User uploads a file and enters details about the file.
2.File may be sent to one or more people/stations.
3.So that people don't have to sit and watch a screen all day to see if files have been uploaded I'll use the auto print process that will print new files as they come in.

I've got most of the process up and running I just can't think of how I could do this. In my database that holds all the info about the uploaded files I've allowed for a printed column.

Would one option be to have a table of the database auto refresh every 10 secs and by counting the number of entries determine that new files have been add then send off the file to auto print?

If it is any suggestions on how to code this?

domedia
01-22-2008, 12:45 PM
When you say 'sent to one or more workstations', how does this happen?

davidj
01-22-2008, 03:18 PM
you could poll in AJAX every few mins and then display the results

smitho
01-22-2008, 08:58 PM
In the upload process I've given the user a check box choice of about 12 possible destinations to send their file. I've also created 12 tables in my database that hold the info for each of the destinations. I've created several destination screens with a dynamic table. This works well in that every time a file is uploaded to any on the destinations the dynamic table (when refreshed) updates with the new file. Here is where I get stuck.

I have not looked into AJAX as I've focused on learning PHP and MySQL the last 2 months. Is AJAX similar to PHP or JavaScript?

davidj
01-22-2008, 10:14 PM
AJAX (Asynchronous JavaScript and XML) can be written to allow you to run a script to get db results without the need to refresh the page

see my tut

"dynamic dropdowns in AJAX"

you could change that to query the db and return a result to the page

domedia
01-23-2008, 12:40 PM
You still have to define 'send' :)

davidj
01-23-2008, 01:09 PM
the send is a manual process

the receipt is automatic

smitho
01-24-2008, 01:46 AM
Thanks guys before I get into the AJAX (watched the tut davidj and you make it look easy) but I need to get my head around this first.

Using the dwc database i've made a couple of changes.

CREATE TABLE IF NOT EXISTS `car_make` (
`make_id` int(11) NOT NULL auto_increment,
`make` varchar(20) default NULL,
`message` tinyint(1) default NULL,
`Good` tinyint(1) default NULL,
`Bad` tinyint(1) default NULL,
`Ugly` tinyint(1) default NULL,
PRIMARY KEY (`make_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
The php table looks like this:


<?php
//connect to the database
$db_connection = mysql_pconnect ('localhost','name','password');
$db_status = mysql_select_db ('dwc', $db_connection);
if (!$db_status){ echo "Data_base Connection Failed";
exit;}

//query the database to display the table.
$query = sprintf("SELECT * FROM car_make");
$result = mysql_query($query);
$rowmake = mysql_fetch_array($result);
$totalrowsmake = mysql_num_rows($result);

//////////////////////////////////////////////////////////
//this is the part I'm having trouble with
if (isset($_POST["form1"])){
mysql_select_db('dwc', $db_connection);
mysql_query("UPDATE make SET (Good, Bad, Ugly) WHERE (Good=$_POST[Good], Bad=$_POST[Bad], Ugly=$_POST[Ugly])");


mysql_query($insertquery) or die('Error, insert query failed');
}
//////////////////////////////////////////////////////////
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF'];?>">
<table width="0" border="0">
<tr>
<td><div align="center">CAR MODEL</div></td>
<td><div align="center">MESSAGE</div></td>
<td><div align="center">GOOD</div></td>
<td><div align="center">BAD</div></td>
<td><div align="center">UGLY</div></td>
<td><div align="center"></div></td>
</tr>
<?php do { ?>
<tr>
<td><div align="center"><?php echo $rowmake['make']; ?></div></td>
<td><div align="center">
<input type="checkbox" name="message" id="message" value=""<?php if (!(strcmp($rowmake['message'],1))) {echo "checked=\"checked\"";} ?> />
</div>
</td>
<td><div align="center">
<input type="checkbox" name="good" id="good" value=""<?php if (!(strcmp($rowmake['Good'],1))) {echo "checked=\"checked\"";} ?> />
</div></td>
<td><div align="center">
<input type="checkbox" name="bad" id="bad" value=""<?php if (!(strcmp($rowmake['Bad'],1))) {echo "checked=\"checked\"";} ?>/>
</div></td>
<td><div align="center">
<input type="checkbox" name="ugly" id="ugly" value=""<?php if (!(strcmp($rowmake['Ugly'],1))) {echo "checked=\"checked\"";} ?>/>
</div></td>
<td><div align="center">
<input type="submit" name="submit" id="submit" value="Submit" />
</div></td>
</tr>
<?php }while ($rowmake = mysql_fetch_array($result)); ?>
</table>
</form>
</body>
</html>

Firstly is it possible to update the check box for (Good, Bad, Ugly) for only the row selected by clicking on the submit button? Would anyone be able to suggest how to link the check box to the database?

The message checkbox would be automatially checked when a message box was cleared by the user. I'll work on this after I get the link between the form and database going.

davidj
01-24-2008, 09:39 AM
in your update statement...

this wont work

Good=$_POST[Good]this is how you should do it



$Good=$_POST['Good']; //<< declare var and parse

update table ...... Good=$Good // << use the parsed var in your update statement

smitho
01-26-2008, 10:49 AM
Not sure if this problem is a php or just basic html coding but here goes.

What I'm trying to do is create a web page for print (not print friendly page). The page is designed to fit A4 so at 72 dpi the measure would be 595px wide by 842px high. I create a table and set the height and width to match these measurements it should print out on any A4 and be fine. The main issue I'm having is with the image on the page. The page is job sheet the top displays some details from the database and underneath is a image of the file. To be able to have an image good enough to print the dpi should be at least 150dpi I'm using 200dpi. This means the images will be wider than 595px need to print on a A4 page.

So is there a way to use an image place holder (linked to database) and have it fit a set size?
This would mean any image larger would proportionally reduce to fit within the set measurements. Any suggestions?

davidj
01-26-2008, 07:20 PM
is this a new question?

smitho
01-27-2008, 11:53 AM
You could say it's a new question. It's part of the auto print process. First I need to get the page looking correct then get the auto print checking that each new entry has been printed.

I found that using tables would probably be the best way to layout a page all the details at the top and the image underneath. I've been looking at the imagecopyresample option which think should work but there's a slight problem. Using the example that in the manual if i try and add a table the image does not appear. Would I need to create the table in the php code for this to work?

<?php

// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 595;
$height = 600;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}

// Resample
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

// Output
$resample=imagejpeg($image_p, null, 100);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table border="0">
<tr>
<td><div align="center"></div></td>
<td><div align="center">This is a resampled image.</div></td>
<td><div align="center"></div></td>
</tr>
<tr>
<td><div align="center"></div></td>
<td><div align="center">
<?php echo $resample;?>
</div></td>
<td><div align="center"></div></td>
</tr>
</table>
<p>&nbsp;</p>

</body>
</html>
<p>table</p>

davidj
01-27-2008, 01:07 PM
this bit is calling a function (imagejpeg()) and is returning the result into a variable ($resample)

$resample=imagejpeg($image_p, null, 100);where is the function????

smitho
01-28-2008, 10:10 AM
Sorry davidj not sure what you mean but where is the function.

The re sample example is from the php manual. It does what I'm after just trying to apply it to a table layout.

If I change the script back to the original layout the result is a page with the image re sampled all works well but not table is shown. If I change the function to:

imagejpeg($image_p, '/resample.jpg', 100);The image does not display in the browser but a resample.jpg is created.

If I comment out the script and just use

$display='resample.jpg';

....<tr>
<td><div align="center"><img src="<?php echo $display;?>" alt="" name="pic" id="pic" /></div></td>
</tr>....The table displays with the correct image.

Do I need to break this process into 2 parts?
Why isn't the table displaying even when the php is working?
Would it be possible to get the re sample script to display the image in a table without having to create a file resample.jpg?

davidj
01-28-2008, 02:23 PM
sorry my fault

its a php function.

you are doing this ....

<?php echo $resample;?>

what are you expecting to happen here?

are you expecting the image to appear?

smitho
01-28-2008, 08:50 PM
Ok now I get it. Yes the intent was to re sample the image to the set size then using a set size for the table, populate the top with information from the database and display the image underneath. By keeping the size locked to a set pixel height and width the page should print without a problem to A4 no matter what size the image was in it original state (A2, A3 etc).

The reason I liked this bit of code was the the function just produced the image to the browser without creating an actual file. This is handy because I then so not have to try and delete the 200dpi file after it's printed.

Currently I'm creating the 200dpi jpg with ghostscript from the pdf file that's been uploaded. Using the code in the example I could re sample the image to the size required and unlink the file created by gs and then send the page of to the printer.

Hope this makes sense.

smitho
01-29-2008, 01:13 AM
It seems that the line:

// Content type
header("Content-type: image/jpeg");

causes the table to not appear. Problem is if i comment out that line the image displays as characters not an image.

smitho
01-29-2008, 03:12 AM
I finally pieced a script together that would take the jpg created from the PDF re sample it to a set height/width. The problem I didn't see until I printed some of the files was that the original jpg from the PDF was created at 200dpi when the script resamples the jpg it drops to 72dpi understandable for web but no good for print. I'm going to have to go back and look at what options I have to create a image placeholder and then have the 200dpi image scaled to fit an A4 area.

Any suggestions on how I might do this?
I've tried applying style="zoom:.32;" to the image placeholder which only seems to work on IE.
I've also tried applying width="50%" height="50%" to the image placeholder which works in IE, Firefox but not Safari.

Is there a way to set a table with a specific height and width and place a dynamic image inside that table so that it scales to fit?

smitho
01-29-2008, 04:13 AM
Ok here's the way I got around it.

Using part of the resample example I did the following.

// The file
$filename = 'test.jpg';

// Set a maximum height and width
$width = 595;
$height = 600;

// Content type
header('Content-type: image/jpeg');

// Get new dimensions
list($width_orig, $height_orig) = getimagesize($filename);

$ratio_orig = $width_orig/$height_orig;

if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}


now in the image placeholder I have the following:

width="<?php echo $width;?>" height="<?php echo $height;?>" Now all I have to do is work out how I'm going to determine when I new entry has been made to the database and send a copy to print.

smitho
01-29-2008, 11:22 AM
I'm having a problem working out how to put a loop together and if I should use a for and while loop. I have a column in my database called printed the results are either true or false 1 or 0 . I've set the auto print page to change the printed option to true once it's been called for that id. I can do a query to determine which entires are still set to 0. Could someone guide me as to how I would check the column and for each result that is set to 0 execute the auto print page.

smitho
01-30-2008, 12:01 AM
Got the auto print to work but not sure if its the best way to do it.

This is the Javascript I'm using to call a popup that prints the page then closes the window.

<script type="text/javascript">
function myPopup() {
window.open('printticket/printticket.php?recordid=<?php echo $row_print['id'];?>', 'window_name', 'width = 620, height = 800');return false;toolbar=1;
}
</script>
At the starting body tag I used this to check the printed column and send off pages that have not been proofed:


<?php
$result=count($row_print['printed']);
if($result>=1){
do{?>
<body onload="myPopup()">
<?php } while ($row_print = mysql_fetch_assoc($print));
mysql_free_result($print);}?>
The page has a META refresh of 30sec.

I'm going to look at the AJAX process that davidj suggested but in the mean time this should be ok shouldn't it?

davidj
01-30-2008, 05:38 AM
i would think so