Skip to content

Instantly share code, notes, and snippets.

@briehanlombaard
Last active August 29, 2015 14:01
Show Gist options
  • Select an option

  • Save briehanlombaard/27cd4a67beb3cc4a397b to your computer and use it in GitHub Desktop.

Select an option

Save briehanlombaard/27cd4a67beb3cc4a397b to your computer and use it in GitHub Desktop.
<?php
function pre_render_pdf($mpdf, $form_id, $lead_id, $arguments, $output, $filename) {
// XXX: We have multiple notifications setup which means this function is
// called multiple times and we end up appending the document many times. This
// will help us get around that by ensuring it's only called once.
static $has_been_called = false;
if($has_been_called) return $mpdf;
// XXX: We're only interested in the form with ID=1
if ($form_id != 1) return $mpdf;
$form = RGFormsModel::get_form_meta($form_id);
$fileupload_fields = GFCommon::get_fields_by_type($form, array('fileupload'));
if (!is_array($fileupload_fields)) return $mpdf;
$entry = GFFormsModel::get_lead($lead_id);
$upload_root = RGFormsModel::get_upload_root();
// XXX: We're assuming that there's only one file upload field.
$field = $fileupload_fields[0];
$url = $entry[$field['id']];
$file = preg_replace('|^(.*?)/gravity_forms/|', $upload_root, $url);
$mpdf->SetImportUse();
$pagecount = $mpdf->SetSourceFile($file);
for ($i=1; $i<=$pagecount; $i++) {
if ($i < $pagecount)
$mpdf->AddPage();
$import_page = $mpdf->ImportPage($i);
$mpdf->UseTemplate($import_page);
}
$has_been_called = true;
return $mpdf;
}
add_filter('gfpdfe_pre_render_pdf', 'pre_render_pdf', 10, 6);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment