1 | import uno |
---|
2 | import getopt, sys, os |
---|
3 | |
---|
4 | from unohelper import Base, systemPathToFileUrl, absolutize |
---|
5 | |
---|
6 | from com.sun.star.beans import PropertyValue |
---|
7 | from com.sun.star.script import CannotConvertException |
---|
8 | from com.sun.star.lang import IllegalArgumentException |
---|
9 | from com.sun.star.task import ErrorCodeIOException |
---|
10 | from com.sun.star.io import IOException, XOutputStream |
---|
11 | from com.sun.star.style.BreakType import PAGE_BEFORE, PAGE_AFTER |
---|
12 | from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK |
---|
13 | |
---|
14 | from xml.dom import minidom |
---|
15 | import sys |
---|
16 | |
---|
17 | keep_trace='' |
---|
18 | |
---|
19 | def addToText(cursor,text,level,value): |
---|
20 | if level==1: |
---|
21 | cursor.NumberingStyleName = "NONE" |
---|
22 | cursor.ParaStyleName="Heading 1" |
---|
23 | text.insertString( cursor, value , 0 ) |
---|
24 | text.insertControlCharacter( cursor, PARAGRAPH_BREAK , 0 ) |
---|
25 | #print >> sys.stderr,' * Main Title : ' + value |
---|
26 | else: |
---|
27 | i=0 |
---|
28 | prefix='' |
---|
29 | while i < level-1: |
---|
30 | prefix+=' ' |
---|
31 | i+=1 |
---|
32 | cursor.NumberingStyleName="List "+str(level-1) |
---|
33 | text.insertString( cursor, prefix+value , 0 ) |
---|
34 | text.insertControlCharacter( cursor, PARAGRAPH_BREAK , 0 ) |
---|
35 | cursor.NumberingStyleName = "NONE" |
---|
36 | #print >> sys.stderr,dir(sys.stderr) |
---|
37 | #print >> sys.stderr,prefix+' * NumberingStyleName '+str(level-1)+' '+value.encode('iso-8859-15') |
---|
38 | |
---|
39 | def printChildren(cursor,text,node,level,keep_trace): |
---|
40 | if node.nodeType==3: |
---|
41 | level-=1 |
---|
42 | |
---|
43 | if not(node.nodeValue!=None and len(node.nodeValue.replace(' ',''))!=1 and keep_trace!='' and keep_trace!=None): |
---|
44 | if keep_trace!='': |
---|
45 | addToText(cursor,text,level-1,keep_trace) |
---|
46 | keep_trace=node.nodeName |
---|
47 | |
---|
48 | if node.hasChildNodes(): |
---|
49 | for i in node.childNodes: |
---|
50 | printChildren(cursor,text,i,level+1,keep_trace) |
---|
51 | keep_trace='' |
---|
52 | else: |
---|
53 | if node.nodeValue != None and len(node.nodeValue.replace(' ',''))>1: |
---|
54 | addToText(cursor,text,level-1,keep_trace+' : '+node.nodeValue) |
---|
55 | keep_trace='' |
---|
56 | else: |
---|
57 | if node.nodeValue != None and len(node.nodeValue.replace(' ',''))>1: |
---|
58 | addToText(cursor,text,level-1,keep_trace+' : '+node.nodeValue) |
---|
59 | keep_trace='' |
---|
60 | else: |
---|
61 | if keep_trace!='#text': |
---|
62 | addToText(cursor,text,level-1,keep_trace) |
---|
63 | keep_trace='' |
---|
64 | |
---|
65 | if node.nodeType==1 and node.hasAttributes(): |
---|
66 | i=0 |
---|
67 | while i<node.attributes.length: |
---|
68 | addToText(cursor,text,level,'(attr) '+node.attributes.keys()[i] + ' : ' + node.attributes[node.attributes.keys()[i]].value) |
---|
69 | i+=1 |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | def Xml2Pdf(conf,input,output): |
---|
74 | localContext = uno.getComponentContext() |
---|
75 | resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext ) |
---|
76 | ctx = resolver.resolve( "uno:socket,host=127.0.0.1,port=3662;urp;StarOffice.ComponentContext" ) |
---|
77 | smgr = ctx.ServiceManager |
---|
78 | desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx) |
---|
79 | adressDoc=systemPathToFileUrl(input["doc"]["value"]) |
---|
80 | propFich=PropertyValue("Hidden", 0, True, 0), |
---|
81 | try: |
---|
82 | myDocument = desktop.loadComponentFromURL(adressDoc,"_blank",0,propFich) |
---|
83 | #Prefer to create a new document without any style ? |
---|
84 | #myDocument = desktop.loadComponentFromURL("private:factory/writer","_blank",0,propFich) |
---|
85 | except: |
---|
86 | conf["lenv"]["message"]='Unable to load input document' |
---|
87 | return 4 |
---|
88 | text = myDocument.Text |
---|
89 | cursor = text.createTextCursor() |
---|
90 | cursor.gotoStart(0) |
---|
91 | cursor.gotoEnd(1) |
---|
92 | xmldoc = minidom.parseString(input['xml']['value']) |
---|
93 | |
---|
94 | if xmldoc.hasChildNodes(): |
---|
95 | for i in xmldoc.childNodes: |
---|
96 | if i.nodeType==1: |
---|
97 | cursor.ParaStyleName="Title" |
---|
98 | text.insertString( cursor, i.nodeName , 0 ) |
---|
99 | text.insertControlCharacter( cursor, PARAGRAPH_BREAK , 0 ) |
---|
100 | #print >> sys.stderr,' * 1st level' + i.nodeName |
---|
101 | if i.hasChildNodes(): |
---|
102 | for j in i.childNodes: |
---|
103 | printChildren(cursor,text,j,2,'') |
---|
104 | |
---|
105 | tmp=myDocument.StyleFamilies.getByName("NumberingStyles") |
---|
106 | |
---|
107 | tmp1=tmp.getByName("Puce 1") |
---|
108 | |
---|
109 | prop1Fich = ( PropertyValue( "FilterName" , 0, "writer_pdf_Export", 0 ),PropertyValue( "Overwrite" , 0, True , 0 ) ) |
---|
110 | outputDoc=systemPathToFileUrl("/tmp/output.pdf") |
---|
111 | myDocument.storeToURL(outputDoc,prop1Fich) |
---|
112 | |
---|
113 | myDocument.close(True) |
---|
114 | ctx.ServiceManager |
---|
115 | output["Document"]["value"]= open('/tmp/output.pdf', 'r').read() |
---|
116 | print >> sys.stderr,len(output["Document"]["value"]) |
---|
117 | return 3 |
---|
118 | |
---|
119 | #To run test from command line uncomment the following line: |
---|
120 | #xml2pdf({},{"file":{"value":"/tmp/demo.xml"},"doc":{"value":"/tmp/demo.odt"}},{}) |
---|