Possible Duplicate:
Convert HTML to PDF in .NET
In our applications we make html documents as reports and exports. But now our customer wants a button that saves that document on their pc. The problem is that the document includes images. You can create a word document with the following code:
private void WriteWordDoc(string docName)
{
Response.Buffer = true;
Response.ContentType = "application/msword";
Response.AddHeader("content-disposition", String.Format("attachment;filename={0}.doc", docName.Replace(" ", "_")));
Response.Charset = "utf-8";
}
But the problem is that the images are just links an thus not embedded in the word document.
Therefore I'm looking for an alternative PDF seems to be a good alternative, does anyone know a good pdf writer for C#? One that has some good references and has been tested properly?
I would opt for creating a PDF file on the server. There are many products that do so but you should research the one that works best in your case considering the following:
I would not rely on Word format for that as PDF will give you some more guarantee that it will be readable in the future.
Also, the option of embedding hard links to the images don't seem a good idea to me. What if the user wants to open the document and the server is not accessible?
You have a bigger problem... saving the file generated is the prerogative of the browser. How the browser deals with any particular file stream, even when you set the content type, is entirely up to the browser. Your best bet is probably to use something like ABCpdf to convert the HTML/images into a PDF. I've had pretty good luck with their software, and they have decent support. Of course, this is a third party tool you'll have to install. Without doing that, your next best option is probably to create a zip of the HTML with images and other files (CSS, javascript?)... but that is going to take quite a bit of back-end logic.
Some browsers have this feature built-in. You could ask your users to use that. :)
open source pdf stream generation .net assembly: http://sourceforge.net/projects/itextsharp/
once you get the hang of it, you'll never use any 3rd party #%$^# or clog up your server doing IO's and taking up space for a temporary file ever again.
You would have to give the images absolute link to some avaible location on the internet.
Once the doc has been loaded into Word, saving the 'HTML' doc as a MSWord one, should include the images (or perhaps an option?).
ExpertPDF does a decent job of converting HTML to PDF (including images). Internally, it uses a hosted copy of IE to render the HTML before converting it, which means this component won't work with Mono on Linux, and it means IE's quirks are your PDF's quirks. That said, it does a good job of rendering moderately complex layouts, and you can control pagination with CSS page-break-before and such.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With