LMS & eLearning

How the Certificate Builder Renders Blocks to PDF With mPDF

A developer walk-through of the LearnDash Certificate Builder pipeline: how it intercepts PDF generation, configures mPDF, turns blocks into HTML and CSS, handles fonts and backgrounds, and serves previews.

How the Certificate Builder Renders Blocks to PDF With mPDF

When a learner clicks a certificate link on a site with the Certificate Builder add-on (1.1.2), the PDF they receive is not made by LearnDash's TCPDF code at all. The add-on intercepts the request, parses the certificate's blocks, converts each one to a fragment of HTML and CSS, and hands the result to a bundled copy of mPDF. Understanding that pipeline explains most of the builder's behaviour: why some blocks are allowed and others not, why fonts must be TTF, why a background image is mandatory, and where the hooks sit. This article follows the code path from click to download.

Step 1: taking over from TCPDF

LearnDash core generates certificates in ld-convert-post-pdf.php, and early in that process it fires the action learndash_tcpdf_init with the certificate arguments: certificate ID, the awarded course or quiz ID, the user ID, language and file name options. On a request for a certificate post, the add-on's controller parses the post content on the wp action and, if the first block is learndash/ld-certificate-builder, attaches its generate_pdf() method to that action. Classic certificates have no such block, so TCPDF proceeds normally for them; builder certificates never reach TCPDF's rendering.

generate_pdf() validates the three IDs, stopping with a message if the certificate, the awarded post or the user does not exist, then parses the blocks and calls the PDF service's serve() with the certificate and course IDs. The same service is used for previews, described later.

Step 2: configuring mPDF

The service's init() creates an mPDF instance with a temporary directory inside uploads, zero margins on all sides, a default font of freeserif, and font data for three families bundled with the add-on: FreeSerif, FreeSans and FreeMono, each with regular, bold, italic and bold-italic files, with OpenType layout enabled for the first two. FreeSans is registered as the substitution font used when a glyph is missing elsewhere. It then reads the option learndash_certificate_builder_fonts, where the font manager stores uploaded TTF files, registers each as an additional family, and adds the upload folder as a font directory. This is why the font uploader accepts TrueType only: mPDF's font loader works from TTF files it can parse.

Before any content is written, serve() sets UTF-8 multibyte encoding, disables core-font-only mode, and passes the instance through the filter learndash_certificate_builder_mpdf, the point where you can change protection, metadata, watermarks or any other mPDF setting.

Step 3: reading the builder block

The outer block's attributes describe the page. The content class reads the background image either from an attachment ID or from a URL attribute, and dies with "A background image is required" if neither resolves. It reads the page size and orientation, the wide L orientation unless portrait is specified, the page width and height, a container width expressed as a percentage of the page that defaults to 70, a spacing value and the default font. It applies the page size to mPDF, sets the body background to the image with no repeat and image-resize mode 6 so the image fills the page, and sets a #wrap element to the container width. Every inner block, recursively, is given a unique generated id so that CSS rules can target it.

Colours come next. The theme's editor colour palette, or the add-on's defaults when the theme does not declare one, becomes a set of CSS classes of the form .has-{slug}-background-color plus gradient classes, mirroring what the block editor emits, so block colour choices survive the conversion.

Step 4: one renderer per block

build_html_structure() walks the blocks and instantiates a renderer class for each type, named after the block: Core_Heading, Core_Paragraph, Core_Columns, Core_Spacer, and the four LearnDash data blocks for course, group, quiz and user information. Anything else is handled by a fallback renderer. Each renderer receives the block, a shared style builder and a shared HTML builder, and its run() method adds HTML and CSS rules keyed to the block id. The base class first injects the generated id and a cb-block class into the block's own markup using a bundled HTML parser.

The details matter for layout. Headings and paragraphs run do_shortcode() on their content, replace spaces with non-breaking spaces so that mPDF does not reflow text differently from the editor, map the editor's named font sizes to fixed pixel values from 16px for extra small up to 122px for gigantic, apply alignment, hex colours and the chosen font family, and default the line height to 1.4. Columns compute each column's width in pixels from the page width, the container percentage and a fixed gutter, float the columns left, and recurse into their inner blocks with a nested HTML builder, which is how multi-column designs survive in a renderer that has no CSS grid. Spacers pass their inline height through unchanged. The allowed block list is filterable through learndash_certificate_builder_allowed_blocks, and our guide to the supported blocks explains what each renderer can and cannot do.

Step 5: writing and output

serve() writes three things to mPDF in order: the add-on's bundled style.css, which reproduces the block editor's base styles, as header CSS; the generated rules from the style builder, also as header CSS; and the assembled HTML wrapped in <div id="wrap">. It then computes a file name from the user login, course title, certificate title and site name, sanitised and ending in .pdf, passes it through learndash_certificate_builder_pdf_name, passes the output destination, inline by default, through learndash_certificate_builder_pdf_output_mode, and calls mPDF's Output(). Snippets for those three filters are in Certificate Builder hooks: output mode, file name and mPDF instance.

Previews

Preview uses the same pipeline with no course context. On template_redirect, when the request is a preview of a single certificate and the current user can edit it, the controller parses the blocks and calls serve() with certificate and course IDs of zero. The file name becomes preview.pdf, and because there is no course or user, the LearnDash data blocks and shortcodes render empty. That is expected; to see real values, complete a test course and open the certificate from the course page. The Preview link in the certificates list replaces View for builder certificates, since there is no HTML page to view.

What this means in practice

  • Layout problems are CSS problems in mPDF's subset of CSS, not WordPress problems. Floats and fixed widths work; flexbox and grid do not, which is why columns are computed in PHP.
  • Missing characters are font problems. mPDF substitutes from FreeSans when a glyph is absent, and shows boxes only when no registered font has it.
  • Performance is per request. Each download parses blocks and renders a PDF; there is no cache. Heavy background images slow every download, so keep them optimised.
  • Anything mPDF can do is reachable through the instance filter, from password protection to attaching metadata.

When to get help

Reading the pipeline is enough for most customisations. Rendering a block type the add-on does not support, generating certificates in bulk for a cohort, or storing PDFs for later retrieval are development tasks that build on the classes above; they are regular work for our LearnDash services team, and a LearnDash developer familiar with mPDF can add a custom renderer in a day. The classic engine's hooks are covered separately in LearnDash certificate hooks.

Frequently asked questions

Does the Certificate Builder still use TCPDF?

No. It hooks learndash_tcpdf_init to take over before TCPDF renders anything and produces the PDF with its bundled mPDF instead. Classic certificates continue to use TCPDF.

Why do my course shortcodes show nothing in the preview?

Previews are served with no course or user context, so data blocks and shortcodes have nothing to print. Test with a completed course to see real values.

Why does the PDF say a background image is required?

The builder block has no image set, or the attachment was deleted. Select a background image in the block's settings; the page size is derived from it.

Can I add support for another block?

Yes. Add the block to the allowed list with the filter, and implement a renderer that adds HTML and CSS for it; blocks without a renderer go through the fallback, which outputs their raw markup.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Free consultation

Need help with LearnDash?

Tell us what you are working on. A developer who does this every week reads your message and replies within one business day with next steps and a free quote.

  • Free consultation and quote
  • NDA on request
  • You own the source code
  • Reply within one business day

Tell us about your project

Add budget and timeline optional, helps us quote faster

This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.