SelectPdf Online REST API is a platform independent PDF manipulation API. SelectPdf REST API is cloud based and it can be used with any language: .NET (C# or VB.NET), Java, PHP, Python, Go, Ruby, Node.js, Perl and many more. We are presenting today the dedicated PHP client library for SelectPdf API.
SelectPdf PHP client library can be used to take advance of the features offered by SelectPdf Online REST API:
HTML to PDF REST API – Use SelectPdf HTML To PDF Online REST API to generate PDF documents from web page urls or raw HTML code.
PDF to TEXT REST API – SelectPdf Pdf To Text REST API is a cloud based solution that can be used to extract text from PDF documents or search PDF documents for specific words.
PDF Merge REST API – SelectPdf Pdf Merge REST API is an online solution that can be used to merge local or remote PDFs into a final PDF document.
All these features can be easily integrated into your PHP applications using the dedicated PHP client library.
Installation
Download selectpdf-api-php-client-1.4.0.zip, unzip it and require SelectPdf.Api.php in your code.
OR
Install SelectPdf PHP Client for Online API from Packagist: SelectPdf API on Packagist.
OR
Clone selectpdf-api-php-client from Github and require SelectPdf.Api.php in your code.
cd selectpdf-api-php-client
Get a trial key for SelectPdf online REST API
Once the library is installed, you need a key to be able to access the API.
GET A DEMO LICENSE KEY NOW
The free trial key for the online API is valid for 7 days and it includes 200 conversions.
Sample Code
The PHP client library makes accessing SelectPdf online REST API very easy. Here are a few samples that present the main features of the API. For details and full list of parameters access the individual pages of the APIs: HTML to PDF API or PDF to TEXT API or PDF Merge API.
Convert HTML to PDF in PHP
The following sample shows the main features of the HTML To PDF API. Comment/uncomment code to convert an url to file or memory or also convert raw HTML to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
<?php require("SelectPdf.Api.php"); $url = 'https://selectpdf.com'; $localFile = "Test.pdf"; $apiKey = "Your API key here"; echo ("This is SelectPdf-" . SelectPdf\Api\ApiClient::CLIENT_VERSION . ".\n"); try { $client = new SelectPdf\Api\HtmlToPdfClient($apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ $client // main properties ->setPageSize(SelectPdf\Api\PageSize::A4) // PDF page size ->setPageOrientation(SelectPdf\Api\PageOrientation::Portrait) // PDF page orientation ->setMargins(0) // PDF page margins ->setRenderingEngine(SelectPdf\Api\RenderingEngine::WebKit) // rendering engine ->setConversionDelay(1) // conversion delay ->setNavigationTimeout(30) // navigation timeout ->setShowPageNumbers(false) // page numbers ->setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // additional properties // ->setUseCssPrint(true) // enable CSS media print // ->setDisableJavascript(true) // disable javascript // ->setDisableInternalLinks(true) // disable internal links // ->setDisableExternalLinks(true) // disable external links // ->setKeepImagesTogether(true) // keep images together // ->setScaleImages(true) // scale images to create smaller pdfs // ->setSinglePagePdf(true) // generate a single page PDF // ->setUserPassword("password") // secure the PDF with a password // generate automatic bookmarks // ->setPdfBookmarksSelectors("H1, H2") // create outlines (bookmarks) for the specified elements // ->setViewerPageMode(SelectPdf\Api\PageMode::UseOutlines) // display outlines (bookmarks) in viewer ; echo ("Starting conversion ...\n"); // convert url to file $client->convertUrlToFile($url, $localFile); // convert url to memory // $pdf = $client->convertUrl($url); // convert html string to file // $client->convertHtmlStringToFile("This is some <b>html</b>.", $localFile); // convert html string to memory // $pdf = $client->convertHtmlString("This is some <b>html</b>."); echo ("Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"); // get API usage $usageClient = new \SelectPdf\Api\UsageClient($apiKey); $usage = $usageClient->getUsage(true); echo("Conversions remained this month: " . $usage["available"] . ".\n"); } catch (Exception $ex) { echo("An error occurred: " . $ex . ".\n"); } ?> |
Convert HTML to PDF with custom header/footer in PHP
The following sample shows how to convert a web page to PDF and also setting a custom header or footer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php require("SelectPdf.Api.php"); $url = 'https://selectpdf.com'; $localFile = "Test.pdf"; $apiKey = "Your API key here"; echo ("This is SelectPdf-" . SelectPdf\Api\ApiClient::CLIENT_VERSION . ".\n"); try { $client = new SelectPdf\Api\HtmlToPdfClient($apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ $client ->setMargins(0) // PDF page margins ->setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // header properties ->setShowHeader(true) // display header // ->setHeaderHeight(50) // header height // ->setHeaderUrl($url) // header url ->setHeaderHtml("This is the <b>HEADER</b>!!!!") // header html // footer properties ->setShowFooter(true) // display footer // ->setFooterHeight(60) // footer height // ->setFooterUrl($url) // footer url ->setFooterHtml("This is the <b>FOOTER</b>!!!!") // footer html // footer page numbers ->setShowPageNumbers(true) // show page numbers in footer ->setPageNumbersTemplate("{page_number} / {total_pages}") // page numbers template ->setPageNumbersFontName("Verdana") // page numbers font name ->setPageNumbersFontSize(12) // page numbers font size ->setPageNumbersAlignment(SelectPdf\Api\PageNumbersAlignment::Center) // page numbers alignment (2-Center) ; echo ("Starting conversion ...\n"); // convert url to file $client->convertUrlToFile($url, $localFile); // convert url to memory // $pdf = $client->convertUrl($url); // convert html string to file // $client->convertHtmlStringToFile("This is some <b>html</b>.", $localFile); // convert html string to memory // $pdf = $client->convertHtmlString("This is some <b>html</b>."); echo ("Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"); // get API usage $usageClient = new \SelectPdf\Api\UsageClient($apiKey); $usage = $usageClient->getUsage(true); echo("Conversions remained this month: " . $usage["available"] . ".\n"); } catch (Exception $ex) { echo("An error occurred: " . $ex . ".\n"); } ?> |
Extract text from PDF in PHP
The following sample shows how to extract the text from a PDF document using SelectPdf API. Comment/uncomment code to convert a local PDF or a PDF from a remote url to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php require("SelectPdf.Api.php"); $testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; $testPdf = "Input.pdf"; $localFile = "Result.txt"; $apiKey = "Your API key here"; echo ("This is SelectPdf-" . SelectPdf\Api\ApiClient::CLIENT_VERSION . ".\n"); try { $client = new SelectPdf\Api\PdfToTextClient($apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ $client ->setStartPage(1) // start page (processing starts from here) ->setEndPage(0) // end page (set 0 to process file til the end) ->setOutputFormat(SelectPdf\Api\OutputFormat::Text) // set output format (0-Text or 1-HTML) ; echo ("Starting pdf to text ...\n"); // convert local pdf to local text file $client->getTextFromFileToFile($testPdf, $localFile); // extract text from local pdf to memory // $text = $client->getTextFromFile($testPdf); // print text // echo($text); // convert pdf from public url to local text file // $client->getTextFromUrlToFile($testUrl, $localFile); // extract text from pdf from public url to memory // $text = $client->getTextFromUrl($testUrl); // print text // echo($text); echo ("Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"); // get API usage $usageClient = new \SelectPdf\Api\UsageClient($apiKey); $usage = $usageClient->getUsage(true); echo("Conversions remained this month: " . $usage["available"] . ".\n"); } catch (Exception $ex) { echo("An error occurred: " . $ex . ".\n"); } ?> |
Search for text in PDF using PHP
The following sample shows how to search a PDF document for a specific text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<?php require("SelectPdf.Api.php"); $testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; $testPdf = "Input.pdf"; $apiKey = "Your API key here"; echo ("This is SelectPdf-" . SelectPdf\Api\ApiClient::CLIENT_VERSION . ".\n"); try { $client = new SelectPdf\Api\PdfToTextClient($apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ $client ->setStartPage(1) // start page (processing starts from here) ->setEndPage(0) // end page (set 0 to process file til the end) ->setOutputFormat(SelectPdf\Api\OutputFormat::Text) // set output format (0-Text or 1-HTML) ; echo ("Starting search pdf ...\n"); // search local pdf $results = $client->searchFile($testPdf, "pdf"); // search pdf from public url // $results = $client->searchUrl($testUrl, "pdf"); // print results $search_results_count = count($results); $search_results_string = json_encode($results, JSON_PRETTY_PRINT); echo ("Search results:\n$search_results_string\nSearch results count: $search_results_count.\n"); echo ("Finished! Number of pages processed: " . $client->getNumberOfPages() . ".\n"); // get API usage $usageClient = new \SelectPdf\Api\UsageClient($apiKey); $usage = $usageClient->getUsage(true); echo("Conversions remained this month: " . $usage["available"] . ".\n"); } catch (Exception $ex) { echo("An error occurred: " . $ex . ".\n"); } ?> |
Merge PDFs using PHP
The following sample shows how merge several PDF documents into a final file. The source PDFs can be local files or PDFs from remote urls. The final PDF can be retrieved in memory or saved to a local file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php require("SelectPdf.Api.php"); $testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; $testPdf = "Input.pdf"; $localFile = "Result.pdf"; $apiKey = "Your API key here"; echo ("This is SelectPdf-" . SelectPdf\Api\ApiClient::CLIENT_VERSION . ".\n"); try { $client = new SelectPdf\Api\PdfMergeClient($apiKey); // set parameters - see full list at https://selectpdf.com/pdf-merge-api/ $client // specify the pdf files that will be merged (order will be preserved in the final pdf) ->addFile($testPdf) // add PDF from local file ->addUrlFile($testUrl) // add PDF From public url // ->addFile($testPdf, "pdf_password") // add PDF (that requires a password) from local file // ->addUrlFile($testUrl, "pdf_password") // add PDF (that requires a password) from public url ; echo ("Starting pdf merge ...\n"); // merge pdfs to local file $client->saveToFile($localFile); // merge pdfs to memory // $pdf = $client->save(); echo ("Finished! Number of pages: " . $client->getNumberOfPages() . ".\n"); // get API usage $usageClient = new \SelectPdf\Api\UsageClient($apiKey); $usage = $usageClient->getUsage(true); echo("Conversions remained this month: " . $usage["available"] . ".\n"); } catch (Exception $ex) { echo("An error occurred: " . $ex . ".\n"); } ?> |
The above PHP samples can also be found in GitHub repository: PHP Samples.