PHP code example of selectpdf / selectpdf-api-client
1. Go to this page and download the library: Download selectpdf/selectpdf-api-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
selectpdf / selectpdf-api-client example snippets
o API key -> keyless demo mode (5-page cap, watermarked output, Chromium engine).
$client = new SelectPdf\Api\HtmlToPdfClient(); // also: new SelectPdf\Api\HtmlToPdfClient("demo")
$client->convertUrlToFile("https://example.com", "demo.pdf");
echo ("Pages: " . $client->getNumberOfPages() . "\n");
if ($client->wasClamped())
echo ("Server clamped: " . implode(",", $client->getClampedFields()) . "\n");
// Upgrade for unwatermarked, unlimited-page PDFs: https://selectpdf.com/pricing/
{
$client = new SelectPdf\Api\HtmlToPdfClient(); // demo mode
$pdf = $client->convertUrl("https://example.com");
}
catch (SelectPdf\Api\DemoRateLimitException $ex) {
// getReason(): "per_ip" | "daily_cap" | "concurrency"
echo ("Rate limited: " . $ex->getReason() . ", retry after " . $ex->getRetryAfter() . "s\n");
echo ("Upgrade: " . $ex->getUpgradeUrl() . "\n");
}
catch (SelectPdf\Api\DemoSafetyException $ex) {
// The supplied URL was rejected. Use a paid endpoint for non-public targets.
echo ("URL rejected: " . $ex->getField() . " (" . $ex->getReason() . ")\n");
}
catch (SelectPdf\Api\DemoUnsupportedException $ex) {
// getField(): which parameter is unavailable in demo mode
echo ("'" . $ex->getField() . "' not available in demo mode. Upgrade: " . $ex->getUpgradeUrl() . "\n");
}
catch (SelectPdf\Api\ApiException $ex) {
echo ("API error: " . $ex . "\n");
}
$client = new SelectPdf\Api\HtmlToPdfClient("Your API key here");
= '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");
}
$client
->setRenderingEngine(SelectPdf\Api\RenderingEngine::Chromium) // WebKit, Restricted, Blink or Chromium
->setWebPageHeight(800) // browser window height in pixels
->setWebPageFixedSize(true) // cut the page at the web page height (false: convert the whole page)
->setAuthUsername("user") // HTTP Basic authentication on the converted page
->setAuthPassword("password")
->setCookies(array("name" => "value")) // HTTP cookies sent to the converted page
;
$client->AsyncCallsPingInterval = 3; // seconds between polls
$client->AsyncCallsMaxPings = 1000; // max polls before timeout
$client->convertUrlToFileAsync("https://selectpdf.com", "Test.pdf");
// $pdf = $client->convertUrlAsync("https://selectpdf.com");
// $client->convertHtmlStringToFileAsync("This is some <b>html</b>.", "Test.pdf");
ent = new SelectPdf\Api\InvoiceClient("Your API key here");
$client
->setInvoiceXmlFile("factur-x.xml") // or setInvoiceXml($xmlString)
->setZugferdProfile(SelectPdf\Api\ZugferdProfile::En16931)
;
$client->createFromHtmlStringToFile($invoiceHtml, "Invoice.pdf");
// ... or from the invoice page your application already renders:
// $client->createFromUrlToFile("https://your-app.example/invoices/INV-2026-001", "Invoice.pdf");
echo ("Pages: " . $client->getNumberOfPages() . ".\n");
tUrl = "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
tUrl = "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");
}
tUrl = "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");
}
git clone https://github.com/selectpdf/selectpdf-api-php-client
cd selectpdf-api-php-client
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.