Docs & Support

Learn about all the Formidable features and get support from our amazing customer success team.

frm_pdfs_before_render

Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or third-party development.

Use the frm_pdfs_before_render action to change a PDF from the Formidable PDFs add-on right before it's rendered. It passes the Dompdf object that builds the PDF. With it, you can change the paper size or draw on every page.

Requirements

  • Formidable PDFs add-on 2.0.2 or later. The add-on is available on the Business and Elite licenses.

Formidable Forms makes advanced site building simple. Launch forms, directories, dashboards, and custom WordPress apps faster than ever before.

How it works

Formidable PDFs builds each PDF with the Dompdf library. The action runs for every PDF the add-on makes, including PDF download links and PDFs attached to email notifications. A Send Email action attaches a PDF when its Attach PDF of entry to email toggle is on. Learn more about how to attach a PDF of an entry to an email.

Each PDF is built in this order:

  1. The add-on creates the Dompdf object, with the options from the frm_pdfs_dompdf_args filter.
  2. It loads the PDF's HTML content.
  3. It sets the paper size and orientation. These come from the shortcode's paper_size and orientation parameters, or letter and portrait by default.
  4. The frm_pdfs_before_render action runs.
  5. Dompdf renders the PDF.
  6. The frm_pdfs_after_render action runs, before the PDF is shown, downloaded, or attached.

A paper size set in this action replaces the one from the shortcode. The HTML is already loaded at this point. To change the content, use the frm_pdfs_export_content filter instead.

When a PDF shows page numbers with show_pagination=1, the add-on renders it twice to count the pages. The action runs in both renders. In the first one, $args doesn't include show_pagination.

Learn more about the PDF shortcode parameters.

Usage

add_action( 'frm_pdfs_before_render', 'my_function_name', 10, 2 );

Parameters

  • $dompdf (Dompdf\Dompdf): The Dompdf object that renders the PDF. Learn more in the Dompdf documentation.
  • $args (array): The PDF's settings.
    • For a PDF link, these are the shortcode parameters, such as id (the entry ID), view, orientation, paper_size, include_fields, exclude_fields, and show_pagination. A parameter that isn't in the shortcode may be missing. The mode is view or download.
    • For a PDF attached to an email, the mode is file. Other settings come from the frm_pdfs_email_attachment_args filter.

Examples

Learn where to put the code when adding a custom PHP function to your site.

Use A4 paper by default

This example makes A4 the default paper size for every PDF, including email attachments. A shortcode with its own paper_size parameter keeps that size, and the orientation parameter still works.

add_action( 'frm_pdfs_before_render', 'my_frm_pdfs_default_a4', 10, 2 );
function my_frm_pdfs_default_a4( $dompdf, $args ) {
	if ( empty( $args['paper_size'] ) ) {
		$orientation = empty( $args['orientation'] ) ? 'portrait' : $args['orientation'];
		$dompdf->setPaper( 'A4', $orientation );
	}
}

Replace A4 with another paper size that Dompdf accepts, such as legal.

Add a watermark to every page

This example prints a light gray CONFIDENTIAL watermark across every page of every PDF.

add_action( 'frm_pdfs_before_render', 'my_frm_pdfs_watermark' );
function my_frm_pdfs_watermark( $dompdf ) {
	$dompdf->setCallbacks(
		array(
			array(
				'event' => 'end_page_render',
				'f'     => function ( $frame, $canvas, $font_metrics ) {
					$font = $font_metrics->getFont( 'helvetica', 'bold' );
					$canvas->set_opacity( 0.2 );
					$canvas->text( 150, 215, 'CONFIDENTIAL', $font, 60, array( 0.5, 0.5, 0.5 ), 0, 0, 45 );
					$canvas->set_opacity( 1 );
				},
			),
		)
	);
}

The numbers 150 and 215 are where the text starts, in points from the top left corner of the page. The font size is 60, and the angle is 45 degrees. These fixed points place the watermark near the center of a letter or A4 portrait page, but off center on landscape and legal pages. The setCallbacks() method replaces any callbacks set earlier. Keep all of your page callbacks in one function.

Was this article helpful? *

This article may contain affiliate links. Once in a while, we earn commissions from those links. But we only recommend products we like, with or without commissions.

In this article

    We have a small, but amazing team of dedicated people who are committed to helping you achieve your goals and project requirements.


    Copyright © 2026 Strategy11, LLC. Formidable Forms® is a registered trademark Strategy11, LLC.

    Complete your purchase
    Don't forget your purchase!
    Complete Purchase
    Join 400,000+ using Formidable Forms to create form-focused solutions fast. Get Formidable Forms