Title: Generating a Second Custom PDF Template via Button
Last modified: November 9, 2025

---

# Generating a Second Custom PDF Template via Button

 *  [minamt](https://wordpress.org/support/users/minamt/)
 * (@minamt)
 * [5 months, 2 weeks ago](https://wordpress.org/support/topic/generating-a-second-custom-pdf-template-via-button/)
 * Hi,
   I have successfully created a custom template by copying it into my child
   theme and personalizing it — everything works great.
 * Now, I’d like to create a **second different PDF template** (like a voucher without
   price). Ideally, when I click a specific button (in the thankyou page after checkout),
   it would generate and download **that second PDF** instead of the default one.
 * Could you please guide me on the best way to:
    1. Add and register an additional custom PDF template; and
    2. Trigger it with a separate button or link so I can generate that second document
       independently?
 * Any example code snippets or documentation references would be really appreciated.
 * Thanks a lot for your help and for the great plugin!
 * Best regards,

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

 *  Plugin Contributor [Darren Peyou](https://wordpress.org/support/users/dpeyou/)
 * (@dpeyou)
 * [5 months, 1 week ago](https://wordpress.org/support/topic/generating-a-second-custom-pdf-template-via-button/#post-18714514)
 * Hi [@minamt](https://wordpress.org/support/users/minamt/),
 * I think the way to go is to dynamically change the path of the custom template
   based on certain conditions.
 * This filter should help: `<code>wpo_wcpdf_template_file</code>`. Its arguments
   are:
   – A string for the file path.– A string for the document type.– A WooCommerce
   order object.
 * Reference: [PDF Filter Hooks – WP Overnight documentation](https://docs.wpovernight.com/woocommerce-pdf-invoices-packing-slips/pdf-template-filter-hooks/)
   
   The link contains a list of all the filter hooks we use in the free plugin. There
   is no built-in functionality for adding a button.
 *  Thread Starter [minamt](https://wordpress.org/support/users/minamt/)
 * (@minamt)
 * [5 months, 1 week ago](https://wordpress.org/support/topic/generating-a-second-custom-pdf-template-via-button/#post-18714819)
 * Hi,
   Thank you for your reply, I’ve already used the `wpo_wcpdf_template_file`
   filter but it shows me “Invalid order” instead of the pdf template even though
   the order number is correct:
 *     ```wp-block-code
       add_action('woocommerce_thankyou', 'add_voucher_pdf_button', 20);function add_voucher_pdf_button($order_id) {    if (!$order_id) return;        $order = wc_get_order($order_id);    if (!$order) return;    // Nonce for security, must match the document type (invoice)    $nonce = wp_create_nonce('generate_wpo_wcpdf');        // Build the correct AJAX URL    $pdf_url = add_query_arg(array(        'action'       => 'generate_wpo_wcpdf',        'document'     => 'invoice', // keep as invoice, since voucher isn’t a registered type        'order_ids'    => $order->get_id(),        'voucher_pdf'  => 1,        '_wpnonce'     => $nonce,    ), admin_url('admin-ajax.php'));        echo '<a href="' . esc_url($pdf_url) . '" target="_blank" class="button alt" style="margin-top:20px;">Download Voucher PDF</a>';}add_filter('wpo_wcpdf_template_file', 'load_voucher_template_conditionally', 10, 3);function load_voucher_template_conditionally($template, $type, $order) {    if (!empty($_GET['voucher_pdf']) && $_GET['voucher_pdf'] == 1) {        $voucher_template = get_stylesheet_directory() . '/woocommerce/pdf/voucher-template/' . basename($template);                if (file_exists($voucher_template)) {            return $voucher_template;        }    }    return $template;}
       ```
   
 * Am I using the filter wrong?

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

You must be [logged in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fgenerating-a-second-custom-pdf-template-via-button%2F%3Foutput_format%3Dmd&locale=en_US)
to reply to this topic.

 * ![](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/)

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)

 * 2 replies
 * 2 participants
 * Last reply from: [minamt](https://wordpress.org/support/users/minamt/)
 * Last activity: [5 months, 1 week ago](https://wordpress.org/support/topic/generating-a-second-custom-pdf-template-via-button/#post-18714819)
 * Status: not resolved