-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf.py
88 lines (73 loc) · 2.44 KB
/
pdf.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
class Header:
def __init__(self, version: str):
self.version = version
class Body:
def __init__(self, objects: list):
self.objects = objects
class Xref:
def __init__(self, table: list):
self.table = table
class XrefSubsection:
def __init__(self, first_object: str, num_entries: str, entries: list):
self.first_object = first_object
self.num_entries = num_entries
self.entries = entries
class Trailer:
def __init__(self, entries: dict):
self.entries = entries
class PDF:
def __init__(self, header: Header, body: Body, xref: Xref, trailer: Trailer, filename: str):
self.header = header
self.body = body
self.xref = xref
self.trailer = trailer
self.filename = filename
def create(self):
b = 0
f = open(self.filename, 'ab')
f.write(bytes(self.header.version, 'utf-8') + b'\n')
b = b + len(bytes(self.header.version, 'utf-8'))
for x in self.body.objects:
f.write(bytes(x, 'utf-8') + b'\n')
b = b + len(bytes(x, 'utf-8'))
f.write(b'xref' + b'\n')
for x in self.xref.table:
f.write(bytes(x.first_object, 'utf-8') + b' ' + bytes(x.num_entries, 'utf-8') + b'\n')
for y in x.entries:
f.write(bytes(y, 'utf-8') + b'\n')
f.write(b'trailer' + b'\n')
f.write(b'<<')
for k in self.trailer.entries:
f.write(b'/' + bytes(k, 'utf-8') + b' ' + bytes(self.trailer.entries[k], 'utf-8'))
f.write(b'\n')
f.write(b'>>' + b'\n')
f.write(b'startxref' + b'\n')
f.write(bytes(str(b), 'utf-8') + b'\n')
f.write(b"%%EOF" + b'\n')
f.close
class PDFReader:
def __init__(self, name: str):
self.name = name
def read(self):
f = open(self.name, 'rb')
l = f.readlines()
for line in l:
print(line)
import os
try:
os.remove('test.pdf')
except Exception:
pass
header = Header('%PDF-1.1')
body = Body(['(hola)'])
xref = Xref([XrefSubsection('0', '1', ['0000000000 65535 f \r\n'])])
trailer = Trailer({'Size': '1', 'Root': '1 0 R'})
pdf = PDF(header, body, xref, trailer, 'test.pdf')
pdf.create()
reader = PDFReader('probe.pdf')
reader.read()
print('=====================================')
print('=====================================')
print('=====================================')
reader = PDFReader('test.pdf')
reader.read()