Skip to content

Instantly share code, notes, and snippets.

@JasvinderSingh1
Last active July 23, 2019 07:20
Show Gist options
  • Select an option

  • Save JasvinderSingh1/d952fff072c92c92041816056e85529d to your computer and use it in GitHub Desktop.

Select an option

Save JasvinderSingh1/d952fff072c92c92041816056e85529d to your computer and use it in GitHub Desktop.
API2PDF
<?php
define("API2PDF_BASE_ENDPOINT", 'https://v2018.api2pdf.com');
define("API2PDF_MERGE", API2PDF_BASE_ENDPOINT.'/merge');
define("API2PDF_WKHTMLTOPDF_HTML", API2PDF_BASE_ENDPOINT.'/wkhtmltopdf/html');
define("API2PDF_WKHTMLTOPDF_URL", API2PDF_BASE_ENDPOINT.'/wkhtmltopdf/url');
define("API2PDF_CHROME_HTML", API2PDF_BASE_ENDPOINT.'/chrome/html');
define("API2PDF_CHROME_URL", API2PDF_BASE_ENDPOINT.'/chrome/url');
define("API2PDF_LIBREOFFICE_CONVERT", API2PDF_BASE_ENDPOINT.'/libreoffice/convert');
class Api2PdfLibrary {
var $apikey;
function __construct($apikey) {
$this->apikey = $apikey;
}
public function headless_chrome_from_url($url, $inline = false, $filename = null, $options = null) {
$payload = array("url"=>$url, "inlinePdf"=>$inline);
if ($filename != null) {
$payload["fileName"] = $filename;
}
if ($options != null) {
$payload["options"] = $options;
}
return $this->_make_request(API2PDF_CHROME_URL, $payload);
}
public function headless_chrome_from_html($html, $inline = false, $filename = null, $options = null) {
$payload = array("html"=>$html, "inlinePdf"=>$inline);
if ($filename != null) {
$payload["fileName"] = $filename;
}
if ($options != null) {
$payload["options"] = $options;
}
return $this->_make_request(API2PDF_CHROME_HTML, $payload);
}
public function wkhtmltopdf_from_url($url, $inline = false, $filename = null, $options = null) {
$payload = array("url"=>$url, "inlinePdf"=>$inline);
if ($filename != null) {
$payload["fileName"] = $filename;
}
if ($options != null) {
$payload["options"] = $options;
}
return $this->_make_request(API2PDF_WKHTMLTOPDF_URL, $payload);
}
public function wkhtmltopdf_from_html($html, $inline = false, $filename = null, $options = null) {
$payload = array("html"=>$html, "inlinePdf"=>$inline);
if ($filename != null) {
$payload["fileName"] = $filename;
}
if ($options != null) {
$payload["options"] = $options;
}
return $this->_make_request(API2PDF_WKHTMLTOPDF_HTML, $payload);
}
public function merge($urls, $inline = false, $filename = null) {
$payload = array("urls"=>$urls, "inlinePdf"=>$inline);
if ($filename != null) {
$payload["fileName"] = $filename;
}
return $this->_make_request(API2PDF_MERGE, $payload);
}
public function libreoffice_convert($url, $inline = false, $filename = null) {
$payload = array("url"=>$url, "inlinePdf"=>$inline);
if ($filename != null) {
$payload["fileName"] = $filename;
}
return $this->_make_request(API2PDF_LIBREOFFICE_CONVERT, $payload);
}
private function _make_request($endpoint, $payload) {
//Initiate cURL.
$ch = curl_init($endpoint);
//Encode the array into JSON.
$jsonDataEncoded = json_encode($payload);
//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: '.$this->apikey));
//Execute the request
$json_result = curl_exec($ch);
if($json_result === false) {
throw new \Exception(curl_error($ch), 1);
}
$result = json_decode($json_result);
return $result;
}
}
<?php
include('wp-load.php');
require('api2pdf.php');
/*$included_files = get_included_files();
print_r($included_files);*/
$a2p_client = new Api2PdfLibrary('f1dd0642-413c-4c21-8489-9003b8e998ed');
//$options = array("orientation"=>"landscape", "pageSize"=>"A4", "enableJavascript"=>true, "javascriptDelay"=>"3000");
//$options = array("paperWidth"=>12.20, "paperHeight"=>9.40, "marginBottom"=>0, "marginLeft"=>0, "marginRight"=>0, "marginTop"=>0, "footerTemplate"=>'<span class="pageNumber"></span>');
$options = array("paperWidth"=>12.20, "paperHeight"=>9.40, "marginBottom"=>0, "marginLeft"=>0, "marginRight"=>0, "marginTop"=>0, "displayHeaderFooter"=> true, "footerTemplate"=> '<div class="page-footer" style="width:100%; text-align:right; font-size:12px;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>');
$api_response = $a2p_client->headless_chrome_from_url('https://churchtech.com/deluxe-survey-report/?ppi=1cde67e3c8', $inline = true, $filename = 'test-report2.pdf', $options = $options);
//print_r($api_response);
//echo $api_response->pdf;
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data($api_response->pdf);
//echo $returned_content;
fopen("wp-content/uploads/pdfs/testreport.pdf", 'w');
$myfile = fopen("wp-content/uploads/pdfs/testreport.pdf", 'w') or die("Unable to open file!");
fwrite($myfile, $returned_content);
fclose($myfile);
$myfile2 = 'wp-content/uploads/pdfs/testreport.pdf';
$body = "Survey Report API2PDF";
$headers = array('Content-Type: text/html; charset=UTF-8');
$headers[] = 'From: ChurchTech <[email protected]>';
$attachments = array( $myfile2 );
wp_mail('[email protected]', "Survey Report", $body, $headers, $attachments );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment