我正在尝试通过从后端将其转换为二进制字符串来显示 PDF。 这是我正在进行的 ajax 调用
$.ajax({
type : 'GET',
url : '<url>',
data : oParameters,
contentType : 'application/pdf;charset=UTF-8',
success : function(odata) {
window.open("data:application/pdf;charset=utf-8," + escape(odata));
} });
当我尝试在新窗口中打开 PDF 时,url 看起来像
data:application/pdf;charset=utf-8,%25PDF-1.3%0D%0A%25%uFFFD%uFFFD%uFFFD%uFFFD%0D%0A2%200%20obj%0D%0A/WinAnsiEncoding%0D........
如您所见,它使用“WinAnsiEncoding”来显示 PDF。因此,某些字符无法正确显示。如何将其更改为 UTF-8?
编辑:后端在 ABAP 中。我正在使用功能模块“CONVERT_OTF”将 smartform 转换为 OTF,然后再转换为字符串。
CALL FUNCTION fname
EXPORTING
user_settings = space
control_parameters = ls_ctropt
output_options = ls_output
gv_lang = lv_lang
IMPORTING
job_output_info = ls_body_text
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
IMPORTING
bin_filesize = ls_pdf_len
bin_file = ls_pdf_xstring
TABLES
otf = ls_body_text-otfdata
lines = lt_lines
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
err_bad_otf = 4
OTHERS = 5.
CALL METHOD server->response->set_header_field( name = 'Content-Type'
value = 'application/pdf;charset=UTF-8' ).
CALL METHOD server->response->append_data( data = lv_pdf_string
length = lv_len ).
最佳答案
关于您关于它使用“WinAnsiEncoding”来显示 PDF 的评论:
在逗号之后
data:application/pdf;charset=utf-8,%25PDF-1.3%0D%0A%25%uFFFD%uFFFD%uFFFD%uFFFD%0D%0A2%200%20obj%0D%0A/WinAnsiEncoding%0D........
一切都是纯数据。因此,“WinAnsiEncoding”只是PDF内容的一部分,如果这是您遇到麻烦的原因,则必须要求PDF生成器更改他的PDF生成过程。
在本例中,您的数据是:
%PDF-1.3
%...
2 0 obj
/WinAnsiEncoding
........
这是完全正常的 PDF 结构。它只是意味着 PDF 对象 2 被定义为 /WinAnsiEncoding
可能会或可能不会用于某些字体定义,即使它被使用,它仍然可能被某些 /差异 以包含您需要的字符。此外,将其更改为 UTF-8(根据您的要求)没有意义,因为 UTF-8 不是 PDF 页面内容的标准编码。如果您以某种方式将 UTF-8
放在那里,您将进一步破坏 PDF。
不过,恐怕还有其他问题。
您将 charset 参数添加到类型 application/pdf --- 这没有意义,PDF 是二进制格式,即一系列字节是预期的,因此不涉及字符集。
您的方法调用
escape(odata)
创建了 %uFFFD%uFFFD%uFFFD%uFFFD --- 根据仅定义<的 RFC,这是无效的/p>A percent-encoding mechanism is used to represent a data octet in a component when that octet's corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component. A percent-encoded octet is encoded as a character triplet, consisting of the percent character "%" followed by the two hexadecimal digits representing that octet's numeric value.
(RFC 3986, section 2.1)
Because the percent ("%") character serves as the indicator for percent-encoded octets, it must be percent-encoded as "%25" for that octet to be used as data within a URI.
(ibidem, section 2.4)
因此,%uFFFD%uFFFD%uFFFD%uFFFD是无效的。
PDF 是一种二进制格式,更适合 Base64 编码,即
data:application/pdf;base64,BASE_64_ENCODED_PDF
因此,我建议您相应地更改您的客户端流程。
关于javascript - 在 PDF 中显示 UTF-8 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16040836/