@@ -905,12 +905,21 @@ const PDFStringTranslateTable = [
905
905
] ;
906
906
907
907
function stringToPDFString ( str ) {
908
+ // See section 7.9.2.2 Text String Type.
909
+ // The string can contain some language codes bracketed with 0x0b,
910
+ // so we must remove them.
908
911
if ( str [ 0 ] >= "\xEF" ) {
909
912
let encoding ;
910
913
if ( str [ 0 ] === "\xFE" && str [ 1 ] === "\xFF" ) {
911
914
encoding = "utf-16be" ;
915
+ if ( str . length % 2 === 1 ) {
916
+ str = str . slice ( 0 , - 1 ) ;
917
+ }
912
918
} else if ( str [ 0 ] === "\xFF" && str [ 1 ] === "\xFE" ) {
913
919
encoding = "utf-16le" ;
920
+ if ( str . length % 2 === 1 ) {
921
+ str = str . slice ( 0 , - 1 ) ;
922
+ }
914
923
} else if ( str [ 0 ] === "\xEF" && str [ 1 ] === "\xBB" && str [ 2 ] === "\xBF" ) {
915
924
encoding = "utf-8" ;
916
925
}
@@ -919,7 +928,11 @@ function stringToPDFString(str) {
919
928
try {
920
929
const decoder = new TextDecoder ( encoding , { fatal : true } ) ;
921
930
const buffer = stringToBytes ( str ) ;
922
- return decoder . decode ( buffer ) ;
931
+ const decoded = decoder . decode ( buffer ) ;
932
+ if ( ! decoded . includes ( "\x1b" ) ) {
933
+ return decoded ;
934
+ }
935
+ return decoded . replaceAll ( / \x1b [ ^ \x1b ] * (?: \x1b | $ ) / g, "" ) ;
923
936
} catch ( ex ) {
924
937
warn ( `stringToPDFString: "${ ex } ".` ) ;
925
938
}
@@ -928,7 +941,13 @@ function stringToPDFString(str) {
928
941
// ISO Latin 1
929
942
const strBuf = [ ] ;
930
943
for ( let i = 0 , ii = str . length ; i < ii ; i ++ ) {
931
- const code = PDFStringTranslateTable [ str . charCodeAt ( i ) ] ;
944
+ const charCode = str . charCodeAt ( i ) ;
945
+ if ( charCode === 0x1b ) {
946
+ // eslint-disable-next-line no-empty
947
+ while ( ++ i < ii && str . charCodeAt ( i ) !== 0x1b ) { }
948
+ continue ;
949
+ }
950
+ const code = PDFStringTranslateTable [ charCode ] ;
932
951
strBuf . push ( code ? String . fromCharCode ( code ) : str . charAt ( i ) ) ;
933
952
}
934
953
return strBuf . join ( "" ) ;
0 commit comments