Welcome Guest Search | Active Topics | Sign In | Register

How to convert SharePoint Online page to PDF Options
Hung
Posted: Tuesday, November 13, 2012 1:43:08 AM
Rank: Newbie
Groups: Member

Joined: 12/21/2011
Posts: 8
Dear EO Support,

I am researching a PDF Solution to convert SharePoint Online page to PDF

The SharePoint Online used HTTPS, and user need to login first. I don't how to let EO.PDF.HtmlToPDF work with "cookies" or any method to connect to SharePoint Online for getting data and converting to PDF

Code:
var url = "https://hphamsp.sharepoint.com/sites/campbell/SitePages/Home.aspx";
var UserName = "thuy@hphamSP.onmicrosoft.com";
var Password = "Password!";

// get cookie
var cookieContainer = new MsOnlineClaimsHelper(url, UserName, Password).getCookieContainer();
var cookies = cookieContainer.GetCookies(new Uri(url));
HtmlToPdf.Options.Cookies.Add(cookies);

HtmlToPdf.ConvertUrl(url, @"E:\PDF\EO.pdf");

Error Message:

EO.Pdf.HtmlToPdfException: Failed to convert Url 'https://hphamsp.sharepoint.com/sites/campbell/SitePages/Home.aspx'.(500:The server encountered an unexpected condition that prevented it from fulfilling the request. This is an error returned by the Web server, not by the HTML to PDF converter. Please try to visit the same Url with your browser to verify whether the Url is valid.)\r\n at EO.Pdf.Internal.dg.a(String A_0, Boolean A_1)\r\n at EO.Pdf.Internal.dg.b(HtmlToPdfOptions A_0, String A_1, Boolean A_2)\r\n at EO.Pdf.Internal.dg.a(bs A_0)\r\n at EO.Pdf.Internal.lo.c.a(Byte[] A_0)

Updated:
After trying "HtmlToPdf.Options.AdditionalHeaders" instead of "HtmlToPdf.Options.Cookies", the error is "more general"

// get cookie
var cookieContainer = new MsOnlineClaimsHelper(url, UserName, Password).getCookieContainer();
var cookies = cookieContainer.GetCookies(new Uri(url));

string cookie = string.Format("{0}={1}; {2}={3}",
cookies[0].Name, cookies[0].Value,
cookies[1].Name, cookies[1].Value
);

