6

I'm using wkhtmltopdf to download a page and create a pdf, as explaind in here. It all works as expected, but when it reaches the following line:

int read = proc.StandardOutput.BaseStream.Read(buffer, 0, buffer.Length);

It takes a very long time to continue. I've also tried the following:

proc.Start();
string output = proc.StandardOutput.ReadToEnd();
string error = proc.StandardError.ReadToEnd();
proc.WaitForExit();

But the same thing happen on line 2 (proc.StandardOutput.ReadToEnd()).

How can i debug this?

Note: As i'm debugging the whole project, i see that the page that is being called by the wkhtmltopdf process only gets called after the delay. So the delay happens between the OS call and the page download itself.


Edit

So i changed the arguments portion to simply http://google.com - and it executed quite fast.

So the problem is somewhere in my full argument: --stop-slow-scripts --redirect-delay 1500 --cookie MOODLEID_ %25E2%25C8%2513E%25BD --cookie ASP.NET_SessionId mg3mfaisk45zn5y1aql0glbb --cookie .ASPXAUTH FEA3D10032E211EDB330E967D8F19A324A912AD792219CA281451AA809F1B580B25466DAF81DE30DF07252FF13F0888C88ED4DB5871579F1DA3EBAA447CBF860131F6FF00A763C9207CE13BCB143301E49A2B00EEBBF1F4AE14F109DF1BE8D3B39C43478B99E4C686C3849D1D51DDBFA3D2E871691BAB8346FE5195D867F88F662F72E40 http://localhost:8404/Relatorio/GeralEstaticoPDF?id=15&pesq_cod=0&IncluirParciais=True&NomesImagens[0]=3fd5b6a2-76a4-470b-af99-93500cc90431.png&NomesImagens[1]=c1c86234-c338-4285-9d25-5069be36a213.png&NomesImagens[2]=4562f9a3-72aa-452c-9736-6d0ba65f59ce.png&NomesImagens[3]=dbc54344-6248-4ee3-9e43-9cb3ef89cc67.png&NomesImagens[4]=8c954917-d572-4d4a-b6fd-2b9cd09e8f8e.png -

I need the cookies to preserve the session.

If i run this directly on command line (but putting in a file name instead of -) it runs fast as well.


Edit 2

So the problem is definitely the cookies, if i remove them it execute fast, BUT it only give me the login screen on the pdf, since it isnt preserving the session.

Community
  • 1
  • 1
ariel
  • 15,620
  • 12
  • 61
  • 73

3 Answers3

4

PhilJ's answer is correct but, I wanted to add a possible solution for any future visitors.

In short:

Adding the [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)] attribute to the controller (doing the printing) resolved the issue for me.

I'm actually using the Rotativa library which in turn uses wkhtmltopdf.

(It would probably be helpful to add a Rotativa tag to question?)

dario
  • 5,149
  • 12
  • 28
  • 32
Quinton Smith
  • 2,550
  • 18
  • 17
  • Hello, I hope you read this. I use asp.net without MVC and wkhtmltopdf. I have the same session problem, so I added `EnableSessionState="ReadOnly"` to the page for print, but still login screen. If I add the sesion cookie id as parameter for wkhtmltopdf, my applications just hangs as PhilJ said. Do you know something else I can do? – Anton Epikhin Apr 15 '16 at 10:17
  • Ok, it doen't hang, it takes like two minutes :( – Anton Epikhin Apr 15 '16 at 10:34
  • Perfect answer! – Tejasvi Hegde Mar 06 '17 at 18:19
3

It seems to be the session cookie that causes the issue, the forms auth cookie is fine or any other cookie for that matter. The reason is thus:

"To avoid data collisions at the session store and unexpected session-state behavior, the SessionStateModule and SessionStateStoreProviderBase classes include lock functionality that exclusively locks the session store item for a particular session for the duration of the execution of an ASP.NET page."

http://msdn.microsoft.com/en-us/library/system.web.sessionstate.sessionstatestoreproviderbase%28VS.80%29.aspx

PhilJ
  • 101
  • 1
  • 1
  • 6
1

I'll add my experience since I ran into it in two different projects.

My options for generating the pdf are:

string[] options = new string[] { 
            "--cookie", "ASP.NET_SessionId", System.Web.HttpContext.Current.Session.SessionID,
            "--cookie", ".ASPNETAUTH", HttpContext.Current.Request.Cookies[".ASPNETAUTH"].Value};

Then I add: EnableSessionState="ReadOnly" to caller's page header and it works.