I am trying to create a PHP form that outputs a PDF (using mPDF) of all the values in the form after hitting submit. I have included jSignature which takes in an e-signature drawn by the user. The signature is then converted to an image and outputs the data URI string in a text area.
<div id="signature"></div><br/>
<input type='button' id='click' value='click'>
<textarea id='output' name="signature"></textarea><br/>
<!-- Preview image -->
<img src='' id='sign_prev' style='display: none;' />
<!-- Script -->
<script>
$(document).ready(function() {
// Initialize jSignature
var $sigdiv = $("#signature").jSignature({'UndoButton':true});
$('#click').click(function(){
// Get response of type image
var data = $sigdiv.jSignature('getData', 'image');
// Storing in textarea
$('#output').val(data);
// Alter image source
$('#sign_prev').attr('src',"data:"+data);
$('#sign_prev').show();
});
});
</script>
<button type="submit" class="btn btn-success btn-lg">Submit</button>
I have another file that uses the name attribute from the input areas to display the values from each input field to show on the PDF. Since the signature comes in as a data URI string I have to convert it back into a png image before creating the PDF. What I am sure of at the moment is that I am able to retrieve the data URI string and I am able to replace the "image/png;base64" string that comes with it so all I am left with is the data itself. But I am struggling to decode the URI back to an image and I am not sure about how I should display it back as an image.
<?php
require_once __DIR__ . '/vendor/autoload.php';
//grab variables
$inspection = $_POST['inspection'];
$ccOfficer = $_POST['cco-name'];
$inspector = $_POST['inspector'];
$tower = $_POST['tower'];
$strata = $_POST['strata'];
$suite = $_POST['suite'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$signature = $_POST['signature'];
//Create new PDf instance
$mpdf = new \Mpdf\Mpdf();
$mpdf->debug = true;
$signature = str_replace('image/png;base64,','', $signature);
$signatureDecode = base64_decode($signature);
$im = imagecreatefromstring($signatureDecode);
if(!$im) {
die('Base64 value is not a valid image');
};
// $img_file = '/wti-report-form/images/signature.png';
// imagepng($im, $img_file, 0);
//$file = fopen($output_file, "wb");
//fwrite($file, $signatureDecode);
//fclose($file);
//Create our PDF
$data = '';
$data .= '<h1>ARC - NEW HOME WALK-THROUGH CHECKLIST</h1>';
//add data
$data .= '<strong>Inspection Date</strong> ' . $inspection . '<br/>';
$data .= '<strong>Customer Care Officer</strong> ' . $ccOfficer . '<br/>';
$data .= '<strong>This Suite was inspected by</strong> ' . $inspector . '<br/>';
$data .= '<strong>Tower #</strong> ' . $tower . '<br/>';
$data .= '<strong>Strata Lot #</strong> ' . $strata . '<br/>';
$data .= '<strong>Suite #</strong> ' . $suite . '<br/>';
$data .= '<strong>Email</strong> ' . $phone . '<br/>';
$data .= '<strong>Phone</strong> ' . $email . '<br/>';
//$data .= "<img src='" . $file . "'/>";
//$data .= '<p>' . $signature . '</p>';
//Write PDF
$mpdf->WriteHTML($data);
//output to the browser
$mpdf->Output('myfile.pdf', 'I');
?>
Any help will be appreciated. Thanks! :)