PHPackages                             api2pdf/api2pdf.php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [PDF &amp; Document Generation](/categories/documents)
4. /
5. api2pdf/api2pdf.php

ActiveLibrary[PDF &amp; Document Generation](/categories/documents)

api2pdf/api2pdf.php
===================

This client library is a wrapper for the Api2Pdf.com REST API. See full REST api documentation at https://www.api2pdf.com/documentation/v2. Api2Pdf is a powerful API that supports HTML to PDF, URL to PDF, HTML to Image, URL to Image, Thumbnail / image preview of an Office file, Office files (Word to PDF), HTML to Docx, HTML to excel, PDF to HTML, merge PDFs together, add bookmarks to PDFs, add passwords to PDFs

2.0.2(4y ago)48632.2k↓17.3%18[1 issues](https://github.com/Api2Pdf/api2pdf.php/issues)4MITPHPPHP &gt;=5.6

Since Jan 28Pushed 3y ago5 watchersCompare

[ Source](https://github.com/Api2Pdf/api2pdf.php)[ Packagist](https://packagist.org/packages/api2pdf/api2pdf.php)[ Docs](https://www.api2pdf.com)[ RSS](/packages/api2pdf-api2pdfphp/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)DependenciesVersions (6)Used By (4)

api2pdf.php
===========

[](#api2pdfphp)

PHP code for [Api2Pdf REST API](https://www.api2pdf.com/documentation/v2)

Api2Pdf.com is a powerful REST API for instantly generating PDF and Office documents from HTML, URLs, Microsoft Office Documents (Word, Excel, PPT), Email files, and images. You can generate image preview or thumbnail of a PDF, office document, or email file. The API also supports merge / concatenation of two or more PDFs, setting passwords on PDFs, and adding bookmarks to PDFs. Api2Pdf is a wrapper for popular libraries such as **wkhtmltopdf**, **Headless Chrome**, **PdfSharp**, and **LibreOffice**.

- [Installation](#installation)
- [Resources](#resources)
- [Authorization](#authorization)
- [Usage](#usage)
- [FAQ](https://www.api2pdf.com/faq)

Installation
----------------------------------------------------

[](#installation)

Run the following from command line:

```
$ composer require api2pdf/api2pdf.php

```

Usage without Composer
----------------------

[](#usage-without-composer)

Copy the file in the `src` directory to a sub-directory in your project, then add the following in the beginning of your PHP file:

```
require_once 'your-own-directory/Api2Pdf.php';

```

Resources
----------------------------------------------

[](#resources)

Resources this API supports:

- [wkhtmltopdf](#wkhtmltopdf)
- [Headless Chrome](#chrome)
- [LibreOffice](#libreoffice)
- [Merge / Concatenate PDFs](#merge)
- [Helper Methods](#helper-methods)

Authorization
------------------------------------------------------

[](#authorization)

### Acquire API Key

[](#acquire-api-key)

Create an account at [portal.api2pdf.com](https://portal.api2pdf.com/register) to get your API key.

Usage
---------------------------------------

[](#usage)

### Initialize the Client

[](#initialize-the-client)

All usage starts by calling the import command and initializing the client by passing your API key as a parameter to the constructor.

```
use Api2Pdf\Api2Pdf;

$apiClient = new Api2Pdf('YOUR-API-KEY');
```

Once you initialize the client, you can make calls like so:

```
$result = $apiClient->chromeHtmlToPdf('Hello, World');
echo $result->getFile();
```

### Result Format

[](#result-format)

An ApiResult object is returned from every API call. If a call is unsuccessful then an exception will be thrown with a message containing the result of failure.

Additional attributes include the total data usage out, and the cost for the API call, typically very small fractions of a penny.

```
$file = $result->getFile();
$cost = $result->getCost();
$mbOut = $result->getMbOut();
$seconds = $result->getSeconds();
$responseId = $result->getResponseId();
```

###  wkhtmltopdf

[](#-wkhtmltopdf)

**Convert HTML to PDF**

```
$result = $apiClient->wkHtmlToPdf('Hello, World');
```

**Convert HTML to PDF (load PDF in browser window and specify a file name)**

```
$result = $apiClient->wkHtmlToPdf('Hello, World', $inline = false, $fileName = "test.pdf");
```

**Convert HTML to PDF (use arguments for advanced wkhtmltopdf settings)**[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)

```
$options = [
    "orientation" => "landscape",
    "pageSize" => "A4"
];
$result = $apiClient->wkHtmlToPdf('Hello, World', $inline = false, $filename = "test.pdf", $options = $options);
```

**Convert URL to PDF**

```
$result = $apiClient->wkUrlToPdf('http://www.api2pdf.com');
```

**Convert URL to PDF (load PDF in browser window and specify a file name)**

```
$result = $apiClient->wkUrlToPdf('http://www.api2pdf.com', $inline = false, $fileName = "test.pdf");
```

**Convert URL to PDF (use arguments for advanced wkhtmltopdf settings)**[View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/)

```
$options = [
    "orientation" => "landscape",
    "pageSize" => "A4"
];
$result = $apiClient->wkUrlToPdf('http://www.api2pdf.com', $inline = false, $filename = "test.pdf", $options = $options);
```

---

Headless Chrome
-------------------------------------------------

[](#headless-chrome)

**Convert HTML to PDF**

```
$result = $apiClient->chromeHtmlToPdf('Hello, World');
```

**Convert HTML to PDF (load PDF in browser window and specify a file name)**

```
$result = $apiClient->chromeHtmlToPdf('Hello, World', $inline = false, $filename = "test.pdf");
```

**Convert HTML to PDF (use arguments for advanced Headless Chrome settings)**[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)

```
$options = [
    "landscape" => true
];
$result = $apiClient->chromeHtmlToPdf('Hello, World', $inline = false, $filename = "test.pdf", $options = $options);
```

**Convert URL to PDF**

```
$result = $apiClient->chromeUrlToPdf('http://www.api2pdf.com');
```

**Convert URL to PDF (load PDF in browser window and specify a file name)**

```
$result = $apiClient->chromeUrlToPdf('http://www.api2pdf.com', $inline = false, $filename = "test.pdf");
```

**Convert URL to PDF (use arguments for advanced Headless Chrome settings)**[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)

```
$options = [
    "landscape" => true
];
$result = $apiClient->chromeUrlToPdf('http://www.api2pdf.com', $inline = false, $filename = "test.pdf", $options = $options);
```

**Convert HTML to Image**

```
$result = $apiClient->chromeHtmlToImage('Hello, World');
```

**Convert HTML to Image (load image in browser window and specify a file name)**

```
$result = $apiClient->chromeHtmlToImage('Hello, World', $inline = false, $filename = "test.jpg");
```

**Convert HTML to Image (use arguments for advanced Headless Chrome settings)**[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)

```
$options = [
    "fullPage" => true
];
$result = $apiClient->chromeHtmlToImage('Hello, World', $inline = false, $filename = "test.jpg", $options = $options);
```

**Convert URL to Image**

```
$result = $apiClient->chromeUrlToImage('http://www.api2pdf.com');
```

**Convert URL to Image (load image in browser window and specify a file name)**

```
$result = $apiClient->chromeUrlToImage('http://www.api2pdf.com', $inline = false, $filename = "test.jpg");
```

**Convert URL to Image (use arguments for advanced Headless Chrome settings)**[View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/)

```
$options = [
    "fullPage" => true
];
$result = $apiClient->chromeUrlToImage('http://www.api2pdf.com', $inline = false, $filename = "test.jpg", $options = $options);
```

---

LibreOffice
--------------------------------------------------

[](#libreoffice)

Convert any office file to PDF, image file to PDF, email file to PDF, HTML to Word, HTML to Excel, and PDF to HTML. Any file that can be reasonably opened by LibreOffice should be convertible. Additionally, we have an endpoint for generating a *thumbnail* of the first page of your PDF or Office Document. This is great for generating an image preview of your files to users.

You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF.

**Convert Microsoft Office Document or Image to PDF**

```
$result = $apiClient->libreOfficeAnyToPdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx');
```

**Thumbnail or Image Preview of a PDF or Office Document or Email file**

```
$result = $apiClient->libreOfficeThumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx');
```

**Convert HTML to Microsoft Word or Docx**

```
$result = $apiClient->libreOfficeHtmlToDocx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html');
```

**Convert HTML to Microsoft Excel or Xlsx**

```
$result = $apiClient->libreOfficeHtmlToXlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html');
```

**Convert PDF to HTML**

```
$result = $apiClient->libreOfficePdfToHtml('http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf');
```

---

PdfSharp - Merge / Concatenate Two or More PDFs, Add bookmarks to pdfs, add passwords to pdfs
------------------------------------------------------------------------------------------------------------------------------

[](#pdfsharp---merge--concatenate-two-or-more-pdfs-add-bookmarks-to-pdfs-add-passwords-to-pdfs)

To use the merge endpoint, supply a list of urls to existing PDFs. The engine will consume all of the PDFs and merge them into a single PDF, in the order in which they were provided in the list.

**Merge PDFs from list of URLs to existing PDFs**

```
$linksToPdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF'];
$mergeResult = $apiClient->pdfsharpMerge($linksToPdfs);
```

**Add bookmarks to existing PDF**

```
$linkToPdf = 'https://LINK-TO-PDF';
$bookmarks = [
    [ "Page" => 0, "Title" => "Introduction" ],
    [ "Page" => 1, "Title" => "Second page" ]
];
$bookmarkResult = $apiClient->pdfsharpAddBookmarks($linkToPdf, $bookmarks);
```

**Add password to existing PDF**

```
$linkToPdf = 'https://LINK-TO-PDF';
$userpassword = 'hello';
$bookmarkResult = $apiClient->pdfsharpAddPassword($linkToPdf, $userpassword);
```

---

Helper Methods
--------------------------------------------------------

[](#helper-methods)

**delete($responseId)**

By default, Api2Pdf will delete your generated file 24 hours after it has been generated. For those with high security needs, you may want to delete your file on command. You can do so by making an DELETE api call with the `responseId` attribute that was returned on the original JSON payload.

```
$result = $apiClient->chromeHtmlToPdf("Hello World");
$responseId = $result->getResponseId();
//delete pdf
$apiClient->utilityDelete($responseId);
```

###  Health Score

42

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity51

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity59

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 76.5% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~250 days

Total

5

Last Release

1665d ago

Major Versions

1.1.1 → 2.0.02021-07-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/be90cb027c6d4549c43afe6eb392751cd91cdb5035bd33734da13eaf7faa0a00?d=identicon)[api2pdf](/maintainers/api2pdf)

---

Top Contributors

[![apexdodge](https://avatars.githubusercontent.com/u/777005?v=4)](https://github.com/apexdodge "apexdodge (26 commits)")[![arkamax2k](https://avatars.githubusercontent.com/u/11448108?v=4)](https://github.com/arkamax2k "arkamax2k (4 commits)")[![georgeboot](https://avatars.githubusercontent.com/u/884482?v=4)](https://github.com/georgeboot "georgeboot (2 commits)")[![rozenlicht](https://avatars.githubusercontent.com/u/6413348?v=4)](https://github.com/rozenlicht "rozenlicht (1 commits)")[![sergiosusa](https://avatars.githubusercontent.com/u/5395597?v=4)](https://github.com/sergiosusa "sergiosusa (1 commits)")

---

Tags

aws-lambdabarcodeheadless-chromehtml-to-docxhtml-to-imagehtml-to-pdfhtml-to-xlsxlibreofficemerge-pdfpdfpdf-generationpdf-to-htmlphpqrcoderest-apiurl-to-imageurl-to-pdfwkhtmltopdfword-to-pdf

### Embed Badge

![Health badge](/badges/api2pdf-api2pdfphp/health.svg)

```
[![Health](https://phpackages.com/badges/api2pdf-api2pdfphp/health.svg)](https://phpackages.com/packages/api2pdf-api2pdfphp)
```

###  Alternatives

[spatie/browsershot

Convert a webpage to an image or pdf using headless Chrome

5.2k32.1M102](/packages/spatie-browsershot)[barryvdh/laravel-snappy

Snappy PDF/Image for Laravel

2.8k24.8M48](/packages/barryvdh-laravel-snappy)[openspout/openspout

PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way

1.2k57.6M131](/packages/openspout-openspout)[keboola/csv

Keboola CSV reader and writer

1451.8M21](/packages/keboola-csv)[setasign/tfpdf

This class is a modified version of FPDF that adds UTF-8 support. The latest version is based on FPDF 1.85.

426.1M30](/packages/setasign-tfpdf)[aspera/xlsx-reader

Spreadsheet reader library for XLSX files

52742.2k5](/packages/aspera-xlsx-reader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
