sanitize_text_field( $title ), 'content' => $text, 'excerpt' => $excerpt, ); } /** * Attempts to locate and parse the /Title in PDF Info or Metadata dictionary. * * @param string $data * @return string */ private static function extract_title( $data ) { // Look for /Title (Some Title) or /Title if ( preg_match( '/\/Title\s*\(([^)]+)\)/i', $data, $matches ) ) { return self::decode_pdf_string( $matches[1] ); } if ( preg_match( '/\/Title\s*<([0-9a-fA-F]+)>/i', $data, $matches ) ) { return self::decode_hex_string( $matches[1] ); } return ''; } /** * Extracts text by locating all streams and decompressing / decoding them. * * @param string $data * @return string */ private static function extract_text( $data ) { $all_text = ''; // Find all stream ... endstream blocks. $offset = 0; while ( false !== ( $start = strpos( $data, 'stream', $offset ) ) ) { $header_start = max( 0, $start - 300 ); $header = substr( $data, $header_start, $start - $header_start ); // Check stream boundaries. $stream_content_start = $start + 6; if ( "\r\n" === substr( $data, $stream_content_start, 2 ) ) { $stream_content_start += 2; } elseif ( "\n" === substr( $data, $stream_content_start, 1 ) || "\r" === substr( $data, $stream_content_start, 1 ) ) { $stream_content_start += 1; } $end = strpos( $data, 'endstream', $stream_content_start ); if ( false === $end ) { break; } $stream_data = substr( $data, $stream_content_start, $end - $stream_content_start ); $offset = $end + 9; // Check if stream is FlateDecode compressed. $is_flate = ( false !== stripos( $header, '/FlateDecode' ) ); $uncompressed = ''; if ( $is_flate && function_exists( 'gzuncompress' ) ) { $uncompressed = @gzuncompress( $stream_data ); if ( false === $uncompressed ) { // Fallback for zlib stream headers. $uncompressed = @gzinflate( substr( $stream_data, 2 ) ); } } else { $uncompressed = $stream_data; } if ( ! empty( $uncompressed ) ) { $stream_text = self::extract_text_from_stream( $uncompressed ); if ( ! empty( $stream_text ) ) { $all_text .= $stream_text . "\n\n"; } } } return $all_text; } /** * Extracts text between BT (Begin Text) and ET (End Text) operators. * * @param string $stream * @return string */ private static function extract_text_from_stream( $stream ) { $text = ''; $blocks = array(); // Match BT ... ET blocks. if ( preg_match_all( '/BT\s*(.*?)\s*ET/s', $stream, $matches ) ) { foreach ( $matches[1] as $block ) { $lines = explode( "\n", $block ); $block_text = ''; foreach ( $lines as $line ) { $line = trim( $line ); // Handle TJ (array of strings and kerning offsets). if ( preg_match_all( '/\[(.*?)\]\s*TJ/', $line, $tj_matches ) ) { foreach ( $tj_matches[1] as $tj_content ) { $block_text .= self::parse_tj_array( $tj_content ); } $block_text .= ' '; } // Handle Tj (single string). if ( preg_match_all( '/\((.*?)\)\s*Tj/', $line, $tj_single ) ) { foreach ( $tj_single[1] as $single_str ) { $block_text .= self::decode_pdf_string( $single_str ); } $block_text .= ' '; } // Handle ' or " (string with newline). if ( preg_match( '/\((.*?)\)\s*[\'"]/', $line, $quote_match ) ) { $block_text .= "\n" . self::decode_pdf_string( $quote_match[1] ); } // Handle Hex strings: <48656c6c6f> Tj if ( preg_match_all( '/<([0-9a-fA-F]+)>\s*Tj/', $line, $hex_matches ) ) { foreach ( $hex_matches[1] as $hex_str ) { $block_text .= self::decode_hex_string( $hex_str ); } $block_text .= ' '; } } if ( ! empty( $block_text ) ) { $blocks[] = trim( $block_text ); } } } return implode( "\n", $blocks ); } /** * Parses a TJ array like [(Hello) -10 (World)] * * @param string $content * @return string */ private static function parse_tj_array( $content ) { $result = ''; $len = strlen( $content ); $in_string = false; $in_hex = false; $current = ''; for ( $i = 0; $i < $len; $i++ ) { $char = $content[ $i ]; if ( '(' === $char && ! $in_string && ! $in_hex ) { $in_string = true; $current = ''; } elseif ( ')' === $char && $in_string ) { $in_string = false; $result .= self::decode_pdf_string( $current ); } elseif ( '<' === $char && ! $in_string && ! $in_hex ) { $in_hex = true; $current = ''; } elseif ( '>' === $char && $in_hex ) { $in_hex = false; $result .= self::decode_hex_string( $current ); } elseif ( $in_string || $in_hex ) { $current .= $char; } } return $result; } /** * Decodes PDF escaped string characters. * * @param string $str * @return string */ private static function decode_pdf_string( $str ) { // Escape sequences: \n, \r, \t, \b, \f, \(, \), \\, \ddd (octal) $str = preg_replace_callback( '/\\\\([0-7]{1,3})/', function ( $m ) { return chr( octdec( $m[1] ) ); }, $str ); $replacements = array( '\\n' => "\n", '\\r' => "\r", '\\t' => "\t", '\\b' => '', '\\f' => '', '\\(' => '(', '\\)' => ')', '\\\\' => '\\', ); $decoded = strtr( $str, $replacements ); // Check for UTF-16BE BOM (\xFE\xFF) if ( 0 === strncmp( $decoded, "\xFE\xFF", 2 ) ) { $converted = @mb_convert_encoding( substr( $decoded, 2 ), 'UTF-8', 'UTF-16BE' ); if ( false !== $converted ) { return $converted; } } return $decoded; } /** * Decodes hexadecimal string into UTF-8. * * @param string $hex * @return string */ private static function decode_hex_string( $hex ) { $bin = @hex2bin( $hex ); if ( false === $bin ) { return ''; } if ( 0 === strncmp( $bin, "\xFE\xFF", 2 ) ) { $converted = @mb_convert_encoding( substr( $bin, 2 ), 'UTF-8', 'UTF-16BE' ); if ( false !== $converted ) { return $converted; } } return $bin; } }