Forum How do I...?

Save pdf automaticaly

JorenWillems
Hi

Now's the case that when you convert a pdf, it opens or you can save it. Is there also a way that when converting a webpage in ASP.NET, the created pdf is saved automaticaly without interaction of the client?
In my application, the purpose is that a client created a pdf and that the system automaticaly sends the pdf to the presscenter
mikeday
In general websites can't automatically save files on client computers, as this would be a huge security risk. You can prompt the user to save it instead of displaying it inline though, using the content-disposition HTTP header.
JorenWillems
I normally use this (works fine):

p.Convert("http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Docubis/Templates/" + 
            webform + "?Template=" + Server.UrlEncode(template) + "&Document=" + document + "&guid=" + guid, 
            this.Response.OutputStream);


But I saw that you can also replace the outputstream with een pdfoutput(string), so I tried this:

p.Convert("http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Docubis/Templates/" + 
            webform + "?Template=" + Server.UrlEncode(template) + "&Document=" + document + "&guid=" + guid, 
            "C:\\Docubis\\" + doc.Name + ".pdf");


This wel get the following error:



Sometimes even my pdf is saved to that path but without the images. --> Strange...
mikeday
Calling Convert() in this way will save the PDF file to the server (the same machine that Prince is running on) with the specified filename. Is this what you want?
JorenWillems
Yes, I try it but it throws the error

*edit: I tried to save the file to "My Documents" and it worked although the error is still poped up :s
mikeday
Sorry, I can't really make sense of what you are trying to do. :)

The ASP page can call Prince, generate a PDF file, and save it on the server. What do you want it to return back to the browser? Is the Acrobat error message popping up in the browser, or when you try to load the file on the server?
JorenWillems
Ok
I'll try to expolain exactly

- A client clicks on a button --> Prince converts a certain webpage to PDF en saves it to C:\Docubis\name.pdf
- The error popups, but the pdf is created anyway...

string absPath = Server.MapPath("~") + "\\";

        string PrinceExePath = "C:\\Prince\\Engine\\bin\\prince.exe";

        Prince p;
        p = new Prince(PrinceExePath);

        string template = Request.QueryString["Template"];
        string document = Request.QueryString["Document"];
        string webform = Request.QueryString["Webform"];
        string guid = Request.QueryString["guid"];

        BLDocument blDocument = new BLDocument();
        BLTemplate blTemplate = new BLTemplate();

        Document doc = blDocument.getDocByName(document);
        Template temp = blTemplate.getTemplateByName(template);

        Response.ContentType = "application/pdf";

        p.AddStyleSheet(absPath + "Templates\\css\\fonts.css");
        p.AddStyleSheet(absPath + "Templates\\css\\" + temp.Size + "_" + doc.PDFProfile.ToLower() + ".css");

        p.SetLog(absPath + "log.txt");

        p.Convert("http://" + Request.ServerVariables["SERVER_NAME"] + ":" + Request.ServerVariables["SERVER_PORT"] + "/Docubis/Templates/" + 
            webform + "?Template=" + Server.UrlEncode(template) + "&Document=" + document + "&guid=" + guid, 
            "C:\Docubis\name.pdf");  
mikeday
If the error pops up on the client, it is because you are specifying a content-type of application/pdf, so the browser is expecting to get a PDF response, but you are not sending a PDF file to the browser, you are saving it to disk on the server. So the browser gets nothing, or some HTML, or something else entirely, anyway it does not get a PDF, which is why Acrobat complains.
JorenWillems
Oh, ok
Well I fixed it by putting after the convert a response.redirect --> There's no error and my pdf is created anyway :D