Blog

Back to blog posts

Generate Custom PDFs from HTML with ScriptCase and Api2Pdf

Published Jul 21, 2018

Intro

ScriptCase is a web development platform built on PHP that helps you put together web apps really quickly with less code. Connect to your database of choice, and start generating graphs, reports, and anything else you need. ScriptCase is great because it allows you to generate PDFs out of your stored data. You can even have some flexibility in the layout of the PDF.

However, even with some of the customizations, it may not be enough. If you truly need a completely custom PDF output that is not supported by what ScriptCase offers out of the box, your only option is to write some raw PHP code. The only problem is, if you deploy your PHP application, trying to use a local installation of wkhtmltopdf or headless chrome may simply not be an option. This is where Api2Pdf comes in. You can write your PHP code to generate whatever custom HTML output you are looking for, and then send that to our API to convert that HTML to a PDF for you. Below, we will provide instructions on how to create a custom PHP file to connect to Api2Pdf in ScriptCase.

Step 1

Log into your ScriptCase application and go to Tools > External Libraries. Click Create a new library. Make sure the library is public and give it a name: Api2Pdf

Step 2

The new library should be listed. Click Use library. Then click Edit.

Step 3

Click New file. Give the file a name, such as ‘api2pdf.php’. Click on the newly created file to edit it.

Step 4

In your new file, copy and paste the code below and click Save.

 

<?php
function wkhtmltopdf_to_pdf($apikey, $payload) {
$endpoint = "https://v2018.api2pdf.com/wkhtmltopdf/html";
//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);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//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: '.$apikey));
//Execute the request
$json_result = curl_exec($ch);
if ($json_result === false) {
$info = curl_getinfo($ch);
curl_close($ch);
die('error occured during curl exec. Additional info: ' . var_export($info));
}
curl_close($ch);
$result = json_decode($json_result);
return $result;
}
?>

 

Step 5

Now create a blank PHP template in your application and use the following sample code below. Replace YOUR-API-KEY with your Api2Pdf.com API Key. If you need an API key, you can create one by signing up here.

 

require('../_lib/libraries/sys/api2pdf/api2pdf.php'); //link this to your external library, whatever your file is named.
$apikey = "YOUR-API-KEY";
$payload = array("html"=>"<p>Hello World</p>", "inlinePdf"=> true, "fileName"=> "test.pdf");
$res = wkhtmltopdf_to_pdf($apikey, $payload);
header("Location: ".$res->pdf); //this redirects to the PDF. Remove this if you don't want to do that.

 

Run your application to test it out and generate a PDF!