2

Hi,

I am converting HTML to pdf through wkhtmltopdf library, here session is behaving very differently that if we set the session when our link is called then it works fine but our previously set session is not getting?

exec('C://"Program Files"//wkhtmltopdf.exe ' . 'http://localhost/test.php?a=351' . ' ' . $file_name . '');

Problem:

Session set in test.php is available in this page when printing the page. But if we set the session in some previous page i.e test2.php then that value is null here in test.php

Is any idea?

Suleman Ahmad
  • 2,025
  • 4
  • 28
  • 43

2 Answers2

12

Though answered is already accepted and its little late, I am still adding the answer to help other just for reference. Using session id we can create the pdf from html.

Command is

 wkhtmltopdf --cookie 'sessionid' 'typpotvp1ha1mdssvnfg548yhkwo5j4q' 127.0.0.1:8000/posts/report ./report.pdf
MaNKuR
  • 2,578
  • 1
  • 19
  • 31
  • Thanks a lot. That works! You just have to replace `sessionid` by the configured [session.name](http://php.net/manual/en/session.configuration.php#ini.session.name) (usually defaults to `PHPSESSID`). – xfra35 Feb 17 '16 at 06:30
  • Hello, I have the same problem, how is your server side script, the mine at beginning is like this: session_start(); if (!isset($_SESSION['login'])) { header('Location:../../../index.php'); } and does not work yet, then wkhtmltopdf prints the page :../../../index.php and not the required page – Alexander Ceballos Sep 24 '19 at 16:01
3

Because it's how sessions works, wkhtmltopdf.exe creates another session, another connection to your script, and it's normal that you won't get values from another session.

You can pass variables as get parameters via GET or, you can store them in some permanent location, like file or database.

Aurimas Ličkus
  • 9,886
  • 4
  • 24
  • 26
  • I have a lot of data and changes required to do just like this method, Is their any settings/hacks that wkhtmltopdf.exe takes our session? – Suleman Ahmad Aug 10 '12 at 07:16
  • 2
    You can pass session id via params, on test.php `session_id($_GET['session_id'])`; Passing session id through url is not secure. More: http://stackoverflow.com/questions/827910/passing-session-id-via-url – Aurimas Ličkus Aug 10 '12 at 07:26