- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Windows 7 上使用 Python 3.4.2 和 PyPDF2 1.24(还使用 reportlab 3.1.44,以防有帮助)。
我最近从 Python 2.7 升级到 3.4,并且正在移植我的代码。此代码用于创建一个空白 pdf 页面,其中嵌入了链接(使用 reportlab)并将其与现有 pdf 页面合并(使用 PyPDF2)。我在使用 Reportlab 时遇到了一个问题,因为保存 Canvas 时使用了 StringIO,需要将其更改为 BytesIO,但在执行此操作后,我遇到了此错误:
Traceback (most recent call last):
File "C:\cms_software\pdf_replica\builder.py", line 401, in merge_pdf_files
input_page.mergePage(link_page)
File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 2013, in mergePage
self.mergePage(page2)
File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 2059, in mergePage
page2Content = PageObject._pushPopGS(page2Content, self.pdf)
File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 1973, in _pushPopGS
stream = ContentStream(contents, pdf)
File "C:\Python34\lib\site-packages\PyPDF2\pdf.py", line 2446, in __init
stream = BytesIO(b_(stream.getData()))
File "C:\Python34\lib\site-packages\PyPDF2\generic.py", line 826, in getData
decoded._data = filters.decodeStreamData(self)
File "C:\Python34\lib\site-packages\PyPDF2\filters.py", line 326, in decodeStreamData
data = ASCII85Decode.decode(data)
File "C:\Python34\lib\site-packages\PyPDF2\filters.py", line 264, in decode
data = [y for y in data if not (y in ' \n\r\t')]
File "C:\Python34\lib\site-packages\PyPDF2\filters.py", line 264, in
data = [y for y in data if not (y in ' \n\r\t')]
TypeError: 'in <string>' requires string as left operand, not int
这是回溯提到的行和上面的行:
link_page = self.make_pdf_link_page(pdf, size, margin, scale_factor, debug_article_links)
if link_page != None:
input_page.mergePage(link_page)
以下是 make_pdf_link_page 函数的相关部分:
packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=(size['width'], size['height']))
....# left out code here is just reportlab specifics for size and url stuff
can.linkURL(url, r1, thickness=1, color=colors.green)
can.rect(x1, y1, width, height, stroke=1, fill=0)
# create a new PDF with Reportlab that has the url link embedded
can.save()
packet.seek(0)
try:
new_pdf = PdfFileReader(packet)
except Exception as e:
logger.exception('e')
return None
return new_pdf.getPage(0)
我假设这是使用 BytesIO 的问题,但我无法使用带有 StringIO 的 reportlab 创建页面。这是一个关键功能,过去可以与 Python 2.7 完美配合,因此我非常感谢对此的任何反馈。谢谢!
更新:我还尝试从使用 BytesIO 更改为仅写入临时文件,然后合并。不幸的是我遇到了同样的错误。这是临时文件版本:
import tempfile
temp_dir = tempfile.gettempdir()
temp_path = os.path.join(temp_dir, "tmp.pdf")
can = canvas.Canvas(temp_path, pagesize=(size['width'], size['height']))
....
can.showPage()
can.save()
try:
new_pdf = PdfFileReader(temp_path)
except Exception as e:
logger.exception('e')
return None
return new_pdf.getPage(0)
更新:我发现了一些有趣的信息。看来如果我注释掉 can.rect 和 can.linkURL 调用它就会合并。因此,在页面上绘制任何内容,然后尝试将其与我现有的 pdf 合并会导致错误。
最佳答案
深入研究 PyPDF2 库代码后,我找到了自己的答案。对于 python 3 用户来说,旧的库可能会很棘手。即使他们说他们支持 python 3,他们也不一定测试所有内容。在本例中,问题出在 PyPDF2 中的filters.py 中的 ASCII85Decode 类。对于 python 3,此类需要返回字节。我从 pdfminer3k 借用了相同类型函数的代码,pdfminer3k 是 pdfminer 的 python 3 的端口。如果您将 ASCII85Decode() 类替换为此代码,它将起作用:
import struct
class ASCII85Decode(object):
def decode(data, decodeParms=None):
if isinstance(data, str):
data = data.encode('ascii')
n = b = 0
out = bytearray()
for c in data:
if ord('!') <= c and c <= ord('u'):
n += 1
b = b*85+(c-33)
if n == 5:
out += struct.pack(b'>L',b)
n = b = 0
elif c == ord('z'):
assert n == 0
out += b'\0\0\0\0'
elif c == ord('~'):
if n:
for _ in range(5-n):
b = b*85+84
out += struct.pack(b'>L',b)[:n-1]
break
return bytes(out)
关于python-3.4 - 移植到Python3 : PyPDF2 mergePage() gives TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27974409/
编译器知道AbstractDemo是一个抽象类,抽象类不能被实例化。 但是当我调用 newInstance() 方法时,为什么它没有给出编译时错误? import java.lang.reflect.
假设我有如下数据类型: data Cell = Cell (Maybe Player) data Board = Board [[Cell]] 现在我想生成一个这样的递归函数: genBoard
当谈到使用 OpenMP 和 TBB 进行共享内存编程时,我是一个初学者。 我正在实现 QuickHull 算法 ( http://en.wikipedia.org/wiki/QuickHull )
我想创建一个随机列表与列表中元素的不同组合。出现在最终列表中的每个元素的计数应该相同。。例如。在这里,我期望所有元素的计数都是10,因为我给了相同的权重。但这并不总是一样的。如何修改代码以使所有元素的
我想创建一个随机列表与列表中元素的不同组合。出现在最终列表中的每个元素的计数应该相同。。例如。在这里,我期望所有元素的计数都是10,因为我给了相同的权重。但这并不总是一样的。如何修改代码以使所有元素的
以下将显示在 Firebug 或 jsconsole.com 中或在其他 Javascript 交互式控制台中: >>> foo = { a : 1, b : 2.2 } Object { a=1,
在 Azure DevOps 管道中的一项任务中,我尝试停止 IIS 服务器。这可以通过在命令提示符中调用命令net stop WAS来实现。手动执行此操作,它会要求确认 手动方式,我只需按 Y EN
在 Azure DevOps 管道中的一项任务中,我尝试停止 IIS 服务器。这可以通过在命令提示符中调用命令net stop WAS来实现。手动执行此操作,它会要求确认 手动方式,我只需按 Y EN
在 R 编码中出现以下错误。 在我的 Brand_X.xlsx 数据集中,我尝试使用 KNN 插补法计算的 NA 值很少,但我得到的结果低于错误。这里有什么问题吗?谢谢! > library(read
在 Android Studio 中,我希望我的每个应用变体都有自己的图标。 我尝试了各种方法,包括此处建议的方法 How to provide different Android app icons
JSFiddle 示例代码:http://jsfiddle.net/SUMPq/8/ 我在容器上有一些文本,代码位于 HTML 的顶部,靠近正文: Heading TAG SOME TEXTSOM
所以我正在实现这个简单的剃须刀支付集成。但它给我一个“没有找到合适的付款方式”的错误。我之前尝试过选择付款选项表,但也没有用。 val razorpay = RazorpayClient("my
Closed. This question needs to be more focused。它当前不接受答案。 想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
我在玩 elm-css . 大多数事情都按我的预期工作。 但是我无法为 Css.opacity 提供正确的值功能。 这是我尝试过的: Css.opacity 0.5 这给出了错误: Function
请参阅以下代码: UIImage *image; NSString *str = [[[Data getInstance]arrPic]objectAtIndex:rowIndex]; NSLog(s
这个问题已经有答案了: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key",
我正在测试一个用于修改文件的工具,在此过程中一个相当重要的功能是告诉文件大小,尤其是当文件仍然打开时。 $file = tempnam('/tmp', 'test_'); file_put_conte
我是Java/Maven新手,我正在尝试构建一个maven Spring Boot项目,它很早就可以工作并且也成功创建了jar包。但它突然停止工作并开始出现 Maven 编译错误。 我知道它与 pom
我正在尝试让我的 pom.xml 在我的 JAXB 对象上生成 hashCode() 和 equals method()。 4.0.0 0.0.1-SNAPSHOT jar
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我是一名优秀的程序员,十分优秀!