views:

601

answers:

3

These browsers (Windows versions) correctly print a web page which has a canvas overlay over an img tag image:

IE6, IE7, IE8, Firefox, Safari

But Chrome (up-to-date as I type this at version 3.0.195.27) and Opera (up-to-date as I type this version 10.0 build 1750) don't. Chrome ignores the canvas rendering entirely. Opera renders the entire canvas as a white rectangle, obscuring the image behind it.

I know the canvas tag is rather new, and that printing web pages is always dicey. How should I handle this? Are there any settings I'm missing to make this work? Is there an "about to print" event I can catch from JavaScript to at least warn the user?


Update: It's even weirder than it seems. The combined image (canvas overlaying an img) prints correctly on my Brother laserjet but not on my hp inkjet).

So the current situation is that most browsers print correctly to my inkjet, but Chrome does not. Opera screws up on both printers.

Is this all down to printer drivers and how various browsers communicate with them?


Update: I notice that Google Docs builds a PDF for printing.

A: 

I would suggest to save the page as PDF and try printing. A PDF document to print should be WYSIWYG. If the PDF renders right, any printer should print it fine.

Joy Dutta
So how do I save a page as PDF? Can I do that programmatically?
Nosredna
Sorry if I assume that you have a mac. Whenever you try to print from a browser in mac, you will see a PDF menu at the bottom left. You can save as PDF using one of the options there.
Joy Dutta
On most Linux distributions like Ubuntu you can print to PDF as well. I think Adobe Acrobat Reader on Windows also comes with that option but I'm not sure.
richard
I'm looking for a way that a user of my site can do it, not something that's OS-specific. Good idea, though.
Nosredna
+1  A: 

I wonder, if instead of rendering to a <canvas> element: it might print correctly if you were to render to an off-document canvas object and set a CSS style on a <div> to have a background-image set to "url("+myCanvasObject.toDataUrl()+")".

Also, you could just set the src attribute of a new <img /> element to that same myCanvasObject.toDataUrl(). This should be handled better than the method I mentioned above..

JasonWyatt
An interesting idea. :-) I bet IE, at least, won't eat that, though. Maybe Chrome and Opera would.
Nosredna
You're probably right about IE, could be worth a shot though!
JasonWyatt
I added another possible solution, this one might be more accurate/useful.
JasonWyatt
+1  A: 

You can use PHP's PDF functions to create PDF's programmatically. It's really easy!

To create a new PDF:

$pdf = pdf_new();

Open the file:

pdf_open_file($pdf, "pdfName.pdf");

You can set some variables such as the author:

pdf_set_info($pdf, "Title", "pdf Title here");

Now you'll need to use the pdf_begin_page() function:

pdf_begin_page($pdf, 595, 842);

these are the dimensions for A4.

content

And close the file with:

pdf_end_page($pdf);
pdf_close($pdf);

For a complete reference, take a look at PHP's PDF manual.

richard
That's cool. Assumes I get all info to the server, of course, including a history of all canvas operations (which I'd have to mimic somehow on the server).
Nosredna