tags:

views:

3697

answers:

8

I'm generating a coupon based on dynamic input and a cropped image, and I'm displaying the coupon using ntml and css right now, the problem is, printing this has become an issue because of how backgrounds disappear when printing and other problems, so I think the best solution would be to be able to generate an image based on the html, or set up some kind of template that takes in strings and an image, and generates an image using the image fed in as a background and puts the coupon information on top.

Is there anything that does this already?

This is for an ASP.NET 3.5 C# website!

Thanks in advance.

edit: It'd be great if the output could be based on the HTML input, as the coupon is designed by manipulating the DOM using jQuery and dragging stuff around, it all works fine, it's just when it comes to the printing (to paper) it has z-indexing issues.

A: 

There is a very powerful image creation library called GD which I often use with PHP.

I am led to believe there is a wrapper for this library that ASP programmers can use. Try this

adam
A: 

Unless the "other problems" are pretty severe, couldn't you just instruct your users to turn on Background Images when printing?

In any case, I'd default to serving a PDF rather than an image, doubly so since it is intended for print.

nickf
Can I generate a PDF using HTML? If it "Prints" to PDF, it would just cause the same problem I'm having with z-indexing
Shahin
+1  A: 

Try PDFSharp...it's not exactly a "take this HTML and make a PDF" but with a small amout of fiddling you can easily make a PDF out of the info you are using to make the HTML.

Webjedi
small amount may be an understatement there ;D
Ed Woodcock
+6  A: 

What you can do is create an aspx page that changes the response type to be in the format you want and then put the image into the stream. I created a barcode generator that does a similar thing. Excluding all the formalities of generating the image, you'll Page_Load will look something like this:

Bitmap FinalBitmap = new Bitmap();
MemoryStream msStream = new MemoryStream();

strInputParameter == Request.Params("MagicParm").ToString()

// Magic code goes here to generate your bitmap image.
FinalBitmap.Save(msStream, ImageFormat.Png);

Response.Clear();
Response.ContentType = "image/png";

msStream.WriteTo(Response.OutputStream);

if ((FinalBitmap != null)) FinalBitmap.Dispose();

and that's it! Then all you have to do in your image is set the URL to be something like RenderImage.aspx?MagicParm=WooHoo or whatever you need. That way you can have it render whatever you want to specify.

Dillie-O
so this will allow be to dynamically generate an image as a background with text on top, pulled from a database? If so it sounds spiffy!
Shahin
Yes. You can either create the entire coupon as an image in your renderer and return that. Or if you want to lay text on top of it, you'll have to do things a little bit trickier and create a div or a table with the renderer as the background for it.
Dillie-O
whats the magic code??
The magic code depends on how you want to generate your bitmap image. You may have a third party library, you may simply load a file from the server, you may generate your own image dynamically using GDI+. It's up to you. As long as the "magic code" renders a bitmap for you. You'll be set.
Dillie-O
+2  A: 

You can render html to a bitmap using the WebBrowser control in either a winforms or console application.

An example of this can be found here: http://www.wincustomize.com/articles.aspx?aid=136426&c=1

The above example can be modified to run in ASP.Net by creating a new STAThread and performing an Application.Run on it to start a new message loop.

A: 

PHP/Ruby Alternative


If you have accessed this question and are actually looking for soething that will work without Windows, you can try the KHTML library: http://wiki.goatpr0n.de/projects/khtmld

The website has a ridiculous name I admit, but I can assure you it is genuine. Other related pages are: the sourceforge page http://khtml2png.sourceforge.net/

Jon Winstanley
A: 

Just set up your css properly, so that you have a css file targeted at the print medium. It is pretty easy to guarantee that the coupon will always be legible, without worrying about whether they have bg images on or not. Needlesly moving to an image doesn't make any sense, unless there is some reason you don't want it to be machine readable.

UpTheCreek
A: 

I haven't tried to myself, but you should be able to render HTML into an image by using the WebBrowser control and the DrawToBitmap() method inherited from the base Control class.

UPDATE: I tried this myself and there are some caveats. The WebBrowser control doesn't seem to render the web page until the control is show, so the WebBrowser needs to be in a Form and the Form must be shown for the HTML to be rendered and the DocumentCompleted event to be raised.

Lucas