> For the complete documentation index, see [llms.txt](https://docs.apryse.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.apryse.com/core/get-started/languages/php.md).

# PHP PDF library integration

Get started with Apryse PHP SDK on macOS. Integrate PDF library, run samples, and target PHP8, PHP7, or PHP5. Follow the guide for seamless setup and enjoy unlimited trial usage. Download now! The Apr

Welcome to Apryse. PHP for the Apryse SDK is supported on Linux and macOS. To get started, choose your preferred platform from the tabs below.

{% tabs %}
{% tab title="macOS" %}

## macOS PHP PDF library integration

This guide will help you [build your own PDFNet PHP wrappers](#initial-setup) by binding a free trial of the PDFNetC library to PHP. It will also show you how to [run Apryse samples](#run-the-samples) and [integrate the Apryse SDK into PHP applications](#integrate-into-your-application) on macOS. Your free trial includes unlimited trial usage and support from solution engineers.

## Prerequisites

* Targeting **PHP8**Please refer to [PDFNet wrappers](https://github.com/PDFTron/PDFNetWrappers/) for more information
  * **PHP8** with developer extensions and [**SWIG**](http://www.swig.org/) **4.1.0** and above.
* Targeting **PHP7**
  * **PHP7** with developer extensions and [**SWIG**](http://www.swig.org/) **3.0.12** OR [**SWIG**](http://www.swig.org/) **4.0.2** and above
* Targeting **PHP5**
  * **PHP5** with developer extensions and [**SWIG**](http://www.swig.org/) **2.0.4 - 2.0.12**
* [**CMake**](https://cmake.org/download/) version ≥ 2.8
* Download Apryse's PHP PDF library for macOS:

{% code lineNumbers="true" %}

```sh
curl -L -O https://downloads.apryse.com/downloads/PDFNetCMac.zip
unzip PDFNetCMac.zip
```

{% endcode %}

**NOTE: If you're using PHP 7, we highly recommend installing PHP with** [**Homebrew**](https://formulae.brew.sh/formula/php#default).

{% hint style="info" %}
**Run Apryse SDK in production**

A commercial license key is required for use in a production environment. Please [contact sales](https://apryse.com/form/contact-sales) to purchase a commercial key or if you need any other license key assistance.
{% endhint %}

{% hint style="warning" %}
**Keep your commercial license key confidential.**

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).
{% endhint %}

## Initial setup

1. Make a directory to store the wrappers and navigate into that directory.
2. Clone the PDFNet Wrappers project, select the correct branch for your release afterwards. This is typically a numbered release such as `11.11`, `11.12`, `11.13` and `12.0`.

{% code lineNumbers="true" %}

```sh
git clone https://github.com/ApryseSDK/PDFNetWrappers
git checkout <SDK Version>
```

{% endcode %}

3. Navigate to `PDFNetWrappers/PDFNetC`. Download the Apryse SDK using the method described in the **Prerequisites** section, and place the downloaded package in this directory and unzip it.
4. Now, to move the headers and libraries in place, make sure you are in the PDFNetWrappers/PDFNetC directory and execute. You can delete PDFNetCMac.zip to free up space. Your `/PDFNetC` folder should be laid out like this:

{% code lineNumbers="true" %}

```sh
mv PDFNetCMac/Headers/ .
mv PDFNetCMac/Lib/ .
```

{% endcode %}

{% code lineNumbers="true" %}

```sh
PDFNetC
├── Headers
├── Lib
├── PDFNetCMac
└── README.txt
```

{% endcode %}

5. Make a build directory inside `/PDFNetWrappers` and navigate to it. This guide will assume the build directory is called Build.

{% code lineNumbers="true" %}

```sh
cd .. # Go back up /PDFNetWrappers.
mkdir Build # Create a directory to create the Makefiles in.
cd Build # Move to that directory.
```

{% endcode %}

6. Generate the Makefile with `cmake`.

{% code lineNumbers="true" %}

```sh
cmake -D BUILD_PDFNetPHP=ON ..
```

{% endcode %}

7. Execute `make` followed by `sudo make install`. If all goes well, you should get a message that reads:

{% code lineNumbers="true" %}

```sh
make # Build the PHP wrappers with SWIG.
sudo make install # Copy the PHP wrappers to where the samples can find them.
```

{% endcode %}

{% code lineNumbers="true" %}

```sh
Build files have been written to ~/PDFNetWrappers/Build
```

{% endcode %}

8. Next, we need to fix the `rpaths` issue on macOS. While still in the same `Build` directory, execute these lines:

{% code lineNumbers="true" %}

```sh
cp ../fix_rpaths.sh ../PDFNetC/Lib/
cd ../PDFNetC/Lib/
sudo sh ./fix_rpaths.sh
```

{% endcode %}

9. Get your Apryse trial key.

{% @apryse-license-key/apryse-license-key platform="SERVER" variant="full" %}

You are now ready to [run the samples](#run-the-samples) or [integrate Apryse SDK into your own application](#integrate-into-your-application).

## Run the samples

**Run a specific sample**

1. To run a test, navigate to the `PHP` folder in the sample, for example `/Samples/AddImageTest/PHP` and execute `./RunTest.sh`

**Run all samples**

1. To run all tests, navigate to `/Samples` and execute:The tests will run one by one.

{% code lineNumbers="true" %}

```php
cd ../../Samples # Move to the Samples directory.
./runall_php.sh # Run all PHP code samples, using the new wrappers.
```

{% endcode %}

{% hint style="info" %}
**Output files will be in /Samples/TestFiles/Output**
{% endhint %}

## Integrate into your application

This section will show you how to use our SDK to create a simple Apryse "Hello World!" application. It will create a document with one blank page and save it as a [linearized PDF](https://apryse.com/blog/pdf-format/what-is-pdf-linearization) in its running directory.

1. Navigate into your `/Samples` directory and make a new directory called `myApp` (if it does not exist already). This guide will assume your application is named myApp. For organization, create a new directory inside `myApp` called `PHP`.
2. Navigate inside that `PHP` directory and create a new PHP file called `myApp.php`. Open it with your favorite text editor and paste this into it:Run your application by executing `php myApp.php`. If all goes well, your console should output:

<pre class="language-php" data-line-numbers><code class="lang-php">&#x3C;?php
    include("../../../PDFNetC/Lib/PDFNetPHP.php");
    PDFNet::Initialize("<code class="expression">visitor.claims.serverKey || "YOUR_LICENSE_KEY"</code>");       // PDFNet must be initialized before accessing any Apryse API
    $doc = new PDFDoc();
    $page = $doc->PageCreate(); // Start a new page
    $doc->PagePushBack($page); // Add the page to document
    $doc->Save("output.pdf", SDFDoc::e_linearized); // Save the document as a linearized PDF
    echo nl2br("Hello World!\n");
?>
</code></pre>

{% code lineNumbers="true" %}

```sh
PDFNet is running in demo mode.
Permission: write
Hello World!<br />
```

{% endcode %}

Check the `output.pdf` that the program output in the same directory. It should be a PDF with one blank page.

## Next step

<a href="https://github.com/iKettles/apryse-gitbook/tree/main/core/guides/README.md" class="button primary">Guides</a><a href="/core/get-started/samples.md" class="button primary">Samples</a>

{% hint style="info" %}
**Also check out the source for the PDFNetC wrappers.**
{% endhint %}

## Troubleshooting

Check the [troubleshooting page](/core/get-started/faq.md) and our [PDFNetWrappers github](https://github.com/PDFTron/PDFNetWrappers/) if you run into any issues going through this document.
{% endtab %}

{% tab title="Linux" %}

## Linux PHP PDF library integration

This guide will help you [build your own PDFNet PHP wrappers](#initial-setup-1) by binding a free trial of the PDFNetC library to PHP. It will also show you how to [run Apryse samples](#run-the-samples-1) and [integrate the Apryse SDK into PHP applications](#integrate-into-your-application-1) on Linux. Your free trial includes unlimited trial usage and support from solution engineers.

## Prerequisites

{% hint style="warning" %}
**Strict PHP and SWIG version compatibility**

Having a single version of SWIG and PHP installed on the server is preferred. Any other combinations of SWIG and PHP versions not listed below is likely to encounter problems. If your build is not working as expected then please double check the version numbers. Installing multiple versions of SWIG and PHP may also produce issues.
{% endhint %}

* Targeting **PHP8**Please refer to [PDFNet wrappers](https://github.com/PDFTron/PDFNetWrappers/) for more information
  * **PHP8** with developer extensions and [**SWIG**](http://www.swig.org/) **4.1.0** and above.
* Targeting **PHP7**
  * **PHP7** with developer extensions and [**SWIG**](http://www.swig.org/) **3.0.12** OR [**SWIG**](http://www.swig.org/) **4.0.2** and above
* Targeting **PHP5**
  * **PHP5** with developer extensions and [**SWIG**](http://www.swig.org/) **2.0.4 - 2.0.12**
* [**CMake**](https://cmake.org/download/) version ≥ 2.8
* Apryse SDK for Linux:

Download the SDK

{% hint style="info" %}
**Run Apryse SDK in production**

A commercial license key is required for use in a production environment. Please [contact sales](https://apryse.com/form/contact-sales) to purchase a commercial key or if you need any other license key assistance.
{% endhint %}

{% hint style="warning" %}
**Keep your commercial license key confidential.**

License keys are uniquely generated. Please make sure that it is not publicly available (e.g. in your public GitHub).
{% endhint %}

## Initial setup

1. Make a directory to store the wrappers and navigate into that directory.
2. Clone the PDFNet Wrappers project, select the correct branch for your release afterwards. This is typically a numbered release such as `11.11`, `11.12`, `11.13` and `12.0`.

{% code lineNumbers="true" %}

```sh
git clone https://github.com/ApryseSDK/PDFNetWrappers # Git clone the code.
git checkout <SDK Version>
```

{% endcode %}

1. Navigate to `PDFNetWrappers/PDFNetC` and [download the PDFNet C/C++ SDK](#prerequisites-1) into that directory. Ensure you obtain the right architecture for your PHP interpreter. For example, if your interpreter is 64bit (which this guide will assume), execute.

{% code lineNumbers="true" %}

```sh
cd PDFNetWrappers/PDFNetC # Move to where we download PDFNet.
wget https://downloads.apryse.com/downloads/PDFNetC64.tar.gz # Download PDFNet.
tar xzvf PDFNetC64.tar.gz # Unpack PDFNet.
```

{% endcode %}

1. Unpack `PDFNetC64.tar.gz` by `tar xvzf PDFNetC64.tar.gz`, then executeto move the PDFNet libraries in place.You can delete `PDFNetC64.tar.gz` to free up space.Your `/PDFNetC` folder should be laid out like this like this:

{% code lineNumbers="true" %}

```sh
mv PDFNetC64/Headers/ . # Move PDFNet Headers/ into place.
mv PDFNetC64/Lib/ . # Move PDFNet Lib/ into place.
```

{% endcode %}

{% code lineNumbers="true" %}

```sh
PDFNetC
├── Headers
├── Lib
├── PDFNetC64
└── README.txt
```

{% endcode %}

1. Make a build directory inside `/PDFNetWrappers` and navigate to it. This guide will assume the build directory is called Build.

{% code lineNumbers="true" %}

```sh
cd .. # Go back up /PDFNetWrappers.
mkdir Build # Create a directory to create the Makefiles in.
cd Build # Move to that directory.
```

{% endcode %}

1. Execute `make` followed by `sudo make install`. It is important when installing the php-dev directories to target the same version of PHP you're utilizing, otherwise it will pull the latest and likely cause build errors. For instance for PHP 7.4 execute `sudo apt-get install php7.4-dev`.If all goes well, you should get a message which reads:

{% code lineNumbers="true" %}

```sh
sudo apt-get install php-dev # add php-dev for required PHP include directories
cmake -D BUILD_PDFNetPHP=ON .. # Create the Makefiles with CMake.
make # Build the PHP wrappers with SWIG.
sudo make install # Copy the PHP wrappers to where the samples can find them.
```

{% endcode %}

{% code lineNumbers="true" %}

```sh
Build files have been written to ~/PDFNetWrappers/Build
```

{% endcode %}

1. Please note that you may need to register `PDFNetPHP.so` as an extension to your PHP by adding the following line in all your `php.ini` files:You may also need to enable dynamic loaded extensions by updating `enable_dl = On` in all your `php.ini` files as well.

{% code lineNumbers="true" %}

```sh
extension=/full/path/to/PDFNetPHP.so
```

{% endcode %}

{% code lineNumbers="true" %}

```sh
enable_dl = On
```

{% endcode %}

1. Get your Apryse trial key.

{% @apryse-license-key/apryse-license-key platform="SERVER" variant="full" %}

You are now ready to [run the samples](#run-the-samples-1) or [integrate Apryse SDK into your own application](#integrate-into-your-application-1).

## Run the samples

**Run a specific sample**

1. To run a test, navigate to the `PHP` folder in the sample, for example `/Samples/AddImageTest/PHP` and execute `./RunTest.sh`

**Run all samples**

1. To run all tests, navigate to `/Samples` and execute:The tests will run one by one.

{% code lineNumbers="true" %}

```php
cd ../Samples # Move to the Samples directory.
./runall_php.sh # Run all PHP code samples, using the new wrappers.
```

{% endcode %}

{% hint style="info" %}
**Output files will be in /Samples/TestFiles/Output**
{% endhint %}

## Integrate into your application

This section will show you how to use our SDK to create a simple Apryse "Hello World!" application. It will create a document with one blank page and save it as a [linearized PDF](https://apryse.com/blog/pdf-format/what-is-pdf-linearization) in its running directory.

1. Navigate into your `/Samples` directory and make a new directory called `myApp` (if it does not exist already). This guide will assume your application is named myApp. For organization, create a new directory inside `myApp` called `PHP`.
2. Navigate inside that `PHP` directory and create a new PHP file called `myApp.php`. Open it with your favorite text editor and paste this into it:Run your application by executing `php myApp.php`. If all goes well, your console should output:

<pre class="language-php" data-line-numbers><code class="lang-php">&#x3C;?php
    include("../../../PDFNetC/Lib/PDFNetPHP.php");
    PDFNet::Initialize("<code class="expression">visitor.claims.serverKey || "YOUR_LICENSE_KEY"</code>");       // PDFNet must be initialized before accessing any Apryse API
    $doc = new PDFDoc();
    $page = $doc->PageCreate(); // Start a new page
    $doc->PagePushBack($page); // Add the page to document
    $doc->Save("output.pdf", SDFDoc::e_linearized); // Save the document as a linearized PDF
    echo nl2br("Hello World!\n");
?>
</code></pre>

{% code lineNumbers="true" %}

```sh
PDFNet is running in demo mode.
Permission: write
Hello World!<br />
```

{% endcode %}

Check the `output.pdf` that the program output in the same directory. It should be a PDF with one blank page.

## Next step

<a href="https://github.com/iKettles/apryse-gitbook/tree/main/core/guides/README.md" class="button primary">Guides</a><a href="/core/get-started/samples.md" class="button primary">Samples</a>

{% hint style="info" %}
**Also check out the source for the PDFNetC wrappers.**
{% endhint %}

## Troubleshooting

Check the [troubleshooting page](/core/get-started/faq.md) and our [PDFNetWrappers github](https://github.com/PDFTron/PDFNetWrappers/) if you run into any issues going through this document.
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.apryse.com/core/get-started/languages/php.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
