Skip to content

Commit

Permalink
extracted PdfApi into its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
Athlon1600 committed Sep 15, 2019
1 parent b5c9121 commit 0a0dc48
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 1 deletion.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
# wkhtmltopdf-api-php-client
# wkhtmltopdf-api-php-client


`composer require ctbuh\wkhtmltopdf-api-php-client`


### API

- `convert($bytes, $options = array())`
- `inline($bytes, $options = array(), $filename = 'document.pdf')`
- `download($bytes, $options = array(), $filename = 'document.pdf')`


9 changes: 9 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "ctbuh/wkhtmltopdf-api-php-client",
"require": {},
"autoload": {
"psr-4": {
"ctbuh\\PdfApi\\": "src/"
}
}
}
95 changes: 95 additions & 0 deletions src/PdfApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace ctbuh\PdfApi;

class PdfApi
{
/**
* Just 'password'
* @var array
*/
private $options;

/**
* PdfApi constructor.
* @param array $options
*/
public function __construct($options = array())
{
$this->options = $options;
}

/**
* https://www.api2pdf.com/documentation/advanced-options-wkhtmltopdf/
* @param $input
* @param array $options
* @param bool $raw_response
* @return mixed|null
*/
public function convert($input, $options = array(), $raw_response = false)
{
$params = array(
'html_base64' => base64_encode($input),
'options' => $options
);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://wkhtmltopdf.api.ctbuh.org/v1/convert");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// some PDFs will take a while to generate
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300); // default
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // no timeout is default

curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Cache-Control: no-cache",
'Content-Type: application/x-www-form-urlencoded'
));

$pdf_response = curl_exec($ch);

/*
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error_str = curl_error($ch);
*/

curl_close($ch);

$json = json_decode($pdf_response, true);

if (is_array($json)) {
return base64_decode($json['pdf_base64']);
}

return null;
}

public function inline($input, $options = array(), $filename = 'document.pdf')
{
$pdf_bytes = $this->convert($input, $options);

if ($pdf_bytes) {

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');

echo $pdf_bytes;
}
}

public function download($input, $options = array(), $filename = 'document.pdf')
{
$pdf_bytes = $this->convert($input, $options);

if ($pdf_bytes) {

header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . $filename . '"');

echo $pdf_bytes;
}
}
}

0 comments on commit 0a0dc48

Please # to comment.