Forum How do I...?

Current HTML page to PDF

swsdam
I'm fairly new to .NET programming and very new to Prince...

I used an XSL to create an HTML page that let's my user show or hide elements on the page. I would now like to send my users current HTML page to PDF? How do I do this?

I'm using ASP.Net and C#.
mikeday
The new .NET interface for Prince includes a PrinceFilter class that can be used as a HttpResponse filter by ASP.NET pages. If you read up on how to use HttpResponse filters, you'll find that they can be a convenient way of modifying a page generated by ASP.NET before it is returned to the browser.

Alternatively, if you can capture the HTML output in a string or a MemoryStream then you can feed it to Prince yourself, then either capture the PDF output and write it to the response stream or just make Prince write it directly to the response, but remembering to set the content-type header to application/pdf.
swsdam
I've opted for trying to use the Response Filter as you suggested. So far it gives me back a pdf as a result of a button click. However, it does not seem to be using any of the stylesheets I've passed to it.

I've tried referencing a link to the .css, including an embeded style and adding it as part of the prince interface. It is also not including images in the pdf.

Any suggestions?

Using C#:
public void button_Click(Object s, EventArgs e){
	Prince prince = new Prince("C:\\ProgramFiles\\Prince\\Engine\\bin\\prince.exe");
	prince.AddStyleSheet("C:\\Path\\CSS\\StyleSheet.css");
	Response.Filter = new PrinceFilter(prince, Response.Filter); 
    Response.AddHeader("Content-Type", "application/pdf");
} 


Thanks for any help
mikeday
You may have relative URLs for images or style sheets in your HTML, like this:
<img src="images/logo.gif">

If the URL to the HTML page is http://example.com/page.aspx then the browser will use that as base URL for the page and rewrite the image and style sheet links, so the image URL in the above example will become http://example.com/images/logo.gif

However, when you write the HTML directly to Prince, it doesn't have a base URL, and the relative links can't be resolved correctly. You can specify a base URL for the page using SetBaseURL in the API:
prince.SetBaseURL("http://example.com/page.aspx")

(There are ASP.NET methods for getting the server name and page path, so that you don't need to hard-code it as a string).