php,curl,html,pdf

 

The good news is that you can easily use cURL with our HTML PDF API service. But what is cURL and how can you use it? If you ask yourself these questions you're on right place at the right time. Let's get started.

cURL is a software project providing CLI tool and a library for transferring data using various protocols such as HTTP, HTTPS, FTP, FTPS, SFTP, etc. The cURL project consists of two products:

  1. cURL - CLI tool
  2. libcurl - free client-side URL transfer library

Use cURL from CLI

If you're using UNIX-based OS (Linux or Mac) you can retrieve pure HTML of any page, if you type:

curl htmlpdfapi.com/examples/simple.html

You will get pure HTML and you will be able to see it in our CLI.

If you're using Windows, you will need to install custom tools like Cygwin to be able to use cURL from the command line. This is a completely different story and you will not waste your precious time. You will be eager to use cURL from PHP as soon as possible ;)

Use cURL in PHP

In PHP world cURL is a library created by Sterling Hughes that allows you to communicate with servers using a different type of protocols. By default, cURL isn't enabled in Apache and cURL calls won't work until you've enabled it.

Check if PHP cURL enabled

To check out if cURL is enabled or disabled with PHP you can try one of these two ways:

1. Create a script test-curl-1.php, add following code and open page in your browser:

<?php 
  var_dump(function_exists('curl_version'));

or from CLI:

$ php test-curl-1.php

If an output is bool(false) cURL is disabled and you need to enable it.

2. If you prefer phpinfo function to output information about PHP you can create a script test-curl-2.php with the following code and open a page in your browser:

<?php 
  phpinfo();

Scroll down to the curl section and see if it is enabled. If you don't see curl section, like on the following picture, cURL is disabled and you need to enable it.

cURL section in PHP

How to enable PHP cURL?

There are a several ways to enable PHP cURL. It will depend on which OS you are running and which version of Apache you're using.

If you're using Apache 2, on Windows, the below, is the main method to enable cURL:

  1. Locate your php.ini file (XAMPP: C:\xampp\apache\bin\php.ini, WAMP: C:\wamp\bin\apache\Apache2.4.4\bin\php.ini)
  2. Open the php.ini in your favorite editor (Notepad++, Sublime)
  3. Search the following line
    ;extension=php_curl.dll
  4. Uncomment this line by removing the semicolon ;
    extension=php_curl.dll
  5. Save php.ini file
  6. Restart Apache service

If you're running Apache 2 on Ubuntu/Debian:

  1. Install cURL with the following command
    sudo apt-get install php5-curl
  2. Restart Apache
    sudo service apache2 restart

Once you have enabled PHP cURL you can run test-curl-1.php script again and now you'll see bool(true) output.

VOILA!!! Congratulations! You have successfully enabled PHP cURL.

Use cURL from PHP

Now you can use cURL from PHP. Create curl-example.php add following code:

<?php

  // Get cURL resource
  $curl = curl_init();

  // Set some options
  curl_setopt_array($curl, array(
    CURLOPT_URL => 'https://htmlpdfapi.com/examples/simple.html'
  ));

  // Send the request & save response to $resp
  $resp = curl_exec($curl);

  // Output results
  echo $resp;

  // Close request to clear up some resources
  curl_close($curl);

If you open the page in a browser, you will get the exact same page as you have accessed it from the browser. If you don't believe me go to the simple page. How? You've sent the request to the server, by cURL, and got back the response with pure HTML. Finally, you print HTML on our page.

Also, you can run the script from CLI and get the same result like in Use cURL from command line section:

php curl-example.php

Use PHP cURL with our HTML to PDF API service

Now, when you know how to use cURL from PHP let's try some serious examples that include our HTML PDF API service.

You can send data to HTML PDF API via HTML POST request. To generate PDF, you can use one of the following methods:

  1. URL
  2. HTML file
  3. HTML string
  4. ZIP file

First, you have to define parameters for the request. You can find the list of available parameters, as well as their description, on HTML PDF API documentation page.

You can download all scripts used in the examples as a ZIP fileDon't forget to change the authentication token!!!

Generate PDF from URL

If you want to generate PDF file directly from URL, you must provide the 'url' parameter. In that case, 'html' and 'file' parameters must not be set. In this example, you'll send data by URL from a remote HTML file and save the results in PDF file. Create a curl-url.php script and add following code:

<?php

  // Get cURL resource
  $curl = curl_init();

  // Set some options
  curl_setopt_array($curl, array(
    CURLOPT_HTTPHEADER => array(
      'Authentication: Token <your token>' // Don't forget to change the token!!!
    ),
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://htmlpdfapi.com/api/v1/pdf',
    CURLOPT_POST => 1,

    // URL
    CURLOPT_POSTFIELDS => array(
      'url' => 'https://htmlpdfapi.com/examples/example.html'
    )
  ));

  // Send the request & save response to $resp
  $resp = curl_exec($curl);

  //Save file
  file_put_contents('result.pdf', $resp);

  // Close request to clear up some resources
  curl_close($curl);

Run a script from a browser or CLI (php curl-url.php) and you'll see the result.pdf file in your current directory.

You can send an array with extra options as a second parameter of the curl_setopt_array function:

  • CURLOPT_HTTPHEADER - Custom HTTP header with user token.
  • CURLOPT_RETURNTRANSFER - This will return the result output as a string. Without this, the output would be directly echoed to the screen or STDOUT.
  • CURLOPT_URL - The URL of our API.
  • CURLOPT_POSTFIELDS - Additional data for HTTP POST request. In this example the URL of HTML file.

Generate PDF from file

If you want to generate PDF from a file, you must provide the 'file' parameter. In that case 'html' and 'url' parameters must not be set. This way you'll send HTML file and save the result in PDF file. Then, create example.html with HTML and curl-file.php script and add following code:

<?php

  if(!file_exists('example.html')) {
    die('Please create or download example.html file. (https://htmlpdfapi.com/examples/example.html)');
  }

  // For PHP < 5.5:
  if (!function_exists('curl_file_create')) {
    function curl_file_create($filename, $mimetype = '', $postname = '') {
      return "@$filename;filename="
          . ($postname ? $postname : basename($filename))
          . ($mimetype ? ";type=$mimetype" : '');
    }
  }

  // Get cURL resource
  $curl = curl_init();
  
  // Set some options
  curl_setopt_array($curl, array(
    CURLOPT_HTTPHEADER => array(
      'Authentication: Token <your token>' // Don't forget to change the token!!!
    ),
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://htmlpdfapi.com/api/v1/pdf',
    CURLOPT_POST => 1,

    // URL
    CURLOPT_POSTFIELDS => array(
      'file' => curl_file_create('example.html')
    )
  ));

  // Send the request & save response to $resp
  $resp = curl_exec($curl);

  //Save file
  file_put_contents('result.pdf', $resp);

  // Close request to clear up some resources
  curl_close($curl);

First you define curl_file_create function if you're using PHP less than 5.5. You use file parameter in CURLOPT_POSTFIELDS array to send a file to the server. 

Generate PDF from HTML string

If you want to generate PDF file from HTML string, you must provide the 'html' parameter. In that case 'file' and 'url' parameters must not be set. In this example you'll send HTML string and save the result in PDF file. Create a curl-html.php script and add following code:

<?php

  // Get cURL resource
  $curl = curl_init();
  // Set some options
  curl_setopt_array($curl, array(
    CURLOPT_HTTPHEADER => array(
      'Authentication: Token <your token>' // Don't forget to change the token!!!
    ),
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => 'https://htmlpdfapi.com/api/v1/pdf',
    CURLOPT_POST => 1,

    // URL
    CURLOPT_POSTFIELDS => array(
      'html' => '<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>Hello world!!!</body></html>'
    )
  ));

  // Send the request & save response to $resp
  $resp = curl_exec($curl);

  //Save file
  file_put_contents('result.pdf', $resp);

  // Close request to clear up some resources
  curl_close($curl);

You use html parameter in CURLOPT_POSTFIELDS array to send pure HTML to the server.

Please note that if you are using curl_setopt you need to use urlencode to escape special characters.
curl_setopt($curl, CURLOPT_POSTFIELDS,
  'html='.urlencode('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>Hello &amp; world!!!</body></html>')
);

Sending additional parameters

In all examples you could send additional parameters through CURLOPT_POSTFIELDS. 

CURLOPT_POSTFIELDS => array(
  'url' => 'https://htmlpdfapi.com/examples/example.html',
  'page_size' => 'A5',
  'margin_top' => 20
)

As we said, you can find the list of available parameters on HTML PDF API documentation page.

That’s it. You're done!

If you have any questions leave them below.

Download - PHP examples for HTML PDF API (ZIP)