How to use Phantomjs cookie API?

I am trying to use rasterize.js from the exmaple phantomjs folder to capture a webpage in a PDF. The problem I encountered is that the webpage requires user login. Then I saw that there is a cookie option for Phantomjs. The website is powered by Django, if that matters.

So, I entered the site courageously, opened firebug and typed document.cookie in firebug. I got the cookie settings and placed in cookie.txt.

 [http://localhost:7000] csrftoken: f3da886168fae33b840e7f6c93240dff sessionid: 27e90c3214b0ec94dadc739665724708 django_language: en 

Then I used rasterize.js to create the pdf, as shown below:

 phantomjs --cookies-file=cookies.txt examples/rasterize.js http://localhost:7000/reports /tmp/report.pdf 

But the result is the journal page.

+7
source share
1 answer

Have you tried automating the login process to PhantomJS by including a cookie in it? This way phantomjs can authenticate the user like a browser using cookies.

Another option would be to check requests using a proxy server, such as a violinist, comparing a request made from firefox that works, and a request made from phantomjs that is not authenticated. The information in the headline should tell you what you are missing.

You can add cookies using the phantomjs function:

http://phantomjs.org/api/webpage/method/add-cookie.html

+6
source

All Articles