Forum How do I...?

Create an actual PDF file and open or show in browser

weeza
Hi Guys,

I've downloaded the trial version of Prince and installed it on my web server.

I can't seem to work out how to make the following work. I need to be able to create an actual PDF document which is viewed in the browser or opened straight after the conversion. The user can then choose to print/save the PDF.

Even though the below code renders a PDF in a browser, if you right-click the page, it's actually still a .aspx page.

Prince prince = new Prince("\\\\[SERVER]\\Prince\\Engine\\Bin\\Prince.exe");
Response.Filter = new PrinceFilter(prince, Response.Filter);
Response.AddHeader("Content-Type", "application/pdf");

I want to be able to do what this http://www.princexml.com/2005/forms/ Prince example does. If I can open it up in a browser, even better, but opening up in an Adobe window is a good start.

Thanks for your help. :)
mikeday
What happens exactly when you right-click? You may need to use the Content-Disposition header, which can specify whether to view the PDF inline in the browser window, or prompt the user to save it.
weeza
Thanks for your response.

I have modified the code to that below which now prompts the user to download the file, but I'm still running into an issue on iPhones.

Your http://www.princexml.com/2005/forms/ example gives the iPhone user an option to open the PDF in iBooks (or other relevant apps). My modified code, however, just opens the PDF in the Safari browser and does not give the user the option to open it an app.

Are you able to provide me with a code example of what you did to make your http://www.princexml.com/2005/forms/ work.

        Directory.CreateDirectory("[SERVER PATH]" + this.Session.SessionID.ToString() + "");
        string inFile = "http://ggsbbint.ggs.vic.edu.au/Error.htm";
        string pdfFile = "[SERVER PATH]" + this.Session.SessionID.ToString() + "\\Oops.pdf";   
        Prince prince = new Prince("[SERVER PATH]\\Prince\\Engine\\Bin\\Prince.exe");
        prince.SetLog("[SERVER PATH]\\Prince\\princelog.log");
        prince.Convert(inFile, pdfFile);

        string filePath = "[SERVER PATH]" + this.Session.SessionID.ToString() + "";
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=" + filePath);

        Response.WriteFile(pdfFile);
        Response.End();
        Response.Flush();
mikeday
It's very similar, it just specifies a Content-Disposition header of 'attachment; filename="invoice.pdf"'.

By the way, do you need to write the PDF to a temporary file, or can it go straight to the response output stream? What if you tweak the code like this:
        string inFile = "http://ggsbbint.ggs.vic.edu.au/Error.htm";
        Prince prince = new Prince("[SERVER PATH]\\Prince\\Engine\\Bin\\Prince.exe");
        prince.SetLog("[SERVER PATH]\\Prince\\princelog.log");

        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=\"document.pdf\"");

        prince.Convert(inFile, Response.OutputStream);

        Response.End();
        Response.Flush();

Note that the specified attachment filename "document.pdf" is the filename that the browser will prompt the user to save the PDF as, so it does not need to include the full path of anything on the server.