PHP - Capturing the moment of print?

PHP - Capturing the moment of print?

Author
Discussion

BliarOut

Original Poster:

72,857 posts

241 months

Wednesday 1st September 2010
quotequote all
I need to log every time someone prints from my app. Currently we use CSS which works a treat, but the class that I'm now using to create PDF prints doesn't support background-image

{{{
<?php
if ($BeenPrinted =='1') {
$BGImage = "background-image:url(logit.php?RecordNo=$RecordNo&DBForLogit=db04)";
}
else {
$BGImage = "background-image:none;";
}
?>
}}}


Can anyone think of another way of capturing the moment someone prints?

Famous Graham

26,553 posts

227 months

Saturday 4th September 2010
quotequote all
I'm assuming they're printing from the browser's menu rather than a dedicated print button in the app?

(haven't you mentioned this before on here, btw? Sounds familiar)

ETA - how about just making it a regular <img >, ie <img width='1' height='1' src='logit.php?etcetc' >

The tricky bit was setting the 'BeenPrinted' var.

Edited by Famous Graham on Saturday 4th September 12:05

BliarOut

Original Poster:

72,857 posts

241 months

Saturday 4th September 2010
quotequote all
I have and the above suggestion has been working admirably smile I just need to find something else that I can trigger when the @media print event occurs. I do have a print button, but it could be circumvented with <ctrl>p

So how would you call an image solely at print time? I can only use a limited set of inline CSS at this juncture.

Famous Graham

26,553 posts

227 months

Saturday 4th September 2010
quotequote all
BliarOut said:
I have and the above suggestion has been working admirably smile I just need to find something else that I can trigger when the @media print event occurs. I do have a print button, but it could be circumvented with <ctrl>p

So how would you call an image solely at print time? I can only use a limited set of inline CSS at this juncture.
Ah, I thought you could put a 1x1 img in the html. Then change the src via PHP&CSS, like you were the background-image.

ETA - ignore that. Can't change the src in CSS. Could have sworn that you could. Nuts.

Edited by Famous Graham on Saturday 4th September 15:26

Famous Graham

26,553 posts

227 months

Saturday 4th September 2010
quotequote all
Javascript? Call a function when BeenPrinted is 1, passing the RecordNo and DBForLogit values as arguments?

Bah, ignore that as well, course that's got to be in the html.

Edited by Famous Graham on Saturday 4th September 16:58

The Excession

11,669 posts

252 months

Saturday 4th September 2010
quotequote all
Jeez you've been struggling with this for a few years now Rob, I wish I had an answer for you but I don't....

So, just thought I'd say 'Hi' and let you know that I'm thinking of you.... wink



hehe

BliarOut

Original Poster:

72,857 posts

241 months

Saturday 4th September 2010
quotequote all
It worked for a bit but I want PDFs now biggrin

it's nearly working again, honest wavey

I just need to call logit.php with a few variables at print time only. It's an interesting conundrum biggrin

Famous Graham

26,553 posts

227 months

dreaddan

282 posts

212 months

Sunday 5th September 2010
quotequote all
IE has onbeforeprint and onafterprint

You could use diffrent CSSs with the print one being a page that logs the print.

<link rel="stylesheet" type="text/css" media="print" href="printCSS.php?id=myId" />
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />


BliarOut

Original Poster:

72,857 posts

241 months

Sunday 5th September 2010
quotequote all
I use onbefore and after to fire a warning if the doc has already been printed smile

Unfortunately the php class I'm using only supports inline CSS frown

Thanks for the suggestions so far, keep 'em coming thumbup

Keith Lard

197 posts

218 months

Sunday 5th September 2010
quotequote all
Is it necessary that it logs an actual successful print event or merely generation of a print dialogue box. Onafterprint is only triggered when sent to queue/cache not actually printed and is IE specific.

Even though your print media css file is a clever workaround, surely a log would be triggered just using print-preview, so not %100 accurate.

If you use FPDF and a bit of JS you can use a single link to dynamically generate the pdf file, open it in a new window, trigger a print dialogue box and log each time the url has been called. This would give a pretty accurate log of printing.

Are you developing it locally or is there a link to the site, so we can have a gandersmile

BliarOut

Original Poster:

72,857 posts

241 months

Sunday 5th September 2010
quotequote all
No can gander unfortunately... And Preview doesn't trigger it. Well I don't think it does but I won't know until tomorrow scratchchin

The CSS works very well as it's cross browser and you can't circumvent it with <ctrl>p

Keith Lard

197 posts

218 months

Sunday 5th September 2010
quotequote all
BliarOut said:
No can gander unfortunately... And Preview doesn't trigger it. Well I don't think it does but I won't know until tomorrow scratchchin

The CSS works very well as it's cross browser and you can't circumvent it with <ctrl>p
Pretty sure it will as its still calling the background image just for the preview.

What are you using to set the value of $BeenPrinted ?


BliarOut

Original Poster:

72,857 posts

241 months

Sunday 5th September 2010
quotequote all
I keep a value of Printed in the DB with a default value of No. As soon as logit is called it updates the dB with a value of Yes.

KISS biggrin

Famous Graham

26,553 posts

227 months

Monday 6th September 2010
quotequote all
So you can only change the bold bits? And only replace/amend it with another style?

{{{
<?php
if ($BeenPrinted =='1') {
$BGImage = "background-image:url(logit.php?RecordNo=$RecordNo&DBForLogit=db04)";
}
else {
$BGImage = "background-image:none;";
}
?>
}}}

BliarOut

Original Poster:

72,857 posts

241 months

Monday 6th September 2010
quotequote all
Famous Graham said:
So you can only change the bold bits? And only replace/amend it with another style?

{{{
<?php
if ($BeenPrinted =='1') {
$BGImage = "background-image:url(logit.php?RecordNo=$RecordNo&DBForLogit=db04)";
}
else {
$BGImage = "background-image:none;";
}
?>
}}}
The only bit that really matters is logit.php?RecordNo=$RecordNo&DBForLogit=db04) When that's called it updates the record to indicate it's been printed then puts an entry in the audit table. I need to somehow get that to run at print time. A print button is an option but that can be side stepped.


idea How about approaching it the other way? Have an @media screen CSS and set a div to not display that somehow calls logit.php? Could that work and if so how would I call it?

Famous Graham

26,553 posts

227 months

Monday 6th September 2010
quotequote all
Yeah, I understand that you need to call logit.php and pass the parameters.

What I was trying to determine was just how much freedom you have to do that.

ie if you're free to simply run some php in that if statement, then you can just use http-get or fsockopen (if http-get isn't installed), and not have to worry about CSS at all.

ETA - cos from that code you've posted, you've already handled the actual print event, thus setting $BeenPrinted, no?

Edited by Famous Graham on Monday 6th September 12:31

BliarOut

Original Poster:

72,857 posts

241 months

Monday 6th September 2010
quotequote all
$BeenPrinted is set by logit.php which won't currently run so the status of the record's printed field always remains at 'No' irked $BeenPrinted is a throwback to some old code which I no longer really need but haven't tidied as yet.

It could really do with altering to
{{{
$Printed = $row_Recordset['Printed'];

if ($Printed=="No") {
$gen = ' (Original)';
} else {
$gen = ' (Duplicate)';
}

}}}

What about calling some ajax to run logit.php attached to the onbefore and onafterprint events. It means i will only be able to print from IE but that's not the end of the world... scratchchin

Keith Lard

197 posts

218 months

Monday 6th September 2010
quotequote all
Can you give us a quick overview of the application?

What's the nature of the documents your outputting. Are users having to pay to view/print these documents?

I'm sure with a bit more info someone here can help you.


BliarOut

Original Poster:

72,857 posts

241 months

Monday 6th September 2010
quotequote all
Keith Lard said:
Can you give us a quick overview of the application?

What's the nature of the documents your outputting. Are users having to pay to view/print these documents?

I'm sure with a bit more info someone here can help you.
It's just a document but I'm afraid I can't give you an overview as it's CIC. Basically the first time it prints it will say original but from then on it will say duplicate and each time it is printed it is audited....