Rank: Newbie Groups: Member
Joined: 8/12/2015 Posts: 1
|
Hi
I'm just evaluating your MVCtoPDF component for our company. One of our requirements is to combine several ActionResults of our MVC Web-Application into one PDF. The page orientation and size may vary between the different views. As I read from other posts it is not possible to change page-size or orientation within one pdf-object. So what we try to do is to convert each view according our needs to a pdf and combine all pdf's in the end.
Currently we do that with the following code:
public ActionResult Booklet(int id) { EO.Pdf.PdfDocument doc = new EO.Pdf.PdfDocument();
EO.Pdf.HtmlToPdf.Options.PageSize = new System.Drawing.SizeF(EO.Pdf.PdfPageSizes.A4.Width, EO.Pdf.PdfPageSizes.A4.Height); EO.Pdf.HtmlToPdf.ConvertUrl(Url.Action("ActionA", "ControllerA", new { @id = id }, Request.Url.Scheme), doc);
EO.Pdf.HtmlToPdf.Options.PageSize = new System.Drawing.SizeF(EO.Pdf.PdfPageSizes.A4.Height, EO.Pdf.PdfPageSizes.A4.Width); EO.Pdf.HtmlToPdf.ConvertUrl(Url.Action("ActionB", "ControllerA", new { @id = id }, Request.Url.Scheme), doc);
EO.Pdf.HtmlToPdf.Options.PageSize = new System.Drawing.SizeF(EO.Pdf.PdfPageSizes.A3.Height, EO.Pdf.PdfPageSizes.A3.Width); EO.Pdf.HtmlToPdf.ConvertUrl(Url.Action("ActionC", "ControllerC", new { @id = id }, Request.Url.Scheme), doc);
//Save the PDF file var ms = new MemoryStream(); doc.Save(ms); ms.Position = 0;
return new FileStreamResult(ms, "application/pdf") { FileDownloadName = "test.pdf" }; }
Basicly we create full-path URL with the Url.Action-Method and pass it to the HtmlToPDF.ConvertUrl-Method. Generally the code works fine, but since the called Actions are only visible to logged-in users the only output visible in the PDF is the login screen. It seems that the current active login-session is not valid when the ConvertUrl-Method is calling the passed URL.
Is there another way to call the Action-Methods without loosing the login-session or do we have to use a total different approach?
Any help would be very appreciated.
Regards, PHomberger
|
Rank: Administration Groups: Administration
Joined: 5/27/2007 Posts: 24,229
|
Hi,
This is normal. When you use CovertUrl, it is treated as a separate convesion thus does not share your session. The only way you can get the current session data is through MVCToPDF, which unfortunately only creates a single result PDF file since the result comes from a single conversion. In your case, you must use multiple conversions since you wish to have different page settings for each section. As such you must use additional schemes to "login" you in.
There are several ways for you to do this:
1. Use additional query string to pass user information. For example, you can pass your current user name as an argument to your controller --- you will definitely want to encrypt your argument in this case so that it is not possible for outside party to fake such an Url. The benefit of this method is that you are in total control over everything --- from providing encrypted additional query string argument, to properly decode it and "log in" the user. The downside of this method is it requires more additional coding;
2. Capture and duplicate your authentication cookies. You must capture at least two cookies: ASP.NET authentication cookie and ASP.NET session id cookie (you can get both from your Request.Cookies collection). Once you have these two cookies, you can pass them to the HTML to PDF converter through HtmlToPdf.Options.Cookies collection. This will carry the two cookies to your server and make your server believe that you are already logged in. The benefit of this method is that it is very little coding, but you also have little control over the process and if things go wrong it's hard to troubleshoot;
3. Use a HtmlToPdfSession object. A HtmlToPdfSession object allows you to simulate a log in by loading the log in page, fill in the log in information and then load and convert another (or multiple pages). The down side of this is that it is highly dependent on the design of your login form and you will also need the user's password;
Hope this gives you enough information to continue. Please feel free to let us know if you still have any questions.
Thanks!
|