Creating Multiple PDFs with Python Cairo

拜拜、爱过 提交于 2020-01-16 02:12:10

问题


Alright, I've been coming here for help for years, and now I finally have a question that isn't covered. I'm trying to take a .txt file from my AS/400 print queue(no problems here), and output each "page" as a PDF(the next step is merging them into one file). Ive got the formatting part down, and I can create a single page document correctly with no issues.

I've taken the code that generates the single pages, and expanded it to handle files that contain multiple pages when "printed" with adobe writer. The code below crashes python (report error to windows) at a specific spot, and I'm just looking for clarification on why. My current code is very sloppy, I'm mainly looking for functionality first, and I do plan on rewriting most of this.

The input()'s and print()'s are there so I can see where any errors occur

The section that casuses the crash:

for line in lineDict[x]:
    context.show_text(line)
    yPos += 14
    context.move_to(10, yPos)
    context.show_page()
    surface.finish()
    print(line) #Testing where crash occurs 

Below is the full program

import cairo
width, height = 792,612
myText=open("QSYSPRT120972.txt","r")
yPos = 10
lineList=[]
lineDict=dict()
x=0
surface = cairo.PDFSurface(outPDF+".pdf", width, height)
context = cairo.Context(surface)
context.set_source_rgb( 1, 1, 1)
context.rectangle(0, 0, width, height)
context.fill()
context.set_font_size(8)
context.select_font_face( "Courier")
context.move_to(10, yPos)
context.set_source_rgb(0, 0, 0)

lineList=[str(line.replace('\x00', '').encode("ascii", "ignore")).lstrip('b\'').replace('\\n','').rstrip("'").replace(' ','  ') for line in myText]
outPDF=lineList[0]
outPDF=outPDF[0:6]#Get the file name from the first line on each page
input("Press Enter to continue")
print(lineList[0])
while '' in lineList:
    lineList.remove('')
ll=';'.join(lineList)
#print(ll)
LoL=ll.split(outPDF)
LoL.remove('')
input("Press enter")
for pages in LoL:
    yPos = 10
    outPDF=lineList[0]
    outPDF=outPDF[0:6]
    LoL[x]=outPDF+lol[x]
    LoL[x].split(";;")
    outPDF=outPDF+str(x)# Not the best way to do this, but its okay for now
    tempLOL=LoL[x]
    tempLOL=tempLOL.split(";")
    while '' in tempLOL:
    tempLOL.remove('')
    print(tempLOL) #More testing
    lineDict[x]=tempLOL
    for line in lineDict[x]: #Crash happens here. Works fine if I just print each line to console
        context.show_text(line)
        yPos += 14
        context.move_to(10, yPos)
        context.show_page()
        surface.finish()
        print(line) #Testing where crash occurs 
    x+=1
    print("File: "+outPDF+".pdf Created!")
#print(lineDict)
input()

Version that only outputs single paged PDFs

import cairo
width, height = 792,612
myText=open("QSYSPRT120972.txt","r")
yPos = 10
lineList=[]

lineList=[str(line.replace('\x00', '').encode("ascii", "ignore")).lstrip('b\'').replace('\\n','').rstrip("'").replace(' ','  ') for line in myText]
outPDF=lineList[0]
outPDF=outPDF[0:6]
surface = cairo.PDFSurface( outPDF+".pdf", width, height)
context = cairo.Context( surface)
context.set_source_rgb( 1, 1, 1)
context.rectangle( 0, 0, width, height)
context.fill()
context.set_font_size(8)
context.select_font_face( "Arial")
context.move_to( 10, yPos)
context.set_source_rgb( 0, 0, 0)
while '' in lineList:
    lineList.remove('')

for line in lineList:
    context.show_text(line)
    yPos += 14
    context.move_to(10, yPos)
context.show_page()
surface.finish()
print("File: "+outPDF+".pdf Created!")

This is really the only relevant question I've found regarding the issue, but from all the print()'s I'm doing, I know that my lists are set to the correct data each iteration.

python: open file, feed line to list, process list data

FWIW I'm on Windows XP.

Thanks.

来源:https://stackoverflow.com/questions/17154330/creating-multiple-pdfs-with-python-cairo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!