Search code examples
phpexecbatik

PHP exec and Batik rasterizer


I'm using Batik to convert SVG images to PDF. When converting them through the terminal everying is fine and the file gets converted. However, what I want is to convert it dynamically from PHP with exec().

This is what i see when I convert from the terminal:

About to convert 1 SVG file(s)

Converting output.svg to /opt/lampp/htdocs/tool/generated/output.pdf...

... success

When using exec() the output is limited to 'Converting output.svg... Etc' without the ...success part. Its as if the server won't wait for the script to finish. Also, the resulting PDF-file that does get generated is completely empty.

This is the call in PHP:

$command = 'cd ~/Downloads/batik-1.7 && java -jar batik-rasterizer.jar /opt/lampp/htdocs/tool/generated/output.svg -m "application/pdf"';

$string = exec($command);

echo '{"success": true, "message": "Design saved as generated/output.svg' . $string . '"}';`

This is what I see in the js console:

Design saved as generated/output.svgConverting output.svg to /opt/lampp/htdocs/tool/generated/output.pdf ...

Edit:

Using stderr I got:

No protocol specified Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.

Edit 2:

After some googling I found the solution:

-Djava.awt.headless=true :)

Thanks all for guiding me in finding the answer!


Solution

  • Learn about the standard streams stdout and stderr.