+
80
-

python如何生成双层pdf?

python如何生成双层pdf?


网友回复

+
0
-

双层PDF就是有两层结构的文件,一层是图片,一层是文字。

双层PDF的优点显而易见,既保留了原文件尤其是原图片的真实状态,又因为具有文本层,方便了数据管理和索引。

采用reportlab 来生成双层pdf

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
import os
# import urllib2
import time
from reportlab import platypus
from reportlab.lib.pagesizes import letter
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Image
from reportlab.pdfgen import canvas

image_file = "/data/wwwroot/default/asset/pdf.png"

# Use Canvas to generate pdf
c = canvas.Canvas('/data/wwwroot/default/Data/reportlab_canvasdemo.pdf', pagesize=letter)
width, height = letter

c.setFillColorRGB(0,0.77,0.77)
# say hello (note after rotate the y coord needs to be negative!)
c.drawString( 3*inch, 3*inch, "Hello World")
c.drawImage(image_file, 0 , 0)
c.showPage()
c.save()

+
0
-

读取pdf文件获取文件信息,代码如下:

#!/usr/local/python3/bin/python3
# -*- coding: utf-8 -*
from PyPDF2 import PdfFileWriter, PdfFileReader

output = PdfFileWriter()
input1 = PdfFileReader(open("/data/wwwroot/default/asset/bfw.pdf", "rb"))

# print document info
print(input1.getDocumentInfo())

# print how many pages input1 has:
print ("pdf_document.pdf has %d pages." % input1.getNumPages())

# print page content
page_content = input1.getPage(0).extractText()
print( page_content )

# add page 1 from input1 to output document, unchanged
output.addPage(input1.getPage(0))

# add page 2 from input1, but rotated clockwise 90 degrees
#output.addPage(input1.getPage(1).rotateClockwise(90))

# finally, write "output" to document-output.pdf
outputStream = open("/data/wwwroot/default/Data/PyPDF2-output.pdf", "wb")
output.write(outputStream)

我知道答案,我要回答