Available Now: Explore our latest release with enhanced accessibility and powerful IDP features

PHP 7 Support Added To Our PHP PDF Library

By Minh Nguyen (Mitch) | 2018 Nov 16

Sanity Image

We are happy to announce that we have added PHP 7 bindings to our PDF SDK through SWIG 3. This blog will outline some of the changes we made to our PHP PDF library so that it would support both PHP5/SWIG2 and PHP7/SWIG3.

APIs and Data Structures

Copied to clipboard

There are some major changes in APIs and data structures between PHP 5 and PHP 7. In order to support PHP 7, we have made some changes to PDFNetPHP.i and PDFNetUStringPHP.i in PDFNetPHP:

PHP 5:

//@data {"displayAs": ""}//
zval **data
Z_ARRVAL_PP, Z_TYPE_PP
zend_hash_get_current_data_ex(_,_,_)
ZVAL_STRINGL(_,_,_,_)

PHP 7:

//@data {"displayAs": ""}//
zval *data
Z_ARRVAL_P, Z_TYPE_P
zend_hash_get_current_data_ex(_,_)
ZVAL_STRINGL(_,_,_)

CMakeLists.txt

Copied to clipboard

We also made the following changes to CMakeLists.txt:

PHP 5:

//@data {"displayAs": ""}//
execute_process(
COMMAND ${SWIG_EXECUTABLE} -c++ -php -DNTS -DSWIGHIDDEN_SIG -I${PDFNetC_Include_Dir} -outdir ${PDFNetPHP_SourcesDir} -o ${PDFNetPHP_SourcesDir}/PDFNetPHP.cpp -oh ${PDFNetPHP_SourcesDir}/PDFNetPHP.hpp PDFNetPHP.i
)

PHP 7:

//@data {"displayAs": ""}//
execute_process(
COMMAND ${SWIG_EXECUTABLE} -c++ -php7 -DNTS -DSWIGHIDDEN_SIG -I${PDFNetC_Include_Dir} -outdir ${PDFNetPHP_SourcesDir} -o ${PDFNetPHP_SourcesDir}/PDFNetPHP.cpp -oh ${PDFNetPHP_SourcesDir}/PDFNetPHP.hpp PDFNetPHP.i
)

A Combined Solution

Copied to clipboard

Instead of having separate solutions for PHP 5 and PHP 7, we were able to combine them into a single build by using a php-config executable in CMake to obtain PHP package information:

//@data {"displayAs": ""}//
find_program(PHP_CONFIG_EXECUTABLE NAMES php-config7 php-config5 php-config4 php-config)
execute_process(
COMMAND
    ${PHP_CONFIG_EXECUTABLE} --vernum
    OUTPUT_STRIP_TRAILING_WHITESPACE
    COMMAND sed -ne "s/....$//p"
    OUTPUT_VARIABLE PHP_MAJOR_VERSION
)

Once PHP_MAJOR_VERSION is obtained, we can set the appropriate arguments on SWIG:

//@data {"displayAs": ""}//
if (PHP_MAJOR_VERSION LESS 7)
       set(php_version "php")
else()
       set(php_version "php7")
endif()
execute_process(
    COMMAND ${SWIG_EXECUTABLE} -c++ -${php_version} …
)

The next changes are using preprocessor directives in the interface files:

#if PHP_MAJOR_VERSION >= 7
...
#else
... 
#endif 
Blog image
Blog image

For more information about setup instructions, please click here.

Conclusion

In this blog, we outlined how we added support for PHP 7 without the need for a separate configuration, while also ensuring seamless support for any newer PHP/SWIG versions. If you have any questions, please feel free to contact us and we will be happy to help.

Sanity Image

Minh Nguyen (Mitch)

Share this post

email
linkedIn
twitter