prepare_html_for_render( $html, $opts ); $paper = isset( $opts['paper'] ) ? $opts['paper'] : 'A4'; if ( ! empty( $opts['fit_height'] ) && \is_array( $paper ) ) { $opts['paper'] = $this->fit_height_paper( $html, $opts, $paper ); } return (string) $this->build( $html, $opts )->output(); } /** * Prepare receipt HTML for Dompdf's locked-down render environment. * * @param string $html HTML document to render. * @param array $opts Render options. * * @return string Prepared HTML. */ private function prepare_html_for_render( string $html, array $opts ): string { // Opt-in: the rewrite encodes receipt-layout knowledge (including legacy // template class names), so it is only applied when the caller asks for it // (receipt rendering) rather than for every generic HTML document. if ( ! empty( $opts['receipt_layout'] ) ) { $preprocessor = new Pdf_Layout_Preprocessor(); $html = $preprocessor->process( $html ); // The preprocessor owns full-document detection so the two sides // can never disagree about which treatment an input received. if ( $preprocessor->is_full_document() ) { // Full HTML documents (the legacy-php receipt template) keep // their own
stylesheet, charset and Dompdf's default // page margins; the preprocessor rewrote their flex containers // (inline-styled and known legacy classes) in place. return $this->embed_local_images( $html ); } // Match the browser preview: the template's own root padding is // the only whitespace around the receipt, so it replaces Dompdf's // default 1.2cm page margin (and keeps later pages consistent // with page one). $margins = $preprocessor->get_page_margins_pt(); $page_style = ''; // Receipts are UTF-8 fragments with no charset declaration; // without one Dompdf sniffs the encoding and mostly-ASCII // receipts with a stray multibyte character (e.g. an em dash) // get mis-decoded. $charset_meta = ''; $html = $this->inject_head_styles( $html, $charset_meta . $page_style ); } return $this->embed_local_images( $html ); } /** * Format a float for CSS output, immune to LC_NUMERIC comma locales. * * @param float $value The value to format. * * @return string The formatted number. */ private static function css_number( float $value ): string { $formatted = rtrim( rtrim( number_format( $value, 2, '.', '' ), '0' ), '.' ); return '' === $formatted ? '0' : $formatted; } /** * Prepend compatibility stylesheets to the receipt HTML. * * Inserted just before `` when the HTML is a full document, otherwise * prepended to the fragment (Dompdf wraps loose markup in html/body itself). * Placing the stylesheet last in the head lets its `!important` rules win the * cascade over template styles. * * @param string $html The receipt HTML. * @param string $styles The