views:

4385

answers:

10

Hi,

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?

Best regards,

Sem

+1  A: 

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

leppie
ok but do you know how we can include these images? otherweise the user has to be online all the time to view those images.
Sem Dendoncker
You other alternative is to use the VSTO stuff, but I have never used it before.
leppie
A: 

This seems to have been asked already.

ssg
but not yet answered, therefore I was hoping some other solution is possible. Export to pdf would be great to.
Sem Dendoncker
You could vote up other question and get more number of views.
ssg
+2  A: 

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?

Remo.D
A: 

I've had the same problem but have not got around to solving it, since we settled to exporting an "image-less" document, since it did not have any images in the first place.

However, when searching for the problem I came across this article, about how to export a doc using XSLT. I haven't yet found the time to work on it, but maybe you can have a go at it

Nikos Steiakakis
thx, but I'll go with the pdf approach.
Sem Dendoncker
+2  A: 

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

Bryan
thx, do you know any other pdf writers?
Sem Dendoncker
That's the only one I've had any experience with. In the past I looked for alternatives, but ABCpdf was pretty much the only game in town. It's a bit of a niche tool.
Bryan
I use doPDF which works by emulating a printer. No formatting lost that way.
Kyte
A: 

MS has a new page-description format "XPS" which can be generated simply from WPF the programming model, whether on the server or on a client. There's an XPS Reader app, like the PDF Reader, that allows users to view and print XPS docs. There's a simple API to produce an XPS doc.

Cheeso
A: 

Try PDF Duo .Net component that converts HTML to PDF to/from string | file | url | stream. It is a small but very effictive library that you can use in your ASP.NET application.

Find ASP.NET C# example on its page.

Constantine
A: 

Hi!

First of all you wanted to convert HTML to Word, but has problem that images are just links an thus not embedded in the word document.

And you decided to go with PDF format.

I have a two advices for you:

  1. Convert HTML to Word with help of HTML-to-RTF Pro DLL .Net, it will embed all images inside Word document (RTF format).

This RTF document is fine for printing, can be opened everywhere: MS Word, WordPad, OpenOffice and any RTF reader.

SautinSoft.HtmlToRtf.Converter h = new SautinSoft.HtmlToRtf.Converter();
string rtf = h.ConvertString(html);
if (rtf!=="")
{
   ...
}

So you will not need in PDF format.

  1. If you are still want to use PDF format, use one of these componenst:

PDF Metamorphosis .Net

PDF Vision .Net

Maximus
+1  A: 

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.

Joel Mueller
+2  A: 

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.

scott