我正在使用Python 3.3,并尝试使用出色的fgfgen / forge_fdf脚本(感谢伙计,顺便说一句)。
当我尝试运行fdfgen的样本测试时,我返回以下错误。
safe = utf16.replace('\x00)', '\x00\\)').replace('\x00(', '\x00\\(')
TypeError: expected bytes, bytearray or buffer compatible object
经过一番环顾,这似乎是python 3处理unicode编码的结果吗?但我不确定。这是执行的fdfgen代码的示例,然后是提供的fdfgen代码。提前致谢:
>>> from fdfgen import forge_fdf
>>> fields = [('last_name', u'Spencer')]
>>> fdf = forge_fdf('SMBRPython.pdf', fields, [], [], [])
# -*- coding: utf-8 -*-
"""
Port of the PHP forge_fdf library by Sid Steward
(http://www.pdfhacks.com/forge_fdf/)
Anders Pearson <anders@columbia.edu> at Columbia Center For New Media Teaching
and Learning <http://ccnmtl.columbia.edu/>
"""
__author__ = "Anders Pearson <anders@columbia.edu>"
__credits__ = ("Sébastien Fievet <zyegfryed@gmail.com>,"
"Brandon Rhodes <brandon@rhodesmill.org>")
import codecs
def smart_encode_str(s):
"""Create a UTF-16 encoded PDF string literal for `s`."""
utf16 = s.encode('utf_16_be')
safe = utf16.replace('\x00)', '\x00\\)').replace('\x00(', '\x00\\(')
return ('%s%s' % (codecs.BOM_UTF16_BE, safe))
def handle_hidden(key, fields_hidden):
if key in fields_hidden:
return "/SetF 2"
else:
return "/ClrF 2"
def handle_readonly(key, fields_readonly):
if key in fields_readonly:
return "/SetFf 1"
else:
return "/ClrFf 1"
def handle_data_strings(fdf_data_strings, fields_hidden, fields_readonly):
for (key, value) in fdf_data_strings:
if type(value) is bool:
if value:
yield "<<\n/V/Yes\n/T (%s)\n%s\n%s\n>>\n" % (
smart_encode_str(key),
handle_hidden(key, fields_hidden),
handle_readonly(key, fields_readonly),
)
else:
yield "<<\n/V/Off\n/T (%s)\n%s\n%s\n>>\n" % (
smart_encode_str(key),
handle_hidden(key, fields_hidden),
handle_readonly(key, fields_readonly),
)
qq_花开花谢_0
相关分类