--- ECHO_content/ECHO_collection.py 2004/11/27 22:38:43 1.190 +++ ECHO_content/ECHO_collection.py 2004/11/29 21:55:29 1.193 @@ -9,7 +9,13 @@ class ECHO_externalLink contains informa """ +from reportlab.pdfgen import canvas +from reportlab.lib.pagesizes import A4 + + + import string +import tempfile import re import os import OFS.Image @@ -272,6 +278,7 @@ def manage_addECHO_fullText(self, id, ti REQUEST.RESPONSE.redirect(u+'/manage_main') return '' + class ECHO_resource(Folder,Persistent,ECHO_basis): """ECHO Ressource""" security=ClassSecurityInfo() @@ -283,6 +290,91 @@ class ECHO_resource(Folder,Persistent,EC security.declareProtected('View','index_html') + + + security.declareProtected('View','createPDF') + def createPDF(self,RESPONSE=None,local=None,dpi=150): + """erzeuge pdf file""" + pages=1 + dpi=float(dpi) + imagePath=self.getImagePath().replace("/mpiwg/online","") + + + image="http://nausikaa2.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler?fn="+imagePath+"&dw=%i&dh=%i&pn=%i" + xmlInfo="http://nausikaa2.mpiwg-berlin.mpg.de/digitallibrary/dlInfo-xml.jsp?fn="+imagePath + + dom=xml.dom.minidom.parse(urllib.urlopen(xmlInfo)) + for parameter in dom.getElementsByTagName('parameter'): + if parameter.getAttribute('name')=="pt": + pages=int(parameter.getAttribute('value')) + break + + tempdir="/tmp/archivesImageServer" + if not os.path.exists(tempdir): + os.mkdir(tempdir) + + tmpPath=tempfile.mkdtemp(dir=tempdir) + + + tmpZip=tempfile.mktemp(dir=tempdir) + + tmpFn=os.path.split(tmpZip)[1] + + + + + + if RESPONSE: + RESPONSE.setHeader("Content-Type","text/html") + RESPONSE.write("

I am creating the pdf

") + txt="

1. step: getting the images( %i pages)

"%pages + RESPONSE.write(txt) + + c=canvas.Canvas(tmpZip) + for i in range(1,pages+1): + if RESPONSE: + RESPONSE.write(str("

Get Page: %i
\n"%i)) + faktor=dpi/72.0 + + fn=tmpPath+"/%i"%i + + width,height=A4 + #print image%(width*faktor,height*faktor,i) + url=urllib.urlopen(image%(width*faktor,height*faktor,i)).read() + fh=file(fn,"w") + fh.write(url) + fh.close() + + + + c.drawImage(fn,0,0,width=width,height=height) + c.showPage() + c.save() + if RESPONSE: + RESPONSE.write("

finished
\n") + + if RESPONSE: + len=os.stat(tmpZip)[6] + downloadUrl=self.absolute_url()+"/downloadPDF" + RESPONSE.write("""

Click here for download ( %i Byte)

\n"""%(tmpFn,len)) + RESPONSE.write("""

The file will be stored for a while, you can download it later, the URL is:

+

%s?fn=%s\n"""%(tmpFn,downloadUrl,tmpFn)) + RESPONSE.close() + + + def downloadPDF(self,fn): + """download prepared set""" + filename="/tmp/archivesImageServer/"+fn + namePDF=self.getId()+".pdf" + self.REQUEST.RESPONSE.setHeader("Content-Disposition","""attachement; filename="%s" """%namePDF) + self.REQUEST.RESPONSE.setHeader("Content-Type","application/octet-stream") + len=os.stat(filename)[6] + self.REQUEST.RESPONSE.setHeader("Content-Length",len) + images=file(filename).read() + self.REQUEST.RESPONSE.write(images) + self.REQUEST.RESPONSE.close() + + def getRDF(self,urn=None): """rdf""" ret=getRDFDescription(self,self.link,urn=urn) @@ -636,8 +728,11 @@ class ECHO_resource(Folder,Persistent,EC else: return "no fulltext available" - - def getImageView(self,noredirect=None): + def getImagePath(self): + """gibt pfad zum image aus""" + return self.getImageView(noredirect="yes",onlyPath="yes") + + def getImageView(self,noredirect=None,onlyPath=None): """getImages; give Imageviewr and if somthing goes wrong.""" try: fh=ECHO_helpers.urlopen(self.metalink) @@ -656,7 +751,10 @@ class ECHO_resource(Folder,Persistent,EC self.REQUEST.RESPONSE.setHeader('Content-Type','text/xml') self.REQUEST.RESPONSE.redirect(imageurl) else: - return imageurl + if not onlyPath: + return imageurl + else: + return archivepath+"/"+imagetemp except: if not noredirect: @@ -898,9 +996,13 @@ class ECHO_resource(Folder,Persistent,EC RESPONSE.redirect('manage_main') - def getMDValue(self,fieldName): - return self.metaDataHash.get(fieldName,'!!NOT USED HERE in Type: %s'%self.contentType) - + def getMDValue(self,fieldName,empty=None): + if empty: + return self.metaDataHash.get(fieldName,'!!NOT USED HERE in Type: %s'%self.contentType) + + else: + return self.metaDataHash.get(fieldName,empty) + def newMetaXML(self,project=None,startpage=None,xslt=None,thumbtemplate=None,topbar=None,digiLibTemplate=None,digiliburlprefix=None): """new index.meta""" self.REQUEST.RESPONSE.setHeader('Content-Type','text/xml') @@ -1184,10 +1286,27 @@ class ECHO_resource(Folder,Persistent,EC def ECHO_getMD(self,item): """Ausgabe der MD""" return getattr(self,item) - + + def checkRDF(self,path): + """check if pdf in the path""" + try: + for fileName in os.listdir(path): + if os.path.splitext(fileName)[1]==".pdf": + return os.path.join(path,fileName) + return None + except: + return None + + def index_html(self): """standard page""" - + pdf=self.checkRDF(self.link) + if pdf: + fh=file(pdf,'r').read() + self.REQUEST.RESPONSE.setHeader('Content-Type','x-Application/pdf') + self.REQUEST.RESPONSE.write(fh) + self.REQUEST.RESPONSE.close() + return return self.REQUEST.RESPONSE.redirect(self.link) def startpage_html(self):