Reputation: 9994
Suppose I develop a web page using the cool Google visualization API, and it does everything the user wants. They can the parameters, look at the graphs, and print the page to get a reasonable-looking report. All good.
Now suppose I want to do the same thing server-side. For example, say we need a set of report generated at a specific time of day, printed to a PDF and emailed to a manager. It's not a user-initiated action, so we don't have a user's browser or their printer. We have a URL that would render the report if we had a browser, and that's it.
Is there a good way to do this server-side? Is this just foolish? Has anyone done anything like that before? Do any of the major browsers have APIs that might provide such functionality?
Keep in mind too that it's not just static HTML; probably javascript will be running first to shift the DOM around.
I know we could implement a whole different reporting engine on the server side to do this, but that will (a) generate reports that look a bit different, and (b) require me to build/maintain two sets of functionality.
Instead, I'd be happy if I could just render the page / pages I want in an invisible server-side browser and print it to a PDF (let's mostly ignore that step - I know any number of PDF printer drivers that could do this).
I don't really want to do it ugly either - i.e. by starting a browser process and then sending keystrokes directly to the window either - that's just bound to fall apart with a slight nudge. The only related question I found had an answer like that.
Any advice appreciated!
Upvotes: 1
Views: 1473
Reputation: 31842
Now I recommend wkhtmltopdf library. It uses WebKit API to render page, executes JavaScript and supports modern CSS. Everything can be exported to PDF. There is .NET wrapper called TuesPechkin.
Old answer
You can write simple WinForms application that will make use of System.Windows.Forms.WebBrowser and print method. If printer's pdf driver is configured correctly, you'll have file on drive ready to send. You can schedule it to run on specific part of the day.
Upvotes: 1
Reputation: 1835
Ancient thread but I stumbled on it while researching alternatives for this exact problem. I've so far had pretty good results with Essential Objects ASPX to PDF. I can't say I've used it enough to say that it's the ultimate answer, but so far it seems to do everything I want to do. The above link is to a help page, and it has sample code. You just reference a few DLLs, add some references and call code similar to:
ASPXToPDF1.RenderAsPDF(false);
There are options for page size, headers/footers, margins, etc. So far seems to work well.
Upvotes: 1