1

I know this topis is there already, but none of the answers helped me... I copied wkhtml folder from my HDD onto the server. When I run exec('"../wkhtmltopdf/wkhtmltopdf.exe" "www.estiroad.com/export.php" "C:/EXTREM.pdf"');

nothing happens... Do I type the paths correctly? I mean, I need to type exact path to the wkhtmltopdf according to where I run the exec command from, right? And if I want to save it onto users HDD, I need to use absolute path, right? Strange is, that it gives me no error, just silently does nothing... I found about PHP bindings, but I don´t understand how to use them... Everybody solves that problem only in Linux and thats worthless for me :( Any help will be appreciated.

Michal S
  • 1,103
  • 6
  • 19
  • 31

3 Answers3

2

You should't be putting the quotes around the library.You can catch the output of the command this way:

$commandString = '../wkhtmltopdf/wkhtmltopdf-i386 http://www.estiroad.com/export.php file.pdf 2>&1';
$output       = shell_exec($commandString);

The 2>&1 in UNIX will mean that the output will come through. 1 is stdout. 2 is stderr. Hope this helps.

Or in windows

$commandString = '../wkhtmltopdf/wkhtmltopdf.exe http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));

From the permission issue it looks like you're running the production script on linux. Go to your production server and run

$ uname -a

You'll get something like:

Linux ora100 2.6.5-7.252-smp #1 SMP Tue Feb 14 11:11:04 UTC 2006 x86_64 x86_64 x86_64 GNU/Linux

the x86_64 suggest you're running a 64 bit CPU, if that's the case download the amd64 version of binary, otherwise download the i386 one. Both can be obrained from this url: http://code.google.com/p/wkhtmltopdf/downloads/list

Keep the windows binary. Have you got a config file? if you do make sure you have a switch where you assign your library path to a constant based on your environment.

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    // this is windows server
    define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf.exe");
} else {
    // or the 64 bit binary?
    define('WKHTML_LIB', "../wkhtmltopdf/wkhtmltopdf-i386");
}

Then change your code that initiates wkhtmltopdf:

$commandString = WKHTML_LIB' http://www.estiroad.com/export.php file.pdf 2> output';
print $out ? $out : join("", file("output"));
Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
  • Thanks for advice, now it throws sh: ../wkhtmltopdf/wkhtmltopdf.exe: Permission denied ...I dont understand it, because when I run wkhtml from command line, it works well... – Michal S Aug 06 '12 at 11:19
  • Is your server a windows one? permission denied would suggest to me you might be using a windows binary on linux? – Kasia Gogolek Aug 06 '12 at 11:32
  • You´re right, I checked it and it is Linux... So what should I do with it? :/ Is it still possible to run it ? However I do everything in Win, Linux isn´t even installed on my computer... – Michal S Aug 06 '12 at 11:56
  • I think Im unable to go to the server, for every change in setting even of PHP I need to write to another person from company I work in. But nevermind, I downloaded both: wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2 and wkhtmltopdf-0.11.0_rc1-static-i386.tar.bz2 and copied them in the folder where original .exe file is. Then when I set define like wkhtmltopdf.exe, it still throws permission denied error...If I set it like -i386, it throws "no such file or directory)... Im not sure if I do it right, Im still new into this and when it comes to Linux, Im lost completely. – Michal S Aug 06 '12 at 13:08
  • for linux you should be using `i386` or `amd64`. Don't use the .exe file on linux, they're different operating systems, and the binary for one won't work on another. try it with the amd64 file, if it still says the file doesn't exist check if they do on the linux server and check the permissions for it? run this first to debug the i386 and amd64 files: http://php.net/manual/en/function.is-executable.php – Kasia Gogolek Aug 06 '12 at 13:14
  • How do I check the permissions? – Michal S Aug 06 '12 at 13:36
  • For both files it is false :/ And that no such file error was caused by me - I forgot to type it with .tar.bz2 :) Now it throws the same error like in .exe case: Permission denied... So I assume that I´ll have to change the rights...which I have no idea how to do that :)) – Michal S Aug 06 '12 at 13:48
  • @MichalSirůček tar.bz2 means the file is a compressed file. you have to unzip them first... http://stackoverflow.com/questions/9454929/how-can-i-untar-a-tar-bz-file-in-unix – Kasia Gogolek Aug 06 '12 at 15:03
  • Well I unzipped them with 7-zip, now I have two files without extension, but the error stays the same...Permission denied... Also the test for executability says the files are not executable... – Michal S Aug 07 '12 at 06:28
  • chmod them on the server then chmod a+x file_name – Kasia Gogolek Aug 07 '12 at 08:57
  • Ufff finally I did it! I found out, that there´s a command line in TotalCommander so I changed the rights via that... Thank you so much for your advice and guidance! Now it finally works ;) – Michal S Aug 07 '12 at 13:10
  • @Kasia Gogolek please help me. I'm following Your suggession. But getting {'http:' is not recognized as an internal or external command, operable program or batch file} - this error. What will I do? – Ripa Saha Jul 24 '13 at 10:10
2

Use snappy from knplabs. Its a wrapper around wkhtmltopdf and handles the cases.

amitchhajer
  • 12,492
  • 6
  • 40
  • 53
1

(I have experience with wkhtmltopdf, but ONLY on *nix)

My advice: First do EVERYTHING without PHP, no exec().

Simply make sure you can type on the commandline the command to create the PDF.

Are you sure you installed WebKit on the machine? wkhtmltopdf.exe depends on it.

Only after you are sure you can generate from commandline, try to translate your action to PHP. ALso make sure PHP has appropriate rights to execute wkhtmltopdf.exe, AND has appropriate right to write away to C:/EXTREM.pdf.

Erwin Moller
  • 2,375
  • 14
  • 22
  • Actually, wkhtmltopdf does not depend on webkit; I don't have webkit on my server, I don't have a GUI, but wkhtmltopdf works like a charm. – Berry Langerak Aug 06 '12 at 09:59
  • @Berry Langerak the WK in wkhtmltopdf stand for webkit. I also have it running on my machine that even doesn't have a graphics card. Still its functionality depends on webkit rendering. (groeten aan Tilburg. :-) ) – Erwin Moller Aug 06 '12 at 10:03
  • I am well aware where the "WK" stands for in wkhtmltopdf. When I simply download the binary "wkhtmltopdf" and place it in a directory, it is instantly usable, there is no need for installation of further dependencies. Yes, it does depend on webkit rendering, but it's built in. – Berry Langerak Aug 06 '12 at 10:18
  • That depends. On modern distros: yes. I have to compile the lot in on a debian with 2.4.27-1-686-smp (i686). – Erwin Moller Aug 06 '12 at 10:26
  • It works in command line well... But I didnt install the WebKit... According to the answer above, I catch the error and it says sh: ../wkhtmltopdf/wkhtmltopdf.exe: Permission denied .... So maybe PHP dont have the rights...How can I allow PHP to execute the program? – Michal S Aug 06 '12 at 11:27
  • (Apparently webkit is compiled into your exe, that is what Berry claimed, so that is fine.) To your permission-problem: Solve it in 2 steps: First: Find out what use PHP runs as. On IIS this is often IUSR_ where machinename is the name of your system. Second, and you need additional right to do this (admin eg), grant execute permission to that IUSR_ for wkhtmltopdf/wkhtmltopdf.exe. Additionally, you might want to add write-rights to the folder where you want your PDF to be created. – Erwin Moller Aug 06 '12 at 12:07
  • Sorry, but I don´t know what IIS and IUSR is or how to change rights.. Im really a newbie :( – Michal S Aug 06 '12 at 13:14
  • IIS = Internet Information Server. Your webserver I presume, or are you running Apache under Windows? IUSR is a user on windows that is associated with IIS. (Each process runs as a user with certain rights.) – Erwin Moller Aug 06 '12 at 13:36