/* Example:
cookie = FedAuth=77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+MGguZnxtZW1iZXJzaGlwfDE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ie
);
HtmlToPdf.Options.AdditionalHeaders = new String[] { strCookie };

HtmlToPdf.ConvertUrl(url, @"E:\PDF\EO.pdf");

ERROR:
EO.Pdf.HtmlToPdfException: Failed to convert Url 'https://hphamsp.sharepoint.com/sites/campbell/SitePages/Home.aspx'.
at EO.Pdf.Internal.dg.a(String A_0, Boolean A_1)
at EO.Pdf.Internal.dg.b(HtmlToPdfOptions A_0, String A_1, Boolean A_2)
at EO.Pdf.Internal.dg.a(bs A_0)
at EO.Pdf.Internal.lo.c.a(Byte[] A_0)

And one more important question: Did EO.PDF.HtmlToPDF work on IIS6 and Win2003?
eo_support
Posted: Tuesday, November 13, 2012 8:35:22 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,081
Hi,

EO.Pdf HTMLToPDF is for .NET, so it works fine on IIS6 and Windows 2003.

As for the SharePoint login, you can try to use HtmlToPdfSession object and see if that works for you:

http://www.essentialobjects.com/doc/4/htmltopdf/session.aspx

The 500 error code is returned by your server. So you must look into your server's log to find out why. As for the more generic version of the error message, you might want to look into your event log to see if you can find more information on that. But I would recommend you to look into the 500 error first.

Thanks!
Hung
Posted: Wednesday, November 14, 2012 2:09:12 AM
Rank: Newbie
Groups: Member

Joined: 12/21/2011
Posts: 8
Hi eo_support,

Thank for your feedback. I already tried "HtmlToPdfSession", but only the "Login Page" was rendered

Code:

Code: C#
using (HtmlToPdfSession session = HtmlToPdfSession.Create())
            {
                //Load the log in page
                session.LoadUrl("https://hphamsp.sharepoint.com/_forms/default.aspx?wa=wsignin1.0");

                //Fill the user name and password field with a valid user name
                //and password. Here "username" and "password" are the name of
                //the two form fields for user to enter their user name and 
                //password
                session.Fill("login", UserName);
                session.Fill("passwd", Password);

                //Submit the log in page. Here "login" the name of the submit
                //button in the page. You can think this method as simulating
                //a "click" on that button. This session is logged in after
                //the Submit method returns
                session.Submit("SI");                

                //Now we are logged in, load the "MyProfile.html" page, which
                //can only be accessed after log in
                session.LoadUrl("https://hphamsp.sharepoint.com/sites/campbell/SitePages/Home.aspx");

                //Render this page as PDF
                session.RenderAsPDF(@"E:\PDF\EO1.pdf");
                
            }


Beside, SharePoint Online site was hosted on Microsoft server. So I can't "look into your server's log"
eo_support
Posted: Wednesday, November 14, 2012 9:31:44 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,081
Hi,

We tried to open the page here. We get a blank page. If you can PM us a working Url and a valid test username/password, we can try to look into it from here. The feature is designed to allow you to log into any Web server that logs in through form variables and stores authentications information by cookie.

Thanks!
Hung
Posted: Wednesday, November 14, 2012 10:36:19 PM
Rank: Newbie
Groups: Member

Joined: 12/21/2011
Posts: 8
Hi,

The page url is still valid. Please try

URL:https://hphamsp.sharepoint.com/sites/campbell/SitePages/Home.aspx
You will be redirect to "SharePoint Online Login" page. Please use

Account:thuy@hphamSP.onmicrosoft.com
Password:Password!

("https://hphamsp.sharepoint.com/_forms/default.aspx?wa=wsignin1.0" is the correct "SharePoint Online Login" page)
eo_support
Posted: Wednesday, November 14, 2012 11:05:30 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,081
Hi,

We looked into this and it does not appear HtmlToPdfSession will work. The reason is HtmlToPdfSession simulate a form post but SharePoint login process is more complicated than a single form post. In order to a conversion, you will need to capture and duplicate the cookies. So your original method was correct, except that you need to add the cookies into HtmlToPdfOptions.Cookies collection. We tried to manually capture the cookies using a tool called Fiddler and then send the cookie to the server and it works. Our steps are like this (download and install Fiddler if you do not already have it):

1. User google Chrome to login to your site, make sure you check both "Remember me" and "Keep me signed in". This will create the authentication cookies;
2. Close Chrome browser;
2. Run Fiddler;
3. Open Chrome browser, access https://hphamsp.sharepoint.com/sites/campbell/SitePages/Home.aspx;

Now go to the first request in Fidder, go to "Headers" section, you should see the following cookies being sent to the server:

FedAuth=xxxxx
rtFa=yyyyyy

Copy the content of these two cookies and then write your code like this:

Code: C#
//Create the cookies. You will need to replace this code
//with your MsOnlineClaimsHelper
System.Net.Cookie cookie1 = 
    new System.Net.Cookie("FedAuth", "...contents_of_fedauth_cookie....");
System.Net.Cookie cookie2 = 
    new System.Net.Cookie("rtFa", "....content_of_rtFa_cookie....");

//Use HtmlToPdfOptions.Cookies to carry the cookies
HtmlToPdfOptions options = new HtmlToPdfOptions();
options.Cookies.Add(cookie1);
options.Cookies.Add(cookie2);

//Convert the page with the correct cookies
HtmlToPdf.ConvertUrl(your_url, pdfFileName, options);


Note the difference between our code and your code is that you use AdditionalHeaders but we use HtmlToPdfOptions.Cookies. Please let us know if this approach works for you.

Thanks!
Hung
Posted: Friday, November 16, 2012 2:15:22 AM
Rank: Newbie
Groups: Member

Joined: 12/21/2011
Posts: 8
Hi EO support,

Sorry for the late feedback. I tried your method but it didn't work either, with same error message

Code: C#
var url = "https://hphamsp1.sharepoint.com/sites/test1/SitePages/Home.aspx";
            var UserName = "user1@hphamsp1.onmicrosoft.com";
            var Password = "Password!";

            // get cookie by MsOnlineClaimsHelper
            var cookieContainer = new MsOnlineClaimsHelper(url, UserName, Password).getCookieContainer();
            var cookies = cookieContainer.GetCookies(new Uri(url));
            string cookie = string.Format("{0}={1}; {2}={3}",
                cookies[0].Name, cookies[0].Value,
                cookies[1].Name, cookies[1].Value
                );

            // Didn't work
            //System.Net.Cookie cookie1 = new System.Net.Cookie(cookies[0].Name, cookies[0].Value);
            //System.Net.Cookie cookie2 = new System.Net.Cookie(cookies[1].Name, cookies[1].Value);

            // Try with the real cookie get from Fiddle
            System.Net.Cookie cookie1 = new System.Net.Cookie("FedAuth", "77u/PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48U1A+MGguZnxtZW1iZXJzaGlwfDEwMDNiZmZkODM0MjgwZWZAbGl2ZS5jb20sMCMuZnxtZW1iZXJzaGlwfHVzZXIxQGhwaGFtc3AxLm9ubWljcm9zb2Z0LmNvbSwxMjk5NzU1OTAyNDQ1NzE5MTMsRmFsc2UsZ3NMRHJjQWNVKzJXM0tDN0JyaTBwUGlPU3A1THRaRFdaOVhySFdmTlZyb0NPQkR3aEtwN1liQmtKVHR2bks0bi9Vb3N4WmRxVjcybml6RFZ5U0VCcjl2RVVQWkd1TUZWbXd1dCt6MGNWRHkxdUR1ZkdrNzhMRlpia3gySHNaUXJ5MytlMHhwSFpMdHQ1dVQ2NkF4SW1SV3VYUHRtYTdCR1V2UTY3TS9tL3RNTjdvRCtzZU9HMlErbkhsV");
            System.Net.Cookie cookie2 = new System.Net.Cookie("rtFa", "A62ZLnrXsxm0Lr4sVbkHJFazWxzREjI7vpRk4Z/khy53mZk87+2Qz2eIU4kSJgG2/QS7yYgSxpb5tTRUYQRJlQuGYOeHQ6t04NUPSg54/k3wa3IU6NX2M0/JT4PkQ0mqAhpyLk+T6KiGEyH2/VcnWS9gDbZExJL6AK8JdILLmijx/wGy01SWjQuTkhob7AnT+/rT+yqYOWaMoa/vjVqOVtTFrvRGB36LkyVH1UCQ83ZzdYKzOk0f0gkM7MxH4gqGS76pChYjbHRyG9PFHakOF3IDDnI8BH52dnzzEz/jEcRhoOsCVSkALzWkOTOWK8YTCBGN1Ws9OEvp/V7HFkXZ5N2F3oT6cCg6+0g2apW1BbV6X2e7VKbhgf/s6yk7Xxo+IAAAAA==");
            HtmlToPdfOptions options = new HtmlToPdfOptions();
            options.Cookies.Add(cookie1);
            options.Cookies.Add(cookie2);            

            HtmlToPdf.ConvertUrl(url, @"E:\PDF\EO.pdf", options);


Error Message
EO.Pdf.HtmlToPdfException: Failed to convert Url 'https://hphamsp1.sharepoint.com/sites/test1/SitePages/Home.aspx'.(500:The server encountered an unexpected condition that prevented it from fulfilling the request. This is an error returned by the Web server, not by the HTML to PDF converter. Please try to visit the same Url with your browser to verify whether the Url is valid.)
at EO.Pdf.Internal.dg.a(String A_0, Boolean A_1)
at EO.Pdf.Internal.dg.b(HtmlToPdfOptions A_0, String A_1, Boolean A_2)
at EO.Pdf.Internal.dg.a(bs A_0)
at EO.Pdf.Internal.lo.c.a(Byte[] A_0)


My SharePoint Online Trial site was expired and disposed yesterday. So please use new SharePoint Online site

Site:https://hphamsp1.sharepoint.com/sites/test1/SitePages/Home.aspx
Account:user1@hphamsp1.onmicrosoft.com
Password:Password!
eo_support
Posted: Friday, November 16, 2012 8:37:35 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,081
Hi,

We tested your code and it works fine for us. Make sure you are running the latest EO.Pdf. Other than that we do not know what else to tell you.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.