Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Duplicate cookies when printing Header/Footer #1870

Closed
serovpetr opened this issue Jul 23, 2014 · 24 comments
Closed

Duplicate cookies when printing Header/Footer #1870

serovpetr opened this issue Jul 23, 2014 · 24 comments
Labels
Milestone

Comments

@serovpetr
Copy link

wkhtmltopdf Version : 0.12.1.0

Operating System and Version: Windows 7 Enterprise x64 Service Pack 1
Application based on ASP.Net

Description:

I'm trying to generate PDF with footers or headers. The source page, requires form authentication, so I pass cookies to converter using --cookie argument.

Main html converts correctly, but when converter send requests to footer html page, in debugger I see cookies duplicate in request. The result is that by URL number 6 (for examle) there are then 6 times the number of cookies, because of one of cookies is an auth cookie, request quickly becomes too big and IIS blocks it.

Result:

So if Geneated pdf contanins more then 6 ~ 7 pages, converter exits with error code =1, but I can open Result Pdf. After 7 page, instead of of footer i see Label: "Http error 400 Bad Request. Request header too Large "

When i use cookie-jar argument, with username and password, everything works correctly, but I can't use this way because of security context.

@mn4367
Copy link
Contributor

mn4367 commented Jul 31, 2014

Can you post the exact command line used?

@serovpetr
Copy link
Author

This is command line

wkhtmltopdf --zoom 1.5 --no-stop-slow-scripts --page-size A4 --footer-html "http://footer" --footer-spacing 0 --orientation portrait --margin-top 10 --margin-bottom 45 --margin-left 10 --margin-right 10 --enable-javascript --print-media-type --load-error-handling abort --cookie <here i put my authentificate cookie> "http://testPage" test.pdf

This is converter log
converterlog

This is Result PDF footer, in this doument it's repeats after page 7 to the end of doc
pdferror

@serovpetr serovpetr reopened this Jul 31, 2014
@mn4367
Copy link
Contributor

mn4367 commented Jul 31, 2014

What happens, if you put the parameter page before --cookie? Reading the help it could be possible that be explicitly definig a page object the cookie only gets passed for the page URL.

@serovpetr
Copy link
Author

It's working. Footers print correctly, now i have first page wihthout content but with footer, webpage content rendered from second page ant it's correct. My command line is

wkhtmltopdf --zoom 1.5 --no-stop-slow-scripts --page-size A4 --footer-html "http://test" --footer-spacing 0 --orientation portrait --margin-top 10 --margin-bottom 45 --margin-left 10 --margin-right 10 --enable-javascript --print-media-type --load-error-handling abort [page] --cookie <here i put my authentificate cookie> "http://testpage" test.pdf

@serovpetr serovpetr reopened this Jul 31, 2014
@mn4367
Copy link
Contributor

mn4367 commented Jul 31, 2014

If it's working now it can be closed.

@avoinea
Copy link

avoinea commented Nov 27, 2014

No, it's not working. It's a workaround that generates another issue: #1903

@0lukasz0
Copy link

0lukasz0 commented Jan 6, 2015

I got the same issue adding [page] before --cookie helped, but I would consider this as workaround, please reopen and further investigate.

@ashkulz
Copy link
Member

ashkulz commented Jan 8, 2015

What happens if you use

wkhtmltopdf --zoom 1.5 --no-stop-slow-scripts --page-size A4 --footer-html "http://test" --footer-spacing 0 --orientation portrait --margin-top 10 --margin-bottom 45 --margin-left 10 --margin-right 10 --enable-javascript --print-media-type --load-error-handling abort "http://testpage" --cookie <here i put my authentificate cookie> test.pdf

i.e. put the --cookie after the URL?

@serovpetr
Copy link
Author

If you put cookies after target URL authentication will not work.

This is my solution:
Add cookies using that command --no-custom-header-propagation --custom-header Cookie <Name>=<Value> instead --cookie <name> <value> arg. That way exclude cookie duplication in Headers.

@0lukasz0
Copy link

  • putting cookie part after url do not solve "request too long" problem,
  • adding word "page" before cookie crashes wkhtml
  • adding word "[page]" with brackets solves "request too long" but generates blank pages before content,

adding --load-error-handling abort has no improvement

my sample command line arguments:
--page-width 99mm --page-height 210mm --header-html http://localhost:34322/Header --header-spacing 10 --footer-html http://localhost:34322/Footer --margin-top 65mm --margin-bottom 10mm --margin-left 8mm --margin-right 5mm --custom-header-propagation --print-media-type --custom-header "disable-session" "disable-session" --custom-header "printer-authoriazation" "" [page] --cookie ".ASPXAUTH" "" http://localhost:34322/MyPage C:\result.pdf

@TripleEmcoder
Copy link

I have confirmed that the solution mentioned by @serovpetr works, even without fiddling with header propagation. Just relace --cookie <name> <value> with --custom-header Cookie <name>=<value>. The actual bug must only exist in the cookie handling code.

@ashkulz ashkulz reopened this Feb 5, 2015
@tecknicaltom
Copy link

@TripleEmcoder's solution does not solve the problem because the custom header is not used in HTTP requests for content embedded within the page. E.g. if the HTML page has an image that also requires authentication, the request will not have the Cookie header (wkhtmltopdf 0.12.1 (with patched qt))

@ashkulz
Copy link
Member

ashkulz commented May 8, 2015

You can use --custom-header-propagation for achieving that.

@ashkulz ashkulz removed the Invalid label Jul 8, 2015
@ashkulz ashkulz added this to the 0.12.3 milestone Jul 9, 2015
@ashkulz ashkulz added the Verified The issue is verified. label Jul 9, 2015
@mrbokx
Copy link

mrbokx commented Aug 5, 2015

When will this fix / release 0.12.3 be available?

@ashkulz
Copy link
Member

ashkulz commented Aug 5, 2015

@mrbokx: as I can work on this project in my spare time (which has been non-existent lately due to a family emergency), there is no ETA for this fix and the 0.12.3 release.

@ashkulz ashkulz modified the milestones: future, 0.12.3 Apr 14, 2016
@pdas77
Copy link

pdas77 commented Oct 12, 2016

Is the issue resolved in the latest wkhtmltopdf release? We are having same issue with the cookies that are duplicating with the subsequent requests. It works fine with lesser number of urls.

Also the suggested --custom-header (with/without --custom-header-propagation) solution is not working for us.

@laguiz
Copy link

laguiz commented Oct 12, 2016

Any of the workaround works for us.

@mrbokx
Copy link

mrbokx commented Oct 12, 2016

@pdas77 Why not?

  1. Store your cookie in a temporary var (before executing the wkhtmltopdf command)
  2. clear your cookies
  3. execute wkhtmltopdf with the current-header Cookie flag (with the stored cookie values)

For example; a solution for ASP.NET C# with Rotativa plugin

public ActionResult Pdf(string rapport)
        {
            RouteValueDictionary rv = new RouteValueDictionary();
            rv.Add("rapport", rapport);
            foreach (string key in Request.QueryString)
                rv.Add(key, Request.QueryString[key]);
            foreach (string key in Request.Form)
                rv.Add(key, Request.Form[key]);

            // cookie workaround !
            var ASPXAUTH = Request.Cookies[".ASPXAUTH"].Value;
            Request.Cookies.Clear();
            // /cookie workaround !

            return new ActionAsPdf(rapport, rv)
            {
                CustomSwitches = "--footer-html " + Url.Action("Footer", null, rv, Request.Url.Scheme) + " " +
                                 "--header-html " + Url.Action("Header", null, rv, Request.Url.Scheme) + " " +
                                 // customer-header Cookie flag with cookie values
                                 "--custom-header Cookie .ASPXAUTH=" + ASPXAUTH + " " +
                                 "--custom-header-propagation " +
                                 (Landscape ? "--orientation landscape " : "") +
                                 "--page-size A4 " +
                                 "--custom-header-propagation " +
                                 "--header-spacing 5 " +
                                 "--margin-left 15 " +
                                 "--margin-right 15 "
                ,
                FileName = String.Format("{0}_{1}.pdf", rapport, DateTime.Now.ToString("yyyyMMddhhmmss"))
            };
        }

works for us

@pdas77
Copy link

pdas77 commented Oct 13, 2016

Thanks @mrbokx for the suggestion. I already tried using Cookie in custom-header option, I get AuthenticationRequiredError.

Successful Conversion Command: using --cookie option
wkhtmltopdf --javascript-delay 10000 --cookie "Web_SessionId" "egl53navecwyrnmu2ocqqve3" --cookie "site" "pc1" --cookie "Co_SessionToken" "TUie2YVUAW2DBbTcM78giW8faMc9QOYibQP2eFdZH4pAxVkX8Mj_rqVhhcJuYL9jnJduBiwGhHQ3GFJj2S1XKlp41MGgYRwsZe5KqW9AV6E" --cookie "ig" "demoshared_pc1_1" --custom-header-propagation http://xxx.com/url court.pdf

Failed Conversion Command: using Cookie in --custom-header option
/home/asadmin/wkhtmltox-0.12.3/bin/wkhtmltopdf --javascript-delay 10000 --custom-header Cookie "Web_SessionId=egl53navecwyrnmu2ocqqve3; site=pc1; Co_SessionToken=TUie2YVUAW2DBbTcM78giW8faMc9QOYibQP2eFdZH4pAxVkX8Mj_rqVhhcJuYL9jnJduBiwGhHQ3GFJj2S1XKlp41MGgYRwsZe5KqW9AV6E; ig=demoshared_pc1_1" --custom-header-propagation http://xxx.com/url court.pdf

*_Error:
*_Exit with code 1 due to network error: AuthenticationRequiredError

Am I doing it correctly? What I have got feedback that when we use Cookie in --custom-header option, the entire cookie value does not got if it is large. is that true? In our case, very large cookies required to download the page.

@ashkulz
Copy link
Member

ashkulz commented Dec 20, 2016

Looks like #3257 should fix this error.

@ItWorksOnMyMachine
Copy link

using --custom-header worked for me as long as the page being printed doesn't make subsequent ajax calls. Unfortunately, if the page does make subsequent ajax calls, the cookie isn't being sent with that call. I am specifying --custom-header-propagation as well.

@ckogevina
Copy link

I see this issue is resolved in 0.12.5 (#3257). Will you release binaries for version 0.12.5? On https://wkhtmltopdf.org/downloads.html the latest version is 0.12.4...

@sanjay205
Copy link

Adding Cookie in custom header like "--custom-header Cookie" worked for me.

This is my sample command :

wkhtmltopdf --header-html http://localhost:52127/ProposalDocument/_Header --footer-html http://localhost:52127/ProposalDocument/_Footer --custom-header Cookie ".AspNetCore.Antiforgery.5uYu5VgZzcE=abc123;JWTToken=xyz123;.AspNetCore.Cookies=xyzabc123" --no-custom-header-propagation http://localhost:52127/ProposalDocument/ProposalDocument D:\htmltopdfTest\test.pdf

@ashkulz
Copy link
Member

ashkulz commented May 30, 2018

A release candidate is available which includes the fixes made for this issue -- please test and report your findings before the final release.

@ashkulz ashkulz closed this as completed May 30, 2018
@ashkulz ashkulz added Fixed and removed Verified The issue is verified. labels May 30, 2018
@ashkulz ashkulz modified the milestones: future, 0.12.5 May 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests