Skip to content

Instantly share code, notes, and snippets.

@justinswelch
Created July 31, 2018 01:06
Show Gist options
  • Save justinswelch/83679225f447114a6920ff0e935f9407 to your computer and use it in GitHub Desktop.
Save justinswelch/83679225f447114a6920ff0e935f9407 to your computer and use it in GitHub Desktop.
Class for Gravity Forms + GravityPDF to append external PDF after creating GPDF
<?php
namespace PXGPDF;
use GPDFAPI;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/*------------------------------------------------
* Class: PXB_Append_PDF
* Uses GPDFAPI (GravityPDF)
*
* Purpose: append an existing PDF (unchanged) to a PDF created by GravityPDF.
* - PDF to append is set with file upload field
------------------------------------------------*/
class PXB_Append_PDF {
public $pdf_path;
public $pdf_id;
public $pdf_new_orientation;
// css class of div that starts the appended pdf
// could add field, but this likely won't change
public $pdf_new_page_class = 'start-appended-pdf';
/* purposely empty constructor */
public function __construct() {}
/* Start it up */
public function init() {
add_filter( 'gfpdf_mpdf_class', [ $this, 'add_external_pdf' ], 11, 5 );
}
/**
* Add a page so we don't overwrite first PDF
* Output empty <div> that resets @page rules set in CSS
* This wasn't working when placed directly inside PDF template
*/
public function insert_new_pdf_template_div( $mpdf ) {
$new_page_div = sprintf( '<div class="%s"></div>', $this->pdf_new_page_class );
$mpdf->WriteHTML( $new_page_div );
}
/**
* Get file path to external PDF we'll be appending
*/
public function get_external_pdf_file( $entry, $settings ) {
if( ! ( $this->pdf_id = $settings[ 'pxb_external_pdf' ] ) || empty( $this->pdf_id ) )
{
return false;
}
$form_data = GPDFAPI::get_form_data( $entry[ 'id' ] );
$this->pdf_path = implode( $form_data[ 'field' ][ $this->pdf_id . '_path' ] );
if( ! file_exists( $this->pdf_path ) ) {
return false;
}
return $this->pdf_path;
}
/**
* Get orientation of PDF to append
* 'L' - landscape or 'P' - portrait
*/
public function set_pdf_new_page_orientation( $settings ) {
$this->pdf_new_orientation = $settings[ 'external_pdf_orientation' ];
}
/**
* Import external PDF after end of initial PDF
*
* Uses: Gravity PDF: gfpdf_mpdf_class()
* @link https://gravitypdf.com/documentation/v5/gfpdf_mpdf_class/
*
*/
function add_external_pdf( $mpdf, $form, $entry, $settings, $Helper_PDF ) {
// Get file path of external PDF to import
if( false === ( $pdf_path = $this->get_external_pdf_file( $entry, $settings ) ) ) {
return $mpdf;
}
// add div to PDF with class to target with CSS
$this->insert_new_pdf_template_div( $mpdf );
// set class variable for new page orientation
$this->set_pdf_new_page_orientation( $settings );
/**
* Add external PDF as new pages at end of document
* @link https://mpdf.github.io/reference/mpdf-functions/setimportuse.html
*/
$mpdf->SetImportUse();
// Set source file (mpdf) and pagecount, or return early
if( ! ( $pagecount = $mpdf->SetSourceFile( $pdf_path ) ) ) {
return $mpdf;
}
/**
* Loop through each page of external PDF,
* import and append pages one by one
* @link https://mpdf.github.io/reference/mpdf-functions/importpage.html
*/
for ( $i = 1; $i <= $pagecount; $i++ ) {
$import_page = $mpdf->ImportPage( $i );
$mpdf->UseTemplate( $import_page );
// skip new page on last round to avoid blank last page
if( $i < $pagecount ) {
$mpdf->AddPageByArray(
[
'orientation' => $this->pdf_new_orientation,
]
);
}
}
// return $mpdf to GravityPDF (which runs $mpdf->output() )
return $mpdf;
}
}
@bwedmore74
Copy link

This is exactly what I'm looking for to add uploaded PDF(s) to a custom Gravity PDF template. Which directory do I put this file into and how do I invoke it?

@mikemmcox
Copy link

This is exactly what I'm looking for to add uploaded PDF(s) to a custom Gravity PDF template. Which directory do I put this file into and how do I invoke it?

Any luck getting this to work? Bit of a newbie, but just need to know where to add this file, along with the external PDF and any other changes that are needed. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment