When you run a WooCommerce store with hundreds or thousands of SKUs (flooring, wall panels, hardware, or B2B supplies), HTML price list pages are great for search traffic, while contractors and buyers still ask for a downloadable PDF to forward to clients.
Our first naive instinct was to generate static .pdf files under /wp-content/uploads/ and link directly to them. Within weeks, three practical issues showed up:
-
Stale prices behind aggressive caching. Static
.pdfassets matched nginxexpires maxrules and proxy caches, so buyers kept downloading last month's prices after a catalog update. -
Duplicate content signals for search engines. Search bots started crawling both the HTML price page and the PDF version. Adding
X-Robots-Tag: noindexalongside acanonicalheader caused mixed signals. -
Random HTTP 500 errors during live timestamp stamping. We wanted every downloaded PDF to show the exact download timestamp and a short validity note on page 1. On large multi-brand catalogs with 400+ thumbnail images, opening and stamping the PDF on the fly occasionally pushed a PHP worker past
memory_limit.
Here is the pattern we ended up using in PHP 8.1+ on WordPress to keep PDF downloads fast, fresh, SEO-safe, and resilient under low memory.
1. Serve PDFs through a lightweight PHP query endpoint
Instead of exposing raw static upload paths, route downloads through the price page's own slug (for example, /price-list-slug/?download=pdf). The base PDF is still pre-generated asynchronously whenever product prices in that table change, so the endpoint only reads a pre-built file, applies an optional per-minute timestamp stamp, and streams it with explicit HTTP headers.
2. Check available memory before stamping, and fall back to HTTP 200
Never let a cosmetic watermark turn a working PDF download into a 500 Internal Server Error. Before running your PDF stamping library (such as mPDF/FPDI), measure remaining headroom below memory_limit. If headroom is tight or stamping throws an exception, serve the cached base PDF untouched and log a notice for later inspection.
Below is a clean, generalized starter pattern (wpdpl_ prefix) you can drop into a custom plugin:
<?php
/**
* Generalized pattern: serve pre-built WooCommerce price list PDFs
* with canonical SEO headers and an OOM-safe timestamp fallback.
*/
defined( 'ABSPATH' ) || exit;
/**
* Check whether PHP has at least $required_bytes left under memory_limit.
*/
function wpdpl_has_memory_headroom( int $required_bytes ): bool {
$raw_limit = (string) ini_get( 'memory_limit' );
if ( '-1' === trim( $raw_limit ) ) {
return true;
}
$limit = wp_convert_hr_to_bytes( $raw_limit );
if ( $limit <= 0 ) {
return true;
}
$used = memory_get_usage( true );
return ( $limit - $used ) >= $required_bytes;
}
/**
* Resolve the file to serve: try a per-minute stamped copy if memory allows,
* otherwise fall back gracefully to the pre-built base PDF.
*/
function wpdpl_prepare_pdf_file( int $page_id, string $base_pdf, callable $stamp_callback ): string {
$required_bytes = 48 * MB_IN_BYTES;
if ( ! wpdpl_has_memory_headroom( $required_bytes ) ) {
error_log( sprintf( '[wpdpl] Skipping PDF stamp for page %d: low memory headroom.', $page_id ) );
return $base_pdf;
}
$minute_bucket = gmdate( 'YmdHi' );
$stamped_pdf = trailingslashit( dirname( $base_pdf ) ) . "tmp/stamped-{$page_id}-{$minute_bucket}.pdf";
if ( is_readable( $stamped_pdf ) ) {
return $stamped_pdf;
}
try {
$ok = (bool) $stamp_callback( $base_pdf, $stamped_pdf, current_time( 'd/m/Y H:i' ) );
return ( $ok && is_readable( $stamped_pdf ) ) ? $stamped_pdf : $base_pdf;
} catch ( Throwable $e ) {
error_log( sprintf( '[wpdpl] Stamp failed for page %d: %s', $page_id, $e->getMessage() ) );
return $base_pdf;
}
}
/**
* Stream PDF with clean SEO headers:
* - Published pages: send Link rel="canonical" pointing to the HTML page (no conflicting noindex).
* - Draft previews: send X-Robots-Tag: noindex.
*/
function wpdpl_stream_price_list_pdf( int $page_id, string $pdf_path, string $filename ): void {
if ( ! is_readable( $pdf_path ) ) {
status_header( 404 );
exit;
}
$is_published = ( 'publish' === get_post_status( $page_id ) );
$canonical_url = get_permalink( $page_id );
nocache_headers();
header( 'Content-Type: application/pdf' );
header( 'Content-Disposition: inline; filename="' . sanitize_file_name( $filename ) . '"' );
header( 'Content-Length: ' . (string) filesize( $pdf_path ) );
if ( $is_published && $canonical_url ) {
header( 'Link: <' . esc_url_raw( $canonical_url ) . '>; rel="canonical"' );
} else {
header( 'X-Robots-Tag: noindex' );
}
// On HEAD requests, send headers only without streaming or stamping.
if ( 'HEAD' === strtoupper( $_SERVER['REQUEST_METHOD'] ?? 'GET' ) ) {
exit;
}
readfile( $pdf_path );
exit;
}
3. Why Link: rel="canonical" without noindex worked better for us
A common SEO mistake is putting both X-Robots-Tag: noindex and Link: <html-url>; rel="canonical" on the same PDF response. Those two directives pull in opposite directions: noindex tells Google to drop the URL completely, while canonical asks Google to consolidate signals toward the HTML page.
Our cleaner split:
-
Published PDFs: omit
noindex, sendLink: <html-page-url>; rel="canonical", and placerel="nofollow"on the HTML download button so crawlers prioritize the HTML table. -
Draft/preview PDFs: require edit permissions and send
X-Robots-Tag: noindex. - HEAD requests: exit right after sending headers so uptime monitors and link checkers never trigger disk writes or PDF stamping.
You can see a live multi-level WooCommerce price list built around this HTML + PDF workflow on 3K Home's price directory (https://3khome.com/), where each brand-level sheet also embeds pre-resized 96px JPEG thumbnails so a 450-image PDF still builds in around a second.
Trade-offs still open on our side
Two details we are still refining:
-
Per-minute stamped cache cleanup: caching stamped files by
YmdHiavoids re-stamping bursts of clicks in the same minute, though you still need a nightly cron to prune old temporary files. - Image-heavy catalogs vs. lightweight master PDFs: keeping thumbnails only on brand-level sheets while leaving the full master catalog text-only keeps the full PDF small, though some buyers still ask for thumbnails everywhere.
How do you handle downloadable PDF price lists or datasheets in WordPress/WooCommerce? Do you prefer Link: rel="canonical" back to the HTML page, blocking PDFs via robots.txt, or offloading PDF generation completely outside PHP?
Top comments (0)