Skip to content

Instantly share code, notes, and snippets.

@BFTrick
Last active Aug 25, 2020
Embed
What would you like to do?
A more customized Ninja Forms PDF Form Submissions template
<?php
/**
* PDF Included With Form Submission
*
* @author Patrick Rauland
* @version 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
?>
<html>
<head>
<link type="text/css" href="pdf.css" rel="stylesheet" />
<link type="text/css" href="../../../themes/your-theme/pdf.css" />
</head>
<body>
<header>
<img src="../../../themes/your-theme/logo.png"/>
</header>
<div id="main">
<p>Hi <?php echo $fields[6]['value'];?> <?php echo $fields[23]['value'];?>!</p>
<p>Thanks for filling out our form. We'll respond to you shortly.</p>
</div>
</body>
</html>
@federod
Copy link

federod commented Jul 24, 2017

I can't get the Values to display anyone else having this issue

@ftank47
Copy link

ftank47 commented Aug 1, 2017

I had the exact same issue. The reason your values are not showing up is that the array keys do not necessarily start at 0 and progress. I did a test on the $fields array variable and output the loop with this code to see the keys in my PDF:

/* Test loop to find the key values */
foreach ($fields as $key => $value) {
    echo "Key: $key; Value: $value<br />\n";
}

I could then see my keys output like this:

Key: 16; Value: Array
Key: 17; Value: Array
Key: 18; Value: Array
Key: 19; Value: Array
Key: 20; Value: Array
Key: 21; Value: Array
Key: 22; Value: Array
Key: 23; Value: Array
Key: 24; Value: Array
Key: 25; Value: Array
Key: 26; Value: Array
Key: 27; Value: Array
Key: 28; Value: Array
Key: 30; Value: Array
Key: 31; Value: Array
Key: 32; Value: Array
Key: 29; Value: Array
Key: 33; Value: Array

I then updated echo $fields[6]['value']; to match whatever key I needed, for example $fields[33]['value']; and it worked fine after that.

@MrEricMW
Copy link

MrEricMW commented Dec 5, 2017

For some reason, using the $fields[6]['value'] way doesn't seem to work for me, even after getting the correct key as recommended. If I enable PHP errors, I get this error message: "Fatal error: Cannot use object of type NF_Database_Models_Field as array". Any ideas? This is NinjaForms 3.2.4 with PDF Form Submissions 3.0.4.

@ketoverlock
Copy link

ketoverlock commented Feb 14, 2018

I couldn't get it to work using a numerical field key, no matter how hard I tried. So I grabbed the field key for each field individually from the Dashboard and plugged them into the code, and it worked fine.

So instead of $fields['6']['value'] it was more like $fields['name_1498535656699']['value'].

To get the plugin-generated field key, you go into your Dashboard > Ninja Forms > Edit Form > Field > Administration and grab the field key:
screen shot 2018-02-14 at 11 36 52 am

@ThorJohann
Copy link

ThorJohann commented May 24, 2018

ketoverlock

Did that work for you?

I've been working on this for a few hours and I have the opposite problem.

I can only use numeric field key but if I change the field key in the form builder then that field disappears from the form on the website side (if you understand).

If I copy the default field key then nothing happens.

I'M GOING CRAZY :)

@davidallenlewis
Copy link

davidallenlewis commented Jan 30, 2019

Same issue here. I tried to reindex the $fields array and that didn't work. After 2 hours of bashing my head against the wall I said the hell with it and simply pushed everything into a brand new array using a foreach loop. Works.

$original_fields = $fields; $fields = array(); foreach( $original_fields as $field ) { array_push($fields, $field); }

@mwbarican
Copy link

mwbarican commented Feb 11, 2019

@uniondesign

May I know where you added that code?

i'm getting an error like this from the pdf.php file I placed on my child theme

Uncaught TypeError: Cannot read property 'fields' of undefined at n.submitErrors (front-end.js?ver=3.4.3:1) at _ (backbone.min.js?ver=1.2.3:1) at m (backbone.min.js?ver=1.2.3:1) at f (backbone.min.js?ver=1.2.3:1) at u.Channel.u.trigger (backbone.min.js?ver=1.2.3:1) at Object.error (front-end.js?ver=3.4.3:3) at i (jquery.js?ver=1.12.4:2) at Object.fireWith [as rejectWith] (jquery.js?ver=1.12.4:2) at y (jquery.js?ver=1.12.4:4) at XMLHttpRequest.c (jquery.js?ver=1.12.4:4)

@Accularian
Copy link

Accularian commented Apr 29, 2019

Patrick,

Thanks for your plugin.

Allow me to contribute. Here's what I found. I'll give you the executive summary first and then ramble on with the details.

  1. Using the Admin Field Key worked for me.
  2. If your form fails, one of two things happen: Blank Page, Page Loads in Browser but does not download.
  3. PHP code must be bang-on correct or an error will occur with the above behaviors.
  4. Field names must absolutely match what you have on your form or you will have errors or fields missing from your form.
  5. Don't use dashes in field names, use underlines.

Ok, the longer version.

Hours and hours later....

I wanted to get my form into a

. The system apparently does not like the element.
If I use , export does not work, if I remove it, I am fine.

I had a field named "qty_t-shirts" and it was pulling data from my first field... why? Who knows. Renamed to "qty_t_shirts" and all was fine.

My system seems to be really picky, even about my html and extra spaces after php code.

After CAREFULLY reviewing my entire layout in the pdf.php file and making sure I had no misspellings. My form was still failing. Next, I went through the form itself and looked at each Admin label. I found three misspellings in the form! Once I fixed them in the form, and made absolutely sure everything matched in pdf.php, it worked fine.

Here was my testing workflow. I filled out a form on my website then viewed it under "View Submissions." Once I was on that page, I could just stay there and click "Export to PDF" as I wanted to review my edits to php.pdf. The results were interesting. If you have an error in your php.pdf file, the page seems to do one of two things. It may load in your browser window up to the point of the error. This might help you track problems down to the line right after the printout fails.

Or, the form may not load at all... just a blank page.

Or, it may trick you and present the page but skip the download. Watch for this carefully.

If you have an error in you PHP code, you will probably just get a blank page.

If you have an error in a variable, it may skip that variable and still present your form on the screen. Unless you have a form where every field is populated with some test data, you may never know if a field is missing or not. In my example, if I had 0 t-shirts, the field would display but would be unpopulated. I needed to make sure that my test form had a number above 0 in that field.

In the perfect scenario, the PDF should download to your system AND be displayed on your screen. If either of these fail, something is wrong. I got faked out a number of times because things looked beautiful on the screen but I did not notice that the form did not download too.

One really weird issue occurred when I used a "dash" in a field name. As I mentioned above, I had a field named "qty_t-shirts". It was weirdly grabbing data from some other field... it appears that it was data from the first field on the form. After struggling with this for quite a while, and even changing the field name a couple of times... while still using a " - " in the word "t-shirt" (because that's how you spell t-shirt)... the error still occurred. Finally I thought that the " - " might be the problem. I changed the field name to qty_t_shirts and it worked perfectly. Don't use dashes, use underscores.

I now have a fully functioning, beautiful PDF receipt that I can easily modify with HTML and CSS. The client loves it and I am thrilled to have achieved this because I am a hack at all this.

Anyway... I sure hope this all helps someone else.

As Yoda said, "Failure... the best teacher it is." Frankly... I prefer to learn from someone else's failures, so I put mine out here for the world to see!

Dana Haynes
machelpglobal.com

@davidallenlewis
Copy link

davidallenlewis commented Oct 16, 2019

@uniondesign

May I know where you added that code?

I put it at the top of my pdf.php template file

@davidallenlewis
Copy link

davidallenlewis commented Oct 16, 2019

Does anyone know how to get the form submission ID in a custom PDF template? You can use the filter:

add_filter( 'ninja_forms_submission_pdf_fetch_sequential_number', '__return_true' );

And that will add it to the DEFAULT template which just appends it to the start of the $table string that the default template uses, but there doesn't seem to be any way to get it into the custom template. I can use get it AFTER the form has been submitted and saved to the database and then you go to Ninja Forms > Submissions and export the PDF... but not on the initial submission for the PDF that gets emailed.

@suzannezc
Copy link

suzannezc commented Oct 17, 2019

Is there a trick for identifying the form number in a multi-site situation?

Thank you to the previous commenters for the other tips. :)

@suzannezc
Copy link

suzannezc commented Oct 24, 2019

The trick that appears to be working for me is using site_url() to determine the site url and then matching it on the site url and the form number. if site_url() is my site, and form is my form_ID, then...

@leyannots
Copy link

leyannots commented Feb 4, 2020

Buenas tardes! Hola a todos. I need help! I have 2 forms, but only one pdf.php, i need 2 forms, and, 2 pdf templates, understand me? Thanks!

@baas411
Copy link

baas411 commented Jul 2, 2020

Buenas tardes! Hola a todos. I need help! I have 2 forms, but only one pdf.php, i need 2 forms, and, 2 pdf templates, understand me? Thanks!

Use if else conditions to display the correct data for each form.

<?php if ( $form_ID == 1 ){ echo $fields[field_key_value_form1]['value']; } elseif ( $form_ID == 2 ) { echo $fields[field_key_value_form2]['value']; } else { echo $table; } ?>

@kapil247
Copy link

kapil247 commented Aug 11, 2020

@baas411 i have the same issue as leyannots of multiple forms and want to have customized pdf for each one. please explain a little more in detail as i m not well versed in php. thanks

@baas411
Copy link

baas411 commented Aug 14, 2020

The if/else renders content for the pdf based on the id of the form. So if the form has id 1 then echo field_key_value_form1.
( $form_ID == 1 ) is the id of the ninja form.
field_key_value_form1 is the value from the form field (find it in ninja forms)
etc

@dustylavender
Copy link

dustylavender commented Aug 25, 2020

Does anybody know all of the available values that are possible or how they relate to the merge tags?

The "label" tag in this shows me label for the radio field, but with the merge tag, {field:q1:label} shows the selected radio label under options. Is it possible to grab that data on this?

Example
I have a radio select with 4 options; Fully Met, Partially Met, Not Met and N/A. Each of those options have different values. The code shows the radio value, not the radio label.

I'd like to pull that Label to the PDF vs the radio value.
Screen Shot 2020-08-25 at 5 10 40 PM

Bonus points question:
Has anybody successfully pulled a calculation onto the PDF form? My PDF needs to show a calculation value from the form...it is basically a quiz that our users take and I need to report the score. The merge tag for that is {calc:scoreval}.

Many thanks in advance!

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