Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export from HTML to PDF (C#) [duplicate]

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?

like image 652
user29964 Avatar asked Feb 26 '09 09:02

user29964


5 Answers

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:

  • Computer resources needed to create the PDF. If it's a complex document it may take too long and or slow down the response to other users.
  • Number of concurrent users that will require this same function
  • Cost (there are free solutions as well as heavy weight commercial products).

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?

like image 72
Remo.D Avatar answered Nov 13 '22 07:11

Remo.D


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. :)

like image 35
Bryan Avatar answered Nov 13 '22 08:11

Bryan


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.

like image 31
scott Avatar answered Nov 13 '22 09:11

scott


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?).

like image 2
leppie Avatar answered Nov 13 '22 07:11

leppie


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.

like image 2
Joel Mueller Avatar answered Nov 13 '22 07:11

Joel Mueller