Title: Add Custom Javascript to invoice template
Last modified: August 12, 2020

---

# Add Custom Javascript to invoice template

 *  Resolved [outmaster](https://wordpress.org/support/users/outmaster/)
 * (@outmaster)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/add-custom-javascript-to-invoice-template/)
 * Hello!
 * I am using Javascript to convert numbers to characters in the invoice template.
   It can Preview in HTML well, but when downloading the PDF, the converted characters
   cannot be displayed.
 * Or do I have to put Javascript in any file or folder?
 * Thanks for the advice

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Plugin Contributor [Ewout](https://wordpress.org/support/users/pomegranate/)
 * (@pomegranate)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/add-custom-javascript-to-invoice-template/#post-13241344)
 * Unfortunately you can’t use JavaScript in the PHP template, so you’ll need to
   find a PHP alternative to your method. What are you trying to achieve exactly?
   If you just need the total amount in words, here’s an example filter:
 *     ```
       add_action ( 'wpo_wcpdf_after_order_details', 'wpo_wcpdf_spell_out_total', 10, 2);
       function wpo_wcpdf_spell_out_total ($template_type, $order) {
       	if ( in_array( $template_type, array( 'invoice', 'credit-note' ) ) ) {
       		$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
       		$spelled_out_total = $f->format($order->get_total());
       		echo "<p><strong>Amount in words:</strong><br>" . $spelled_out_total . "</p>";
       	}
       }
       ```
   
 * If you haven’t worked with code snippets (actions/filters) or functions.php before,
   read this guide: [How to use filters](https://docs.wpovernight.com/general/how-to-use-filters/)
 *  Thread Starter [outmaster](https://wordpress.org/support/users/outmaster/)
 * (@outmaster)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/add-custom-javascript-to-invoice-template/#post-13241703)
 * Thanks for the advice
 * I put your example in the theme’s functions.php, It works
 * But now I have created HTML fields for signing at the end of Invoice.
 * That makes the total amount in words not in the desired position.
 * If I am converting from your sample filter to PHP, put it in the invoice template,
   what do I do?
 *  Thread Starter [outmaster](https://wordpress.org/support/users/outmaster/)
 * (@outmaster)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/add-custom-javascript-to-invoice-template/#post-13241845)
 * I tried this, now it works as I want.
 *     ```
       <td>
       <?php do_action( 'custom_amount_in_words', $this->type, $this->order ); ?>
       </td>
       ```
   
 * and
 *     ```
       add_action ( 'custom_amount_in_words', 'wpo_wcpdf_spell_out_total', 10, 2);
       function wpo_wcpdf_spell_out_total ($template_type, $order) {
       	if ( in_array( $template_type, array( 'invoice', 'credit-note' ) ) ) {
       		$f = new NumberFormatter("en", NumberFormatter::SPELLOUT);
       		$spelled_out_total = $f->format($order->get_total());
       		echo "<p><strong>Amount in words:</strong><br>" . $spelled_out_total . "</p>";
       	}
       }
       ```
   
 * Thank you so much
 *  Plugin Contributor [Ewout](https://wordpress.org/support/users/pomegranate/)
 * (@pomegranate)
 * [5 years, 11 months ago](https://wordpress.org/support/topic/add-custom-javascript-to-invoice-template/#post-13241978)
 * Very glad to hear that! FWIW you could also call the function directly or even
   integrate the code in the function directly in the template. Whatever works for
   you 🙂
 * If you can spare a minute, we always appreciate a plugin review [here on wordpress.org](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/#new-post).
 * Thanks in advance and have a fantastic day!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Add Custom Javascript to invoice template’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce-pdf-invoices-packing-slips/assets/icon-256x256.
   png?rev=2189942)
 * [PDF Invoices & Packing Slips for WooCommerce](https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce-pdf-invoices-packing-slips/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce-pdf-invoices-packing-slips/reviews/)

 * 4 replies
 * 2 participants
 * Last reply from: [Ewout](https://wordpress.org/support/users/pomegranate/)
 * Last activity: [5 years, 11 months ago](https://wordpress.org/support/topic/add-custom-javascript-to-invoice-template/#post-13241978)
 * Status: resolved