Create Dynamic Fillable PDFs

I have an Excel Workbook that I want to save as a PDF. This I will do manually outside of Yii2.

From there, in my Yii2 app, how can I overlay editable fields in the PDF, add default values to some of the fields from a model and save it as a specific pdf file to then output to the user?

1 Like

It’s not free, but I’ve used SetAsign in the past for this sort of thing: https://www.setasign.com. Small shop, very easy to work with. Lots of tools for working with PDFs via PHP. Be forewarned, however … working with PDFs is a pain in the neck.

1 Like

… here’s some sample code from a project a while ago where I needed to fill out a form and then put a stamp on it. Not exactly what you are talking about, but an example of taking a form with some pre-defined fields, adding values to those fields, adding a couple of time stamps and then saving the whole thing as a PDF. (This is a just a piece of the method in the model.)

$document = \SetaPDF_Core_Document::loadByFilename(Yii::getAlias('@app/resources/'.$this->name));
$formFiller = new \SetaPDF_FormFiller($document);
$fields = $formFiller->getFields();
$fieldNames = $fields->getNames();
Yii::info('form fields '.print_r($fieldNames,true),__METHOD__);
$formFiller->setRemoveXfaInformation(true);

$maps = json_decode($this->map);
foreach ($maps->accountInfo as $k=>$v) if ($v and isset($this->accountInfo->$v)) $fields[$k]->setValue($this->accountInfo->$v);
foreach ($maps->form as $k=>$v) if ($v and isset($this->form->$v))  $fields[$k]->setValue($this->fisc->$v);
$writer = new \SetaPDF_Core_Writer_Http($date->format('Y-m-d H-i-s ').$this->accountInfo->id.' form.pdf',true);
$document->setWriter($writer);
$stamper = new \SetaPDF_Stamper($document);
$pages = $document->getCatalog()->getPages();

if ($sign) {
	foreach ($fieldNames as $name) {
		if (!in_array($name,['SignaturePlaceholder','euShortDate'])) $fields->get($name)->flatten();
	}
	$fields->get('SignaturePlaceholder')->setValue('{Signature}');
	for ($i=5;$i>2;$i--) $pages->deletePage($i);
}
else {
	$intro = $pages->create('letter','portrait',false);
	$pages->prepend($intro);
	$outro = $pages->create('letter','portrait',false);
	$pages->append($outro);

	$stamp = new \SetaPDF_Stamper_Stamp_Pdf(
		'pdfs/intro.pdf', 1, \SetaPDF_Core_PageBoundaries::CROP_BOX
	);
	$stamper->addStamp(
		$stamp,
		\SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
		1
	);
	$stamp = new \SetaPDF_Stamper_Stamp_Pdf(
		'pdfs/outro.pdf', 1, \SetaPDF_Core_PageBoundaries::CROP_BOX
	);
	$stamper->addStamp(
		$stamp,
		\SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
		'last'
	);
}

$font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);;
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 8);
$stamp->setTextColor([0, 0, 0]);
$stamp->setText(trim((string)$this->accountInfo->id).' F52 '.$date->format('Ymd'));
$stamper->addStamp(
	$stamp,
	\SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
	2,
	5,
	30,
	0
);

$font = new \SetaPDF_Core_Font_TrueType_Subset($document,'fonts/Code39r.ttf');
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 24);
$stamp->setTextColor([0, 0, 0]);
$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_CENTER);
$stamp->setText(trim((string)$this->accountInfo->LOOKUP_ID).' F52 '.$date->format('Ymd'));
$stamper->addStamp(
	$stamp,
	\SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
	2,
	5,
	5
);

$font = \SetaPDF_Core_Font_Standard_Helvetica::create($document);
if (!$sign) {
	$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 12);
	$stamp->setTextColor([1, 0, 0]);
	$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_CENTER);
	$stamp->setText('Please Return This Page');
	$stamper->addStamp(
		$stamp,
		\SetaPDF_Stamper::POSITION_RIGHT_BOTTOM,
		[2,3],
		0,
		100,
		-90
	);

}
$font = \SetaPDF_Core_Font_Standard_HelveticaBold::create($document);
$stamp = new \SetaPDF_Stamper_Stamp_Text($font, 10);
$stamp->setTextColor([0, 0, 0]);
$stamp->setAlign(\SetaPDF_Core_Text::ALIGN_LEFT);
$stamp->setText('Secure Upload: ');
$stamper->addStamp(
	$stamp,
	\SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
	5,
	30,
	40,
);

$phone = new \SetaPDF_Stamper_Stamp_Text($font, 10);
$phone->setTextColor([0, 0, 0]);
$phone->setAlign(\SetaPDF_Core_Text::ALIGN_LEFT);
$phone->setText(preg_replace('/(\d{3})(\d{3})(\d{4})/','($1) $2-$3',$this->accountInfo->office->telephone));
$stamper->addStamp(
	$phone,
	\SetaPDF_Stamper::POSITION_LEFT_BOTTOM,
	5,
	340,
	110,
);
// stamp the document
$stamper->stamp();

if ($sign) return $document;

$writer = new \SetaPDF_Core_Writer_Http($date->format('Y-m-d H-i-s ').$this->accountInfo->id.' form.pdf',true);
$document->setWriter($writer);
$document->save()->finish();
1 Like

The PDF specification allows HTML forms to be used. I am not sure if this is what you mean with your question. mPDF is an active PDF library in the PHP world.

I have an existing PDF.

I’m looking for a way to add form fields in it at specific locations (overlayed on top of the existing pdf content), then fill some of those fields in with values from a model and output it as a new PDF. The goal is to produce a PDF where the fields can be edited, but some already have information coming from my system/db.

Something like this? Form filling

1 Like

is there an fpdf yii2 extension? I have been unable to locate 1.

You don’t need one, just require fpdf in composer and then code away :slight_smile:

1 Like

I’ll give it a try. Thank you!

1 Like