fpdi_print
This module uses library FPDI Generate a PDF file for printing based on a template.
You can import pages from existing PDF documents.
How to use:
- install module fpdi_print with composer require drupal/fpdi_print. It'll add libraries tcpdf & fpdi to vendor
- Create view page node with contextual filter (nid)
- Add Pdf Print (global) to Header or Footer Views page
- Fill all the configuration
- Path to pdf source file - version 1.4. /!\ Check your version pdf before upload, it doesn't work with version 1.5 or 1.6 Path asbolute /var/www/drupal/sites/default/files/template.pdf or relative sites/default/files/template.pdf
- Add position x,y, text (format array yaml) /!\check validation yaml or /fpdi-print/validate before paste
If you leave the position configuration empty. It'll print contents of the view. You can set pdf header footer with global area custom text from views header footer
Example:
If your fpdf is Form filling and compatible (FPDM)
you can set configuration like this (you don't need position x,y)
- form: "address"
text: "My city"
- form: "address"
text: "My phone number"
You can goto page /fpdi-print/validate to check pdf and yaml format
for developper
There is hook fpdi_print_views for alter data positions before generate.
you can call service fpdi_print.print_builder
Format of positions : page number => [ x , y , (text|html|image), height , width]
the page number must start with 1 (NOT BEGIN WITH 0)
/*
$positions = [
'1' => [
[ 'x'=> 10, 'y'=>20, 'text'=>'Show text in page 1'],
[ 'x'=> 15, 'y'=>40, 'height'=>50 , 'width'=>50, 'image'=>101 //you can add fid for image],
[ 'x'=> 15, 'y'=>50, 'image'=>'/path/to/image' // add image path],
[ 'x'=> 15, 'y'=>60, 'image'=>'<img src="/path/to/image1.png"/> <img src="/path/to/image2.jpg"/>'// add html multi images],
],
'2' => [
[ 'x'=> 10, 'y'=>20, 'height'=> 10, 'html'=>'Show <b>html</b>\n in page 2' ],
]
'3' => [
[ 'pdf'=>'/path/to/merge/file.pdf' ],
]
]
*/
$pdf = \Drupal::service('fpdi_print.print_builder')->getPDF($positions, $filePdfTemplate);
//you can save pdf to file with $pdf->Output('I','yourFileName.pdf');
//or show in directly to browser
$response = new StreamedResponse();
return $response->setCallback(function () use ($pdf) {
ob_clean();
$handle = fopen('php://output', 'w+');
$pdf->Output();
fclose($handle);
});
Hook avaible before render pdf
/**
* Implements hook_fpdi_print_views_alter().
*/
function myModule_fpdi_print_views_alter($dataPositions, $view, $filePdfTemplate){
$view_id = $view->id();
...
}
Do you like this module? Show your appreciation by buying me ☕.