forked from invoice-x/factur-x-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfwriter.py
501 lines (460 loc) · 20.4 KB
/
pdfwriter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import hashlib
import io
import mimetypes
from datetime import datetime
from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2.generic import DictionaryObject, DecodedStreamObject, \
NameObject, createStringObject, ArrayObject
from lxml import etree
from .logger import logger
# Python 2 and 3 compat
try:
file_types = (file, io.IOBase)
except NameError:
file_types = (io.IOBase,)
unicode = str
class FacturXPDFWriter(PdfFileWriter):
def __init__(self, facturx, pdf_metadata=None):
"""Take a FacturX instance and write the XML to the attached PDF file"""
super(FacturXPDFWriter, self).__init__()
# TODO: Can handle str/paths and ByteIO?
self.factx = facturx
original_pdf = PdfFileReader(facturx.pdf)
# Extract /OutputIntents obj from original invoice
output_intents = _get_original_output_intents(original_pdf)
self.appendPagesFromReader(original_pdf)
original_pdf_id = original_pdf.trailer.get('/ID')
logger.debug('original_pdf_id=%s', original_pdf_id)
if original_pdf_id:
self._ID = original_pdf_id
# else : generate some ?
if pdf_metadata is None:
base_info = {
'seller': self.factx['seller_name'],
'number': self.factx['invoice_number'],
'date': self.factx['date'],
'doc_type': self.factx['type'],
}
pdf_metadata = _base_info2pdf_metadata(base_info)
else:
# clean-up pdf_metadata dict
for key, value in pdf_metadata.iteritems():
if not isinstance(value, (str, unicode)):
pdf_metadata[key] = ''
self._update_metadata_add_attachment(pdf_metadata, output_intents)
def _update_metadata_add_attachment(self, pdf_metadata, output_intents):
'''This method is inspired from the code of the addAttachment()
method of the PyPDF2 lib'''
# The entry for the file
facturx_xml_str = self.factx.xml_str
md5sum = hashlib.md5().hexdigest()
md5sum_obj = createStringObject(md5sum)
params_dict = DictionaryObject({
NameObject('/CheckSum'): md5sum_obj,
NameObject('/ModDate'): createStringObject(_get_pdf_timestamp()),
NameObject('/Size'): NameObject(str(len(facturx_xml_str))),
})
file_entry = DecodedStreamObject()
file_entry.setData(facturx_xml_str) # here we integrate the file itself
file_entry.update({
NameObject("/Type"): NameObject("/EmbeddedFile"),
NameObject("/Params"): params_dict,
# 2F is '/' in hexadecimal
NameObject("/Subtype"): NameObject("/text#2Fxml"),
})
file_entry_obj = self._addObject(file_entry)
# The Filespec entry
ef_dict = DictionaryObject({
NameObject("/F"): file_entry_obj,
NameObject('/UF'): file_entry_obj,
})
xmp_filename = self.factx.flavor.details['xmp_filename']
fname_obj = createStringObject(xmp_filename)
filespec_dict = DictionaryObject({
NameObject("/AFRelationship"): NameObject("/Data"),
NameObject("/Desc"): createStringObject("Factur-X Invoice"),
NameObject("/Type"): NameObject("/Filespec"),
NameObject("/F"): fname_obj,
NameObject("/EF"): ef_dict,
NameObject("/UF"): fname_obj,
})
filespec_obj = self._addObject(filespec_dict)
name_arrayobj_cdict = {fname_obj: filespec_obj}
# TODO: add back additional attachments?
logger.debug('name_arrayobj_cdict=%s', name_arrayobj_cdict)
name_arrayobj_content_sort = list(
sorted(name_arrayobj_cdict.items(), key=lambda x: x[0]))
logger.debug('name_arrayobj_content_sort=%s', name_arrayobj_content_sort)
name_arrayobj_content_final = []
af_list = []
for (fname_obj, filespec_obj) in name_arrayobj_content_sort:
name_arrayobj_content_final += [fname_obj, filespec_obj]
af_list.append(filespec_obj)
embedded_files_names_dict = DictionaryObject({
NameObject("/Names"): ArrayObject(name_arrayobj_content_final),
})
# Then create the entry for the root, as it needs a
# reference to the Filespec
embedded_files_dict = DictionaryObject({
NameObject("/EmbeddedFiles"): embedded_files_names_dict,
})
res_output_intents = []
logger.debug('output_intents=%s', output_intents)
for output_intent_dict, dest_output_profile_dict in output_intents:
dest_output_profile_obj = self._addObject(
dest_output_profile_dict)
# TODO detect if there are no other objects in output_intent_dest_obj
# than /DestOutputProfile
output_intent_dict.update({
NameObject("/DestOutputProfile"): dest_output_profile_obj,
})
output_intent_obj = self._addObject(output_intent_dict)
res_output_intents.append(output_intent_obj)
# Update the root
xmp_level_str = self.factx.flavor.details['levels'][self.factx.flavor.level]['xmp_str']
xmp_template = self.factx.flavor.get_xmp_xml()
metadata_xml_str = _prepare_pdf_metadata_xml(xmp_level_str, xmp_filename, xmp_template, pdf_metadata)
metadata_file_entry = DecodedStreamObject()
metadata_file_entry.setData(metadata_xml_str)
metadata_file_entry.update({
NameObject('/Subtype'): NameObject('/XML'),
NameObject('/Type'): NameObject('/Metadata'),
})
metadata_obj = self._addObject(metadata_file_entry)
af_value_obj = self._addObject(ArrayObject(af_list))
self._root_object.update({
NameObject("/AF"): af_value_obj,
NameObject("/Metadata"): metadata_obj,
NameObject("/Names"): embedded_files_dict,
# show attachments when opening PDF
NameObject("/PageMode"): NameObject("/UseAttachments"),
})
logger.debug('res_output_intents=%s', res_output_intents)
if res_output_intents:
self._root_object.update({
NameObject("/OutputIntents"): ArrayObject(res_output_intents),
})
metadata_txt_dict = _prepare_pdf_metadata_txt(pdf_metadata)
self.addMetadata(metadata_txt_dict)
def _get_metadata_timestamp():
now_dt = datetime.now()
# example format : 2014-07-25T14:01:22+02:00
meta_date = now_dt.strftime('%Y-%m-%dT%H:%M:%S+00:00')
return meta_date
def _base_info2pdf_metadata(base_info):
if base_info['doc_type'] == '381':
doc_type_name = u'Refund'
else:
doc_type_name = u'Invoice'
date_str = datetime.strftime(base_info['date'], '%Y-%m-%d')
title = '%s: %s %s' % (
base_info['seller'], doc_type_name, base_info['number'])
subject = 'Factur-X %s %s dated %s issued by %s' % (
doc_type_name, base_info['number'], date_str, base_info['seller'])
pdf_metadata = {
'author': base_info['seller'],
'keywords': u'%s, Factur-X' % doc_type_name,
'title': title,
'subject': subject,
}
logger.debug('Converted base_info to pdf_metadata: %s', pdf_metadata)
return pdf_metadata
def _prepare_pdf_metadata_txt(pdf_metadata):
pdf_date = _get_pdf_timestamp()
info_dict = {
'/Author': pdf_metadata.get('author', ''),
'/CreationDate': pdf_date,
'/Creator':
u'factur-x Python lib',
'/Keywords': pdf_metadata.get('keywords', ''),
'/ModDate': pdf_date,
'/Subject': pdf_metadata.get('subject', ''),
'/Title': pdf_metadata.get('title', ''),
}
return info_dict
def _prepare_pdf_metadata_xml(xmp_level_str, xmp_filename, facturx_ext_schema_root, pdf_metadata):
nsmap_x = {'x': 'adobe:ns:meta/'}
nsmap_rdf = {'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'}
nsmap_dc = {'dc': 'http://purl.org/dc/elements/1.1/'}
nsmap_pdf = {'pdf': 'http://ns.adobe.com/pdf/1.3/'}
nsmap_xmp = {'xmp': 'http://ns.adobe.com/xap/1.0/'}
nsmap_pdfaid = {'pdfaid': 'http://www.aiim.org/pdfa/ns/id/'}
nsmap_fx = {
'fx': 'urn:factur-x:pdfa:CrossIndustryDocument:invoice:1p0#'}
ns_x = '{%s}' % nsmap_x['x']
ns_dc = '{%s}' % nsmap_dc['dc']
ns_rdf = '{%s}' % nsmap_rdf['rdf']
ns_pdf = '{%s}' % nsmap_pdf['pdf']
ns_xmp = '{%s}' % nsmap_xmp['xmp']
ns_pdfaid = '{%s}' % nsmap_pdfaid['pdfaid']
ns_fx = '{%s}' % nsmap_fx['fx']
ns_xml = '{http://www.w3.org/XML/1998/namespace}'
root = etree.Element(ns_x + 'xmpmeta', nsmap=nsmap_x)
rdf = etree.SubElement(
root, ns_rdf + 'RDF', nsmap=nsmap_rdf)
desc_pdfaid = etree.SubElement(
rdf, ns_rdf + 'Description', nsmap=nsmap_pdfaid)
desc_pdfaid.set(ns_rdf + 'about', '')
etree.SubElement(
desc_pdfaid, ns_pdfaid + 'part').text = '3'
etree.SubElement(
desc_pdfaid, ns_pdfaid + 'conformance').text = 'B'
desc_dc = etree.SubElement(
rdf, ns_rdf + 'Description', nsmap=nsmap_dc)
desc_dc.set(ns_rdf + 'about', '')
dc_title = etree.SubElement(desc_dc, ns_dc + 'title')
dc_title_alt = etree.SubElement(dc_title, ns_rdf + 'Alt')
dc_title_alt_li = etree.SubElement(
dc_title_alt, ns_rdf + 'li')
dc_title_alt_li.text = pdf_metadata.get('title', '')
dc_title_alt_li.set(ns_xml + 'lang', 'x-default')
dc_creator = etree.SubElement(desc_dc, ns_dc + 'creator')
dc_creator_seq = etree.SubElement(dc_creator, ns_rdf + 'Seq')
etree.SubElement(
dc_creator_seq, ns_rdf + 'li').text = pdf_metadata.get('author', '')
dc_desc = etree.SubElement(desc_dc, ns_dc + 'description')
dc_desc_alt = etree.SubElement(dc_desc, ns_rdf + 'Alt')
dc_desc_alt_li = etree.SubElement(
dc_desc_alt, ns_rdf + 'li')
dc_desc_alt_li.text = pdf_metadata.get('subject', '')
dc_desc_alt_li.set(ns_xml + 'lang', 'x-default')
desc_adobe = etree.SubElement(
rdf, ns_rdf + 'Description', nsmap=nsmap_pdf)
desc_adobe.set(ns_rdf + 'about', '')
producer = etree.SubElement(
desc_adobe, ns_pdf + 'Producer')
producer.text = 'PyPDF2'
desc_xmp = etree.SubElement(
rdf, ns_rdf + 'Description', nsmap=nsmap_xmp)
desc_xmp.set(ns_rdf + 'about', '')
creator = etree.SubElement(
desc_xmp, ns_xmp + 'CreatorTool')
creator.text = 'factur-x python lib'
timestamp = _get_metadata_timestamp()
etree.SubElement(desc_xmp, ns_xmp + 'CreateDate').text = timestamp
etree.SubElement(desc_xmp, ns_xmp + 'ModifyDate').text = timestamp
# The Factur-X extension schema must be embedded into each PDF document
facturx_ext_schema_desc_xpath = facturx_ext_schema_root.xpath(
'//rdf:Description', namespaces=nsmap_rdf)
rdf.append(facturx_ext_schema_desc_xpath[1])
# Now is the Factur-X description tag
facturx_desc = etree.SubElement(
rdf, ns_rdf + 'Description', nsmap=nsmap_fx)
facturx_desc.set(ns_rdf + 'about', '')
facturx_desc.set(
ns_fx + 'ConformanceLevel', xmp_level_str)
facturx_desc.set(ns_fx + 'DocumentFileName', xmp_filename)
facturx_desc.set(ns_fx + 'DocumentType', 'INVOICE')
facturx_desc.set(ns_fx + 'Version', '1.0')
# TODO: should be UTF-16be ??
xml_str = etree.tostring(
root, pretty_print=True, encoding="UTF-8", xml_declaration=False)
head = u'<?xpacket begin="\ufeff" id="W5M0MpCehiHzreSzNTczkc9d"?>'.encode(
'utf-8')
tail = u'<?xpacket end="w"?>'.encode('utf-8')
xml_final_str = head + xml_str + tail
logger.debug('metadata XML:')
# logger.debug(xml_final_str.decode())
return xml_final_str
# def createByteObject(string):
# string_to_encode = u'\ufeff' + string
# x = string_to_encode.encode('utf-16be')
# return ByteStringObject(x)
def _filespec_additional_attachments(
pdf_filestream, name_arrayobj_cdict, file_dict, file_bin):
filename = file_dict['filename']
logger.debug('_filespec_additional_attachments filename=%s', filename)
mod_date_pdf = _get_pdf_timestamp(file_dict['mod_date'])
md5sum = hashlib.md5(file_bin).hexdigest()
md5sum_obj = createStringObject(md5sum)
params_dict = DictionaryObject({
NameObject('/CheckSum'): md5sum_obj,
NameObject('/ModDate'): createStringObject(mod_date_pdf),
NameObject('/Size'): NameObject(str(len(file_bin))),
})
file_entry = DecodedStreamObject()
file_entry.setData(file_bin)
file_mimetype = mimetypes.guess_type(filename)[0]
if not file_mimetype:
file_mimetype = 'application/octet-stream'
file_mimetype_insert = '/' + file_mimetype.replace('/', '#2f')
file_entry.update({
NameObject("/Type"): NameObject("/EmbeddedFile"),
NameObject("/Params"): params_dict,
NameObject("/Subtype"): NameObject(file_mimetype_insert),
})
file_entry_obj = pdf_filestream._addObject(file_entry)
ef_dict = DictionaryObject({
NameObject("/F"): file_entry_obj,
})
fname_obj = createStringObject(filename)
filespec_dict = DictionaryObject({
NameObject("/AFRelationship"): NameObject("/Unspecified"),
NameObject("/Desc"): createStringObject(file_dict.get('desc', '')),
NameObject("/Type"): NameObject("/Filespec"),
NameObject("/F"): fname_obj,
NameObject("/EF"): ef_dict,
NameObject("/UF"): fname_obj,
})
filespec_obj = pdf_filestream._addObject(filespec_dict)
name_arrayobj_cdict[fname_obj] = filespec_obj
# moved to FacturXPDFWriter
def _facturx_update_metadata_add_attachment(
pdf_filestream, facturx_xml_str, pdf_metadata, facturx_level,
output_intents=[], additional_attachments={}):
'''This method is inspired from the code of the addAttachment()
method of the PyPDF2 lib'''
# The entry for the file
md5sum = hashlib.md5(facturx_xml_str).hexdigest()
md5sum_obj = createStringObject(md5sum)
params_dict = DictionaryObject({
NameObject('/CheckSum'): md5sum_obj,
NameObject('/ModDate'): createStringObject(_get_pdf_timestamp()),
NameObject('/Size'): NameObject(str(len(facturx_xml_str))),
})
file_entry = DecodedStreamObject()
file_entry.setData(facturx_xml_str) # here we integrate the file itself
file_entry.update({
NameObject("/Type"): NameObject("/EmbeddedFile"),
NameObject("/Params"): params_dict,
# 2F is '/' in hexadecimal
NameObject("/Subtype"): NameObject("/text#2Fxml"),
})
file_entry_obj = pdf_filestream._addObject(file_entry)
# The Filespec entry
ef_dict = DictionaryObject({
NameObject("/F"): file_entry_obj,
NameObject('/UF'): file_entry_obj,
})
fname_obj = createStringObject(FACTURX_FILENAME)
filespec_dict = DictionaryObject({
NameObject("/AFRelationship"): NameObject("/Data"),
NameObject("/Desc"): createStringObject("Factur-X Invoice"),
NameObject("/Type"): NameObject("/Filespec"),
NameObject("/F"): fname_obj,
NameObject("/EF"): ef_dict,
NameObject("/UF"): fname_obj,
})
filespec_obj = pdf_filestream._addObject(filespec_dict)
name_arrayobj_cdict = {fname_obj: filespec_obj}
for attach_bin, attach_dict in additional_attachments.items():
_filespec_additional_attachments(
pdf_filestream, name_arrayobj_cdict, attach_dict, attach_bin)
logger.debug('name_arrayobj_cdict=%s', name_arrayobj_cdict)
name_arrayobj_content_sort = list(
sorted(name_arrayobj_cdict.items(), key=lambda x: x[0]))
logger.debug('name_arrayobj_content_sort=%s', name_arrayobj_content_sort)
name_arrayobj_content_final = []
af_list = []
for (fname_obj, filespec_obj) in name_arrayobj_content_sort:
name_arrayobj_content_final += [fname_obj, filespec_obj]
af_list.append(filespec_obj)
embedded_files_names_dict = DictionaryObject({
NameObject("/Names"): ArrayObject(name_arrayobj_content_final),
})
# Then create the entry for the root, as it needs a
# reference to the Filespec
embedded_files_dict = DictionaryObject({
NameObject("/EmbeddedFiles"): embedded_files_names_dict,
})
res_output_intents = []
logger.debug('output_intents=%s', output_intents)
for output_intent_dict, dest_output_profile_dict in output_intents:
dest_output_profile_obj = pdf_filestream._addObject(
dest_output_profile_dict)
# TODO detect if there are no other objects in output_intent_dest_obj
# than /DestOutputProfile
output_intent_dict.update({
NameObject("/DestOutputProfile"): dest_output_profile_obj,
})
output_intent_obj = pdf_filestream._addObject(output_intent_dict)
res_output_intents.append(output_intent_obj)
# Update the root
metadata_xml_str = _prepare_pdf_metadata_xml(facturx_level, pdf_metadata)
metadata_file_entry = DecodedStreamObject()
metadata_file_entry.setData(metadata_xml_str)
metadata_file_entry.update({
NameObject('/Subtype'): NameObject('/XML'),
NameObject('/Type'): NameObject('/Metadata'),
})
metadata_obj = pdf_filestream._addObject(metadata_file_entry)
af_value_obj = pdf_filestream._addObject(ArrayObject(af_list))
pdf_filestream._root_object.update({
NameObject("/AF"): af_value_obj,
NameObject("/Metadata"): metadata_obj,
NameObject("/Names"): embedded_files_dict,
# show attachments when opening PDF
NameObject("/PageMode"): NameObject("/UseAttachments"),
})
logger.debug('res_output_intents=%s', res_output_intents)
if res_output_intents:
pdf_filestream._root_object.update({
NameObject("/OutputIntents"): ArrayObject(res_output_intents),
})
metadata_txt_dict = _prepare_pdf_metadata_txt(pdf_metadata)
pdf_filestream.addMetadata(metadata_txt_dict)
def _extract_base_info(facturx_xml_etree):
namespaces = facturx_xml_etree.nsmap
date_xpath = facturx_xml_etree.xpath(
'//rsm:ExchangedDocument/ram:IssueDateTime/udt:DateTimeString',
namespaces=namespaces)
date = date_xpath[0].text
date_dt = datetime.strptime(date, '%Y%m%d')
inv_num_xpath = facturx_xml_etree.xpath(
'//rsm:ExchangedDocument/ram:ID', namespaces=namespaces)
inv_num = inv_num_xpath[0].text
seller_xpath = facturx_xml_etree.xpath(
'//ram:ApplicableHeaderTradeAgreement/ram:SellerTradeParty/ram:Name',
namespaces=namespaces)
seller = seller_xpath[0].text
doc_type_xpath = facturx_xml_etree.xpath(
'//rsm:ExchangedDocument/ram:TypeCode', namespaces=namespaces)
doc_type = doc_type_xpath[0].text
base_info = {
'seller': seller,
'number': inv_num,
'date': date_dt,
'doc_type': doc_type,
}
logger.debug('Extraction of base_info: %s', base_info)
return base_info
def _base_info2pdf_metadata(base_info):
if base_info['doc_type'] == '381':
doc_type_name = u'Refund'
else:
doc_type_name = u'Invoice'
date_str = datetime.strftime(base_info['date'], '%Y-%m-%d')
title = '%s: %s %s' % (
base_info['seller'], doc_type_name, base_info['number'])
subject = 'Factur-X %s %s dated %s issued by %s' % (
doc_type_name, base_info['number'], date_str, base_info['seller'])
pdf_metadata = {
'author': base_info['seller'],
'keywords': u'%s, Factur-X' % doc_type_name,
'title': title,
'subject': subject,
}
logger.debug('Converted base_info to pdf_metadata: %s', pdf_metadata)
return pdf_metadata
def _get_original_output_intents(original_pdf):
output_intents = []
try:
pdf_root = original_pdf.trailer['/Root']
ori_output_intents = pdf_root['/OutputIntents']
logger.debug('output_intents_list=%s', ori_output_intents)
for ori_output_intent in ori_output_intents:
ori_output_intent_dict = ori_output_intent.getObject()
logger.debug('ori_output_intents_dict=%s', ori_output_intent_dict)
dest_output_profile_dict =\
ori_output_intent_dict['/DestOutputProfile'].getObject()
output_intents.append(
(ori_output_intent_dict, dest_output_profile_dict))
except:
pass
return output_intents
def _get_pdf_timestamp(date=None):
if date is None:
date = datetime.now()
# example date format: "D:20141006161354+02'00'"
pdf_date = date.strftime("D:%Y%m%d%H%M%S+00'00'")
return pdf_date