How to Convert JPG to PDF with PHP

Learn how to use pdfRest Convert to PDF API Tool with PHP to convert JPG images to PDF documents
Share this page

Why Convert JPG to PDF with PHP?

The pdfRest Convert to PDF API Tool offers a convenient way to programmatically convert various file types to PDF using a RESTful API. This tutorial will show you how to send an API call to Convert to PDF with PHP, which can be particularly useful when you need to integrate PDF conversion into your web applications or automate document workflows.

You might need to convert documents such as invoices, reports, or forms to PDF format to ensure consistency in presentation, maintain document integrity, and ensure compatibility across different platforms and devices. For example, a web application that allows users to upload documents and receive them as PDFs can benefit from this API by providing a seamless user experience and ensuring that the documents are easy to view and share.

Convert JPG to PDF with PHP Code Example

require 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

$client = new Client();

$headers = [
  'Api-Key' =--> 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

$options = [
  'multipart' => [
    [
      'name' => 'file',
      'contents' => Utils::tryFopen('/path/to/file', 'r'),
      'filename' => '/path/to/file',
      'headers' => [
        'Content-Type' => ''
      ]
    ],
    [
      'name' => 'output',
      'contents' => 'pdfrest_pdf'
    ]
  ]
];

$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);

$res = $client->sendAsync($request, $options)->wait();

echo $res->getBody();

Source of the provided code: GitHub

Breaking Down the Code

The provided PHP script uses the Guzzle HTTP client to make a multipart/form-data POST request to the pdfRest API. Here's a breakdown of the key components:

require 'vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Utils;

This section includes the necessary Guzzle library and imports the classes needed to create the HTTP client, the request, and handle file streams.

$headers = [
  'Api-Key' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
];

The headers array includes the API key for authentication with the pdfRest API.

$options = [
  'multipart' => [
    // ...multipart form data...
  ]
];

The options array contains the multipart form data to be sent with the request. Each entry in the 'multipart' array represents a different part of the form data:

  • file: This is the file to be converted. The 'contents' key should be a stream of the file, and the 'filename' key should be the path to the file.
  • output: This option specifies the desired output filename prefix.
$request = new Request('POST', 'https://api.pdfrest.com/pdf', $headers);
$res = $client->sendAsync($request, $options)->wait();
echo $res->getBody();

A new Request object is created with the POST method, the API endpoint, and the headers. The request is sent asynchronously, and the script waits for the response. The response body, which should contain the processed PDF content, is then outputted.

Beyond the Tutorial

This tutorial demonstrated how to make an API call to the pdfRest Convert to PDF API using PHP. The code sends a JPG file to the pdfRest API, which converts it to PDF. The resulting PDF is then returned in the response.

For further exploration, you can demo all of the pdfRest API Tools in the API Lab and refer to the API Reference Guide for more detailed information on the available options and tools.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at GitHub.

Generate a self-service API Key now!

Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.

Compare Plans
Contact Us