Sample code for 30+ languages & platforms
PHP

Extract PDF from JSON

See more JSON Examples

Demonstrates how to extract a PDF file contained within JSON. The file is represented as a base64 string within the JSON. Note: This example can extract any type of file, not just a PDF file.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\BinData;
use Chilkat2\JsonObject;
use Chilkat2\StringBuilder;

$json = new JsonObject();

// Load the JSON.
try {
    $json->LoadFile('qa_data/json/JSR5U.json');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// The JSON we loaded contains this:

//     {
//     ...
//     ...
//       "data": {
//         "content": "JVBERi0xLjQ..."
//       }
//     ...
//     ...
//     }

$sb = new StringBuilder();
$json->StringOfSb('data.content', $sb);

$bd = new BinData();
$bd->AppendEncodedSb($sb, 'base64');

$bd->WriteFile('qa_output/a0015.pdf');