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.
Upgrade your WordPress site with powerful, flexible forms.
Requirements
- Formidable PDFs add-on 2.0.2 or later. The add-on is available on the Business and Elite licenses.
From Idea to Reality in Minutes. Build Powerful Forms, Dashboards, Apps and More.
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:
- The add-on creates the Dompdf object, with the options from the frm_pdfs_dompdf_args filter.
- It loads the PDF's HTML content.
- It sets the paper size and orientation. These come from the shortcode's paper_size and orientation parameters, or letter and portrait by default.
- The frm_pdfs_before_render action runs.
- Dompdf renders the PDF.
- 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
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.
Related developer hooks
- Change the PDF after it's rendered, such as to add page numbers, with the frm_pdfs_after_render hook.
- Change the Dompdf options, such as remote images, with the frm_pdfs_dompdf_args hook.
- Add or remove content in the PDF with the frm_pdfs_export_content hook.
- Change the settings of a PDF attached to an email with the frm_pdfs_email_attachment_args hook.