If you often use Scriptcase to quickly develop CRUD applications in PHP, you must definitely have used the Export feature from a Grid application, and most likely find it very useful as it can export in many different formats such as Office, PDF or event XML:
For most users, the export usually works out of the box and requires little configuration. However, recently in one of my projects, I came across an issue where the exported PDF file was either empty or had very small size such as 37 bytes which contained only the PDF header and was therefore corrupted. The web user interface did not show any useful messages, other than “PDF generation finished.”, and exporting to all other formats still worked well.
To investigate this problem, I checked the Scriptcase temporary folder, which is usually at the /_lib/tmp subdirectory. The full path to the log folder is also indicated in the “Configure production environment” menu after you log in to the Scriptcase production environment, by appending /_lib/prod to the site address.
The following is the screenshot of the configuration settings for Scriptcase v6:
Checking the temporary folder reveals the following files:
The .log file contains the following command, which presumably was used to generate the PDF file. Filenames and related information have been removed for security reasons.
java -Xms128m -Xmx256m -Djava.awt.headless=true -cp .:pd4ml.jar Pd4Php http://www.example.com/_lib/tmp/sc_grid_html_a8647dfca9a1a7861f93ca687eeb1c57.html 800 LETTER -orientation PORTRAIT -permissions 2076 > /home/example/_lib/tmp/sc_pdf_20151208170941_771_grid.pdf
Executing this command by connecting to the web host via SSH, which is a CentOS 6 VPS, result in the following error:
-bash: java: command not found
At this point it is clear what caused the issue. When the user requests to export a grid to PDF, Scriptcase first generates the corresponding HTML document for the grid, then executes pd4ml.jar with the help of Java to convert the HTML file to PDF. The conversion failed simply because Java was not installed on the server.
To fix this, we need to install Java. Take note that the following command is just an example, you might need to customize it based on your further needs, for example which version of Java needs to be installed and whether you will just need the Java Runtime Environment (JRE) or the full development kit (JDK). In our case, just the JRE is needed to execute the Scriptcase jar program. We proceed by installing the default Java package that comes with the OS:
sudo yum install java
Once installation is completed, try again – PDF export should work. If it doesn’t, check the URL that was passed in to pd4ml.jar (e.g. http://www.example.com/_lib/tmp/sc_grid_html_a8647dfca9a1a7861f93ca687eeb1c57.html) and make sure that you can download from it. If the URL is not correct, check the “PDF Server IP” field in the Scriptcase configuration settings. Contrary to what the name of the field may tell you, this has to be the root address of the server where the site is deployed, which can be an IP address (for test servers), or a domain name (for production servers). For example, if the deployed URL is http://www.example.com/script_case_app, enter www.example.com into the “PDF Server IP” field.
If it still doesn’t work, try to run the export command directly on your server to find out the exact error message. You might need to field in the full path to Java for the “Path for Java binary” and the “Java Binary” fields, although in my case I can just leave them as default since Java is in the system path.
If you are using Scriptcase v8, you will notice that the Java-related fields are removed from the configuration screen:
The export command now instead calls a tool named wkhtmltopdf directly to perform the conversion:
./wkhtmltopdf-amd64 --page-size Letter --orientation Landscape --outline-depth 0 --header-right "[page]" http://example.com/myapp/_lib/tmp/sc_grid_html_2fbff2b18e8a12a76de439a72c7907a1.html /home/example/public_html/_lib/tmp/sc_pdf_20151208173442_943_grid.pdf
The wkhtmltopdf tool is located in the _lib/prod/third/wkhtmltopdf folder, with different versions for different operating systems (Linux, OS X, Windows) and helps remove the need for Java, which may not be possible for users on shared web hosting services. If you still cannot export to PDF on Scriptcase v8 despite setting correct value for the “PDF Server IP” field, try to set the permissions of the wkhtmltopdf utility to 777, e.g. allow all read/write/execute permissions for everyone. This can be done by connecting to the server via SSH, or by using an FTP client such as FileZilla that supports setting file permissions.
Interestingly, exporting to other format such as Word, Excel or RTF does not require setting up the permissions or installing Java, at least for my case, even though the Scriptcase tools folder contains phpexcel and similar utilities, presumably for exporting purposes. I also found out that the exported documents are generally well-formatted, except when the grid has too many columns, in which case the page orientation of the Word document may need to be changed to landscape. For PDF documents, there is no easy way to change the page orientation and this is something I hope will be improved in the next Scriptcase version, for example, by allowing users to select between portait/landscape orientation before exporting.