Created
January 27, 2019 02:23
-
-
Save Ravenna/1f2b1598d1ccb7b43481d71e1cc5d099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$fit = true; | |
$path = base_path().'/public'; | |
$file = $path .'/forms/doc-27.pdf'; | |
$docDeal = str_replace(' ', '-', strtolower($deal->name)); | |
$docInvestor = str_replace(' ', '-', strtolower($user->name)); | |
$date = date('m-d-Y'); | |
$signature = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $request->base64_signature)); | |
// Set Writer | |
$tempWriter = new SetaPDF_Core_Writer_TempFile(); | |
$tempWriter->setTempDir($path .'/forms/temp/'); | |
$tempWriter->setKeepFile(true); | |
// Create Doc | |
$document = SetaPDF_Core_Document::loadByFilename($file, $tempWriter); | |
// Set Fields | |
$formFiller = new SetaPDF_FormFiller($document); | |
$fields = $formFiller->getFields(); | |
$fields['Name']->setValue($user->name); // add some randomness | |
// Handle Signature | |
$image = SetaPDF_Core_Image::get(new SetaPDF_Core_Reader_String($signature))->toXObject($document); | |
$sigField = $fields['Investor Signature']->getAnnotation(); | |
$width = $sigField ->getWidth(); | |
$height = $sigField ->getHeight(); | |
$xobject = SetaPDF_Core_XObject_Form::create($document, array(0, 0, $width, $height)); | |
$canvas = $xobject->getCanvas(); | |
if ($fit == true) { | |
if ($image->getHeight($width) >= $height) { | |
$image->draw($canvas, $width / 2 - $image->getWidth($height) / 2, 0, null, $height); | |
} else { | |
$image->draw($canvas, 0, $height / 2 - $image->getHeight($width) / 2, $width); | |
} | |
} else { | |
$imageWidth = 100; | |
$image->draw($canvas, 0, $height/2 - $image->getHeight($imageWidth)/2, $imageWidth); | |
} | |
$sigField->setAppearance($xobject); | |
// | |
$document->save()->finish(); | |
// Upload PDF to s3 | |
$disk = Storage::disk('s3'); | |
$filePath = $disk->put('investment-docs/'. $docDeal .'-'.$docInvestor .'-'. $date .'.pdf',$tempWriter->getPath(), 'private'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment