pages[] = ''; $this->p = count( $this->pages ) - 1; } /** @return int Number of pages. */ public function count() { return count( $this->pages ); } /** Switch to a page (for footers). */ public function set_page( $i ) { $this->p = (int) $i; } /** @return string UTF-8 → Windows-1252. */ private static function enc( $s ) { $s = (string) $s; $s = str_replace( array( "\u{2018}", "\u{2019}", "\u{201C}", "\u{201D}", "\u{2013}", "\u{2014}", "\u{2026}", "\u{00A0}", "\u{2022}", "\u{2713}" ), array( "'", "'", '"', '"', '-', '-', '...', ' ', '-', 'v' ), $s ); if ( function_exists( 'iconv' ) ) { $c = @iconv( 'UTF-8', 'Windows-1252//TRANSLIT//IGNORE', $s ); // phpcs:ignore if ( false !== $c ) { return $c; } } return preg_replace( '/[^\x20-\x7E]/', '?', $s ); } /** @return float Width of a Windows-1252 string in points. */ private static function width_enc( $s, $size, $bold ) { $w = 0; $n = strlen( $s ); for ( $i = 0; $i < $n; $i++ ) { $o = ord( $s[ $i ] ); $w += ( $o >= 32 && $o <= 126 ) ? self::$w[ $o - 32 ] : 556; } return $w * $size / 1000 * ( $bold ? 1.06 : 1 ); } /** @return float Width of a UTF-8 string. */ public function width( $s, $size, $bold = false ) { return self::width_enc( self::enc( $s ), $size, $bold ); } /** * Wrap text to a width. * * @return string[] Lines (UTF-8). */ public function wrap( $text, $size, $max, $bold = false ) { $out = array(); foreach ( preg_split( '/\r\n|\r|\n/', (string) $text ) as $para ) { $line = ''; foreach ( preg_split( '/(\s+)/', $para, -1, PREG_SPLIT_NO_EMPTY ) as $word ) { $try = '' === $line ? $word : $line . ' ' . $word; if ( $this->width( $try, $size, $bold ) <= $max ) { $line = $try; continue; } if ( '' !== $line ) { $out[] = $line; } // Break very long words (URLs). while ( $this->width( $word, $size, $bold ) > $max ) { $cut = max( 1, (int) floor( strlen( $word ) * $max / $this->width( $word, $size, $bold ) ) - 1 ); $out[] = substr( $word, 0, $cut ); $word = substr( $word, $cut ); } $line = $word; } $out[] = $line; } return $out; } /** @return string "r g b" from #rrggbb. */ private static function rgb( $hex ) { $hex = ltrim( (string) $hex, '#' ); if ( 6 !== strlen( $hex ) ) { return '0 0 0'; } return sprintf( '%.3F %.3F %.3F', hexdec( substr( $hex, 0, 2 ) ) / 255, hexdec( substr( $hex, 2, 2 ) ) / 255, hexdec( substr( $hex, 4, 2 ) ) / 255 ); } /** Text at x, y (y from the top of the page, baseline). */ public function text( $x, $y, $s, $size = 10, $bold = false, $color = '#111827' ) { $e = str_replace( array( '\\', '(', ')' ), array( '\\\\', '\\(', '\\)' ), self::enc( $s ) ); $this->pages[ $this->p ] .= sprintf( "BT /%s %.2F Tf %s rg %.2F %.2F Td (%s) Tj ET\n", $bold ? 'F2' : 'F1', $size, self::rgb( $color ), $x, self::H - $y, $e ); } /** Right-aligned text ending at x. */ public function text_right( $x, $y, $s, $size = 10, $bold = false, $color = '#111827' ) { $this->text( $x - $this->width( $s, $size, $bold ), $y, $s, $size, $bold, $color ); } /** Line. */ public function line( $x1, $y1, $x2, $y2, $color = '#e5e7eb', $w = 0.6 ) { $this->pages[ $this->p ] .= sprintf( "%s RG %.2F w %.2F %.2F m %.2F %.2F l S\n", self::rgb( $color ), $w, $x1, self::H - $y1, $x2, self::H - $y2 ); } /** Filled rectangle (x, y = top-left). */ public function rect( $x, $y, $w, $h, $color = '#f3f4f6' ) { $this->pages[ $this->p ] .= sprintf( "%s rg %.2F %.2F %.2F %.2F re f\n", self::rgb( $color ), $x, self::H - $y - $h, $w, $h ); } /** @return string The PDF file. */ public function output( $title = '' ) { $objs = array(); $objs[] = '<< /Type /Catalog /Pages 2 0 R >>'; $kids = array(); $n = count( $this->pages ); for ( $i = 0; $i < $n; $i++ ) { $kids[] = ( 5 + $i * 2 ) . ' 0 R'; } $objs[] = '<< /Type /Pages /Kids [' . implode( ' ', $kids ) . '] /Count ' . $n . ' >>'; $objs[] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding /WinAnsiEncoding >>'; $objs[] = '<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold /Encoding /WinAnsiEncoding >>'; foreach ( $this->pages as $i => $stream ) { $objs[] = sprintf( '<< /Type /Page /Parent 2 0 R /MediaBox [0 0 %.2F %.2F] /Resources << /Font << /F1 3 0 R /F2 4 0 R >> >> /Contents %d 0 R >>', self::W, self::H, 6 + $i * 2 ); $objs[] = "<< /Length " . strlen( $stream ) . " >>\nstream\n" . $stream . "endstream"; } $info = '<< /Title (' . str_replace( array( '\\', '(', ')' ), array( '\\\\', '\\(', '\\)' ), self::enc( $title ) ) . ') /Producer (Fieldwren) /CreationDate (D:' . gmdate( 'YmdHis' ) . 'Z) >>'; $objs[] = $info; $pdf = "%PDF-1.4\n%\xE2\xE3\xCF\xD3\n"; $offs = array(); foreach ( $objs as $i => $o ) { $offs[] = strlen( $pdf ); $pdf .= ( $i + 1 ) . " 0 obj\n" . $o . "\nendobj\n"; } $xref = strlen( $pdf ); $pdf .= "xref\n0 " . ( count( $objs ) + 1 ) . "\n0000000000 65535 f \n"; foreach ( $offs as $o ) { $pdf .= sprintf( "%010d 00000 n \n", $o ); } $pdf .= 'trailer << /Size ' . ( count( $objs ) + 1 ) . ' /Root 1 0 R /Info ' . count( $objs ) . " 0 R >>\nstartxref\n" . $xref . "\n%%EOF"; return $pdf; } }