├── .gitignore ├── LICENSE ├── README.md ├── composer.json └── src ├── Api2Pdf.php ├── Api2PdfException.php ├── Api2PdfInterface.php └── Api2PdfResult.php /.gitignore: -------------------------------------------------------------------------------- 1 | # ignore IDE/hidden/OS cache files 2 | .* 3 | *~ 4 | nbproject 5 | nb-configuration.xml 6 | Session.vim 7 | *.tmproj 8 | *.tmproject 9 | tmtags 10 | Thumbs.db 11 | Desktop.ini 12 | 13 | # ignore Composer downloads 14 | /vendor/* 15 | 16 | # keep some hiddens 17 | !.htaccess 18 | !/.gitignore 19 | !/.travis.yml 20 | 21 | test.php 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Api2Pdf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # api2pdf.php 2 | PHP code for [Api2Pdf REST API](https://www.api2pdf.com/documentation/v2) 3 | 4 | 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**. 5 | 6 | - [Installation](#installation) 7 | - [Resources](#resources) 8 | - [Authorization](#authorization) 9 | - [Usage](#usage) 10 | - [FAQ](https://www.api2pdf.com/faq) 11 | 12 | ## Installation 13 | 14 | Run the following from command line: 15 | 16 | ``` 17 | $ composer require api2pdf/api2pdf.php 18 | ``` 19 | 20 | ## Usage without Composer 21 | 22 | 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: 23 | 24 | ``` 25 | require_once 'your-own-directory/Api2Pdf.php'; 26 | 27 | ``` 28 | 29 | ## Resources 30 | 31 | Resources this API supports: 32 | 33 | - [wkhtmltopdf](#wkhtmltopdf) 34 | - [Headless Chrome](#chrome) 35 | - [LibreOffice](#libreoffice) 36 | - [Merge / Concatenate PDFs](#merge) 37 | - [Helper Methods](#helper-methods) 38 | 39 | ## Authorization 40 | 41 | ### Acquire API Key 42 | 43 | Create an account at [portal.api2pdf.com](https://portal.api2pdf.com/register) to get your API key. 44 | 45 | ## Usage 46 | 47 | ### Initialize the Client 48 | 49 | All usage starts by calling the import command and initializing the client by passing your API key as a parameter to the constructor. 50 | 51 | ```php 52 | use Api2Pdf\Api2Pdf; 53 | 54 | $apiClient = new Api2Pdf('YOUR-API-KEY'); 55 | ``` 56 | 57 | Once you initialize the client, you can make calls like so: 58 | 59 | ```php 60 | $result = $apiClient->chromeHtmlToPdf('

Hello, World

'); 61 | echo $result->getFile(); 62 | ``` 63 | 64 | ### Result Format 65 | 66 | 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. 67 | 68 | Additional attributes include the total data usage out, and the cost for the API call, typically very small fractions of a penny. 69 | 70 | ```php 71 | $file = $result->getFile(); 72 | $cost = $result->getCost(); 73 | $mbOut = $result->getMbOut(); 74 | $seconds = $result->getSeconds(); 75 | $responseId = $result->getResponseId(); 76 | ``` 77 | 78 | ### wkhtmltopdf 79 | 80 | **Convert HTML to PDF** 81 | 82 | ```php 83 | $result = $apiClient->wkHtmlToPdf('

Hello, World

'); 84 | ``` 85 | 86 | **Convert HTML to PDF (load PDF in browser window and specify a file name)** 87 | 88 | ```php 89 | $result = $apiClient->wkHtmlToPdf('

Hello, World

', $inline = false, $fileName = "test.pdf"); 90 | ``` 91 | 92 | **Convert HTML to PDF (use arguments for advanced wkhtmltopdf settings)** 93 | [View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/) 94 | 95 | ```php 96 | $options = [ 97 | "orientation" => "landscape", 98 | "pageSize" => "A4" 99 | ]; 100 | $result = $apiClient->wkHtmlToPdf('

Hello, World

', $inline = false, $filename = "test.pdf", $options = $options); 101 | ``` 102 | 103 | **Convert URL to PDF** 104 | 105 | ```php 106 | $result = $apiClient->wkUrlToPdf('http://www.api2pdf.com'); 107 | ``` 108 | 109 | **Convert URL to PDF (load PDF in browser window and specify a file name)** 110 | 111 | ```php 112 | $result = $apiClient->wkUrlToPdf('http://www.api2pdf.com', $inline = false, $fileName = "test.pdf"); 113 | ``` 114 | 115 | **Convert URL to PDF (use arguments for advanced wkhtmltopdf settings)** 116 | [View full list of wkhtmltopdf options available.](https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/) 117 | 118 | ```php 119 | $options = [ 120 | "orientation" => "landscape", 121 | "pageSize" => "A4" 122 | ]; 123 | $result = $apiClient->wkUrlToPdf('http://www.api2pdf.com', $inline = false, $filename = "test.pdf", $options = $options); 124 | ``` 125 | 126 | 127 | --- 128 | 129 | ## Headless Chrome 130 | 131 | **Convert HTML to PDF** 132 | 133 | ```php 134 | $result = $apiClient->chromeHtmlToPdf('

Hello, World

'); 135 | ``` 136 | 137 | **Convert HTML to PDF (load PDF in browser window and specify a file name)** 138 | 139 | ```php 140 | $result = $apiClient->chromeHtmlToPdf('

Hello, World

', $inline = false, $filename = "test.pdf"); 141 | ``` 142 | 143 | **Convert HTML to PDF (use arguments for advanced Headless Chrome settings)** 144 | [View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) 145 | 146 | ```php 147 | $options = [ 148 | "landscape" => true 149 | ]; 150 | $result = $apiClient->chromeHtmlToPdf('

Hello, World

', $inline = false, $filename = "test.pdf", $options = $options); 151 | ``` 152 | 153 | **Convert URL to PDF** 154 | 155 | ```php 156 | $result = $apiClient->chromeUrlToPdf('http://www.api2pdf.com'); 157 | ``` 158 | 159 | **Convert URL to PDF (load PDF in browser window and specify a file name)** 160 | 161 | ```php 162 | $result = $apiClient->chromeUrlToPdf('http://www.api2pdf.com', $inline = false, $filename = "test.pdf"); 163 | ``` 164 | 165 | **Convert URL to PDF (use arguments for advanced Headless Chrome settings)** 166 | [View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) 167 | 168 | ```php 169 | $options = [ 170 | "landscape" => true 171 | ]; 172 | $result = $apiClient->chromeUrlToPdf('http://www.api2pdf.com', $inline = false, $filename = "test.pdf", $options = $options); 173 | ``` 174 | 175 | **Convert HTML to Image** 176 | 177 | ```php 178 | $result = $apiClient->chromeHtmlToImage('

Hello, World

'); 179 | ``` 180 | 181 | **Convert HTML to Image (load image in browser window and specify a file name)** 182 | 183 | ```php 184 | $result = $apiClient->chromeHtmlToImage('

Hello, World

', $inline = false, $filename = "test.jpg"); 185 | ``` 186 | 187 | **Convert HTML to Image (use arguments for advanced Headless Chrome settings)** 188 | [View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) 189 | 190 | ```php 191 | $options = [ 192 | "fullPage" => true 193 | ]; 194 | $result = $apiClient->chromeHtmlToImage('

Hello, World

', $inline = false, $filename = "test.jpg", $options = $options); 195 | ``` 196 | 197 | **Convert URL to Image** 198 | 199 | ```php 200 | $result = $apiClient->chromeUrlToImage('http://www.api2pdf.com'); 201 | ``` 202 | 203 | **Convert URL to Image (load image in browser window and specify a file name)** 204 | 205 | ```php 206 | $result = $apiClient->chromeUrlToImage('http://www.api2pdf.com', $inline = false, $filename = "test.jpg"); 207 | ``` 208 | 209 | **Convert URL to Image (use arguments for advanced Headless Chrome settings)** 210 | [View full list of Headless Chrome options available.](https://www.api2pdf.com/documentation/advanced-options-headless-chrome/) 211 | 212 | ```php 213 | $options = [ 214 | "fullPage" => true 215 | ]; 216 | $result = $apiClient->chromeUrlToImage('http://www.api2pdf.com', $inline = false, $filename = "test.jpg", $options = $options); 217 | ``` 218 | 219 | --- 220 | 221 | ## LibreOffice 222 | 223 | 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. 224 | 225 | You must provide a url to the file. Our engine will consume the file at that URL and convert it to the PDF. 226 | 227 | **Convert Microsoft Office Document or Image to PDF** 228 | 229 | ```php 230 | $result = $apiClient->libreOfficeAnyToPdf('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx'); 231 | ``` 232 | 233 | **Thumbnail or Image Preview of a PDF or Office Document or Email file** 234 | 235 | ```php 236 | $result = $apiClient->libreOfficeThumbnail('https://www.api2pdf.com/wp-content/themes/api2pdf/assets/samples/sample-word-doc.docx'); 237 | ``` 238 | 239 | **Convert HTML to Microsoft Word or Docx** 240 | 241 | ```php 242 | $result = $apiClient->libreOfficeHtmlToDocx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleHtml.html'); 243 | ``` 244 | 245 | **Convert HTML to Microsoft Excel or Xlsx** 246 | 247 | ```php 248 | $result = $apiClient->libreOfficeHtmlToXlsx('http://www.api2pdf.com/wp-content/uploads/2021/01/sampleTables.html'); 249 | ``` 250 | 251 | **Convert PDF to HTML** 252 | 253 | ```php 254 | $result = $apiClient->libreOfficePdfToHtml('http://www.api2pdf.com/wp-content/uploads/2021/01/1a082b03-2bd6-4703-989d-0443a88e3b0f-4.pdf'); 255 | ``` 256 | --- 257 | 258 | ## PdfSharp - Merge / Concatenate Two or More PDFs, Add bookmarks to pdfs, add passwords to pdfs 259 | 260 | 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. 261 | 262 | **Merge PDFs from list of URLs to existing PDFs** 263 | 264 | ```php 265 | $linksToPdfs = ['https://LINK-TO-PDF', 'https://LINK-TO-PDF']; 266 | $mergeResult = $apiClient->pdfsharpMerge($linksToPdfs); 267 | ``` 268 | 269 | **Add bookmarks to existing PDF** 270 | 271 | ```php 272 | $linkToPdf = 'https://LINK-TO-PDF'; 273 | $bookmarks = [ 274 | [ "Page" => 0, "Title" => "Introduction" ], 275 | [ "Page" => 1, "Title" => "Second page" ] 276 | ]; 277 | $bookmarkResult = $apiClient->pdfsharpAddBookmarks($linkToPdf, $bookmarks); 278 | ``` 279 | 280 | **Add password to existing PDF** 281 | 282 | ```php 283 | $linkToPdf = 'https://LINK-TO-PDF'; 284 | $userpassword = 'hello'; 285 | $bookmarkResult = $apiClient->pdfsharpAddPassword($linkToPdf, $userpassword); 286 | ``` 287 | 288 | --- 289 | 290 | ## Helper Methods 291 | 292 | **delete($responseId)** 293 | 294 | 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. 295 | 296 | ```php 297 | $result = $apiClient->chromeHtmlToPdf("

Hello World

"); 298 | $responseId = $result->getResponseId(); 299 | //delete pdf 300 | $apiClient->utilityDelete($responseId); 301 | ``` 302 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "api2pdf/api2pdf.php", 3 | "description": "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", 4 | "license": "MIT", 5 | "homepage": "https://www.api2pdf.com", 6 | "support": { 7 | "source": "https://github.com/api2pdf/api2pdf.php" 8 | }, 9 | "require": { 10 | "php": ">=5.6", 11 | "ext-curl": "*" 12 | }, 13 | "autoload": { 14 | "psr-4": { 15 | "Api2Pdf\\": "src/" 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Api2Pdf.php: -------------------------------------------------------------------------------- 1 | apiKey = $apiKey; 20 | $this->base_url = $base_url; 21 | } 22 | 23 | /** 24 | * @param string $url 25 | * @throws Api2PdfException 26 | * @return Api2PdfResult 27 | */ 28 | public function chromeUrlToPdf($url, $inline = true, $filename = null, $options = null) 29 | { 30 | $payload = array_merge( 31 | $this->buildPayloadBase($inline, $filename, $options), 32 | [ 33 | 'url' => $url, 34 | ] 35 | ); 36 | return $this->makeRequest('/chrome/pdf/url', $payload); 37 | } 38 | 39 | /** 40 | * @param string $html 41 | * 42 | * @throws Api2PdfException 43 | * @return Api2PdfResult 44 | */ 45 | public function chromeHtmlToPdf($html, $inline = true, $filename = null, $options = null) 46 | { 47 | $payload = array_merge( 48 | $this->buildPayloadBase($inline, $filename, $options), 49 | [ 50 | 'html' => $html, 51 | ] 52 | ); 53 | 54 | return $this->makeRequest('/chrome/pdf/html', $payload); 55 | } 56 | 57 | /** 58 | * @param string $url 59 | * @throws Api2PdfException 60 | * @return Api2PdfResult 61 | */ 62 | public function chromeUrlToImage($url, $inline = true, $filename = null, $options = null) 63 | { 64 | $payload = array_merge( 65 | $this->buildPayloadBase($inline, $filename, $options), 66 | [ 67 | 'url' => $url, 68 | ] 69 | ); 70 | return $this->makeRequest('/chrome/image/url', $payload); 71 | } 72 | 73 | /** 74 | * @param string $html 75 | * 76 | * @throws Api2PdfException 77 | * @return Api2PdfResult 78 | */ 79 | public function chromeHtmlToImage($html, $inline = true, $filename = null, $options = null) 80 | { 81 | $payload = array_merge( 82 | $this->buildPayloadBase($inline, $filename, $options), 83 | [ 84 | 'html' => $html, 85 | ] 86 | ); 87 | 88 | return $this->makeRequest('/chrome/image/html', $payload); 89 | } 90 | 91 | /** 92 | * @param string $url 93 | * 94 | * @throws Api2PdfException 95 | * @return Api2PdfResult 96 | */ 97 | public function wkUrlToPdf($url, $inline = true, $filename = null, $options = null, $enableToc = false) 98 | { 99 | $payload = array_merge( 100 | $this->buildPayloadBase($inline, $filename, $options), 101 | [ 102 | 'url' => $url, 103 | 'enableToc' => $enableToc 104 | ] 105 | ); 106 | 107 | return $this->makeRequest('/wkhtml/pdf/url', $payload); 108 | } 109 | 110 | /** 111 | * @param string $html 112 | * 113 | * @throws Api2PdfException 114 | * @return Api2PdfResult 115 | */ 116 | public function wkHtmlToPdf($html, $inline = true, $filename = null, $options = null, $enableToc = false) 117 | { 118 | $payload = array_merge( 119 | $this->buildPayloadBase($inline, $filename, $options), 120 | [ 121 | 'html' => $html, 122 | 'enableToc' => $enableToc 123 | ] 124 | ); 125 | 126 | return $this->makeRequest('/wkhtml/pdf/html', $payload); 127 | } 128 | 129 | /** 130 | * @param string $url 131 | * 132 | * @throws Api2PdfException 133 | * @return Api2PdfResult 134 | */ 135 | public function libreOfficeAnyToPdf($url, $inline = true, $filename = null) 136 | { 137 | $payload = array_merge( 138 | $this->buildPayloadBase($inline, $filename), 139 | [ 140 | 'url' => $url, 141 | ] 142 | ); 143 | 144 | return $this->makeRequest('/libreoffice/any-to-pdf', $payload); 145 | } 146 | 147 | /** 148 | * @param string $url 149 | * 150 | * @throws Api2PdfException 151 | * @return Api2PdfResult 152 | */ 153 | public function libreOfficeThumbnail($url, $inline = true, $filename = null) 154 | { 155 | $payload = array_merge( 156 | $this->buildPayloadBase($inline, $filename), 157 | [ 158 | 'url' => $url, 159 | ] 160 | ); 161 | 162 | return $this->makeRequest('/libreoffice/thumbnail', $payload); 163 | } 164 | 165 | /** 166 | * @param string $url 167 | * 168 | * @throws Api2PdfException 169 | * @return Api2PdfResult 170 | */ 171 | public function libreOfficePdfToHtml($url, $inline = true, $filename = null) 172 | { 173 | $payload = array_merge( 174 | $this->buildPayloadBase($inline, $filename), 175 | [ 176 | 'url' => $url, 177 | ] 178 | ); 179 | 180 | return $this->makeRequest('/libreoffice/pdf-to-html', $payload); 181 | } 182 | 183 | /** 184 | * @param string $url 185 | * 186 | * @throws Api2PdfException 187 | * @return Api2PdfResult 188 | */ 189 | public function libreOfficeHtmlToDocx($url, $inline = true, $filename = null) 190 | { 191 | $payload = array_merge( 192 | $this->buildPayloadBase($inline, $filename), 193 | [ 194 | 'url' => $url, 195 | ] 196 | ); 197 | 198 | return $this->makeRequest('/libreoffice/html-to-docx', $payload); 199 | } 200 | 201 | /** 202 | * @param string $url 203 | * 204 | * @throws Api2PdfException 205 | * @return Api2PdfResult 206 | */ 207 | public function libreOfficeHtmlToXlsx($url, $inline = true, $filename = null) 208 | { 209 | $payload = array_merge( 210 | $this->buildPayloadBase($inline, $filename), 211 | [ 212 | 'url' => $url, 213 | ] 214 | ); 215 | 216 | return $this->makeRequest('/libreoffice/html-to-xlsx', $payload); 217 | } 218 | 219 | /** 220 | * @param array $urls 221 | * 222 | * @throws Api2PdfException 223 | * @return Api2PdfResult 224 | */ 225 | public function pdfsharpMerge($urls, $inline = true, $filename = null) 226 | { 227 | $payload = array_merge( 228 | $this->buildPayloadBase($inline, $filename), 229 | [ 230 | 'urls' => $urls, 231 | ] 232 | ); 233 | 234 | return $this->makeRequest('/pdfsharp/merge', $payload); 235 | } 236 | 237 | /** 238 | * @param $url 239 | * @param $bookmarks 240 | * @throws Api2PdfException 241 | * @return Api2PdfResult 242 | */ 243 | public function pdfsharpAddBookmarks($url, $bookmarks, $inline = true, $filename = null) 244 | { 245 | $payload = array_merge( 246 | $this->buildPayloadBase($inline, $filename), 247 | [ 248 | 'url' => $url, 249 | 'bookmarks' => $bookmarks 250 | ] 251 | ); 252 | 253 | return $this->makeRequest('/pdfsharp/bookmarks', $payload); 254 | } 255 | 256 | /** 257 | * @param $url 258 | * @param $bookmarks 259 | * @throws Api2PdfException 260 | * @return Api2PdfResult 261 | */ 262 | public function pdfsharpAddPassword($url, $userpassword, $ownerpassword = null, $inline = true, $filename = null) 263 | { 264 | $payload = array_merge( 265 | $this->buildPayloadBase($inline, $filename), 266 | [ 267 | 'url' => $url, 268 | 'userpassword' => $userpassword 269 | ] 270 | ); 271 | 272 | if (!is_null($ownerpassword)) { 273 | $payload['ownerpassword'] = $ownerpassword; 274 | } 275 | 276 | return $this->makeRequest('/pdfsharp/password', $payload); 277 | } 278 | 279 | /** 280 | * @param string $responseId 281 | * 282 | * @return ApiResult 283 | * @throws ConversionException 284 | * @throws ProtocolException 285 | */ 286 | public function utilityDelete($responseId) 287 | { 288 | $url = $this->base_url . '/file/'. $responseId; 289 | 290 | $ch = curl_init($url); 291 | 292 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); 293 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 294 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 295 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 296 | 297 | curl_setopt( 298 | $ch, 299 | CURLOPT_HTTPHEADER, 300 | [ 301 | 'Content-Type: application/json', 302 | 'Authorization: '.$this->apiKey 303 | ] 304 | ); 305 | 306 | $response = curl_exec($ch); 307 | 308 | if ($response === false) { 309 | throw new Api2PdfException(curl_error($ch) ?: 'API request failed'); 310 | } 311 | 312 | return Api2PdfResult::createFromResponse($response); 313 | } 314 | 315 | /** 316 | * @param $endpoint 317 | * @param $payload 318 | * @throws Api2PdfException 319 | * @return Api2PdfResult 320 | */ 321 | private function makeRequest($endpoint, $payload) 322 | { 323 | $url = $this->base_url . $endpoint; 324 | 325 | $ch = curl_init($url); 326 | 327 | $jsonDataEncoded = json_encode($payload); 328 | 329 | curl_setopt($ch, CURLOPT_POST, 1); 330 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 331 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); 332 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 333 | curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); 334 | 335 | curl_setopt( 336 | $ch, 337 | CURLOPT_HTTPHEADER, 338 | [ 339 | 'Content-Type: application/json', 340 | 'Authorization: '.$this->apiKey 341 | ] 342 | ); 343 | 344 | $response = curl_exec($ch); 345 | 346 | if ($response === false) { 347 | throw new Api2PdfException(curl_error($ch) ?: 'API request failed'); 348 | } 349 | 350 | return Api2PdfResult::createFromResponse($response); 351 | } 352 | 353 | /** 354 | * @return array 355 | */ 356 | private function buildPayloadBase($inline, $filename, $options = null) 357 | { 358 | $payload = [ 359 | 'inline' => $inline, 360 | ]; 361 | 362 | if (!is_null($filename)) { 363 | $payload["fileName"] = $filename; 364 | } 365 | 366 | if (!is_null($options) && !empty($options)) { 367 | $payload["options"] = $options; 368 | } 369 | 370 | return $payload; 371 | } 372 | } 373 | -------------------------------------------------------------------------------- /src/Api2PdfException.php: -------------------------------------------------------------------------------- 1 | file = isset($data['FileUrl'])?$data['FileUrl']: null; 55 | $apiResponse->seconds = $data['Seconds']; 56 | $apiResponse->mbOut = $data['MbOut']; 57 | $apiResponse->cost = $data['Cost']; 58 | $apiResponse->responseId = $data['ResponseId']; 59 | $apiResponse->raw_json = $response; 60 | 61 | return $apiResponse; 62 | } 63 | 64 | /** 65 | * @return string|null 66 | */ 67 | public function getFile() 68 | { 69 | return $this->file; 70 | } 71 | 72 | /** 73 | * @return string|null 74 | */ 75 | public function getFileContents() 76 | { 77 | return $this->file ? file_get_contents($this->file) : null; 78 | } 79 | 80 | public function getJson() 81 | { 82 | return $this->raw_json; 83 | } 84 | 85 | /** 86 | * @return float|null 87 | */ 88 | public function getSeconds() 89 | { 90 | return $this->seconds; 91 | } 92 | 93 | /** 94 | * @return float|null 95 | */ 96 | public function getMbOut() 97 | { 98 | return $this->mbOut; 99 | } 100 | 101 | /** 102 | * @return float|null 103 | */ 104 | public function getCost() 105 | { 106 | return $this->cost; 107 | } 108 | 109 | /** 110 | * @return string|null 111 | */ 112 | public function getResponseId() 113 | { 114 | return $this->responseId; 115 | } 116 | } 117 | --------------------------------------------------------------------------------