-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvert-pdf.mjs
205 lines (187 loc) · 7.14 KB
/
invert-pdf.mjs
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
import { spawn } from "child_process"
import { fileURLToPath } from 'url'
import { dirname } from 'path'
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "fs"
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// const pdfName = `${__dirname}/VyacheslavEgorov_BenchmarkingJavaScript.pdf`
const pdfName = process.argv[2]
try {
rmSync(`${__dirname}/tempWork`,{recursive:true})
} catch (error) {
console.log(error)
}
mkdirSync(`${__dirname}/tempWork`,{recursive:true})
await new Promise(resolve=>{
const ok=spawn(`${__dirname}/qpdf`,[pdfName,"--decrypt",`${__dirname}/tempWork/decrypted.pdf`])
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",()=>{
resolve()
})
})
await new Promise(resolve=>{
const ok=spawn(`${__dirname}/pdftk`,[`${__dirname}/tempWork/decrypted.pdf`,"output",`${__dirname}/tempWork/uncompressed.pdf`,"uncompress"])
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",()=>{
resolve()
})
})
await new Promise(resolve=>{
const ok=spawn(`${__dirname}/pdfimages`,["-all",`${__dirname}/tempWork/uncompressed.pdf`,"sure"],{cwd:`${__dirname}/tempWork`})
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",()=>{
resolve()
})
})
const ok=spawn(`${__dirname}/pdfimages`,["-list",`${__dirname}/tempWork/uncompressed.pdf`])
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",async ()=>{
const lol=Buffer.concat(toConcat).toString().replace(/\r/g,"")
const keys = lol.slice(0,lol.indexOf("\n")).trim().replace(/\s+/g," ").split(" ")
const lol2=lol.slice(lol.indexOf("\n",lol.indexOf("\n")+1)+1).trim()
const arr = lol2.split("\n").map(v=>Object.fromEntries(v.trim().replace(/\s+/g," ").split(" ").map((v,i)=>[keys[i],v])))
const arr2 = Array.from((new Map(arr.map(v=>[v.object,v]))).values())
const ext={jpeg:"jpg",image:"png"}
const arr3 = arr2.map(v=>({basename:(()=>{
if (v.num===undefined) {
debugger
}
return `sure-${v.num.padStart(3,"0")}`
})(),ext:(()=>{
if (!ext[v.enc]) {
debugger
}
return ext[v.enc]
})(),object:v.object,width:v.width}))
await Promise.all(arr3.map(v=>new Promise(resolve=>{
const ok=spawn(`${__dirname}/magick`,[`${__dirname}/tempWork/${v.basename}.${v.ext}`,"-channel","RGB","-negate",`${__dirname}/tempWork/${v.basename}_negate.${v.ext}`])
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",()=>{
resolve()
})
})))
console.log(arr3)
let buf = readFileSync(`${__dirname}/tempWork/uncompressed.pdf`)
for (const v of arr3) {
const pos_object = buf.indexOf(`\n${v.object} 0 obj`)
const pos_stream = buf.indexOf("stream",pos_object) + 7
let paletteBuf
const imageBuf = readFileSync(`${__dirname}/tempWork/${v.basename}_negate.${v.ext}`)
const streamBuf = v.ext==="png" ? (()=>{
const buf = imageBuf
const toConcat = []
let i = 8
while (true) {
const Chunk_Type = buf.toString("utf8", i + 4, i + 8)
const Length = buf.readUint32BE(i)
if (Chunk_Type === "IDAT") {
toConcat.push(buf.subarray(i + 8,i + 8 + Length))
} else if (Chunk_Type === "IEND") {
return Buffer.concat(toConcat)
} else if (Chunk_Type === "PLTE") {
paletteBuf=buf.subarray(i + 8,i + 8 + Length)
}
// console.log(Length, Chunk_Type)
i += Length + 12
}
})() : imageBuf
let paletteLength, paletteStr
if (paletteBuf) {
paletteLength = Math.trunc(paletteBuf.length / 3)
paletteStr = `<${paletteBuf.toString("hex").toUpperCase()}>`
}
const new_meta = (lines=>{
const new_lines=[]
for (const line of lines) {
if (line.indexOf("/Length") > -1) {
new_lines.push(`/Length ${streamBuf.length}`)
if (v.ext==="png") {
new_lines.push("/Filter /FlateDecode")
new_lines.push("/DecodeParms <<")
new_lines.push(" /BitsPerComponent 8")
new_lines.push(" /Predictor 15")
new_lines.push(` /Columns ${v.width}`)
new_lines.push(` /Colors ${paletteBuf ? 1 : 3}`)
new_lines.push(">>")
new_lines.push(`/ColorSpace ${paletteBuf ? `[ /Indexed /DeviceRGB ${paletteLength-1} ${paletteStr} ]` : "/DeviceRGB"}`)
}
continue
}
if (line.indexOf("/Filter") > -1 && v.ext==="png") {
continue
}
if (line.indexOf("/ColorSpace") > -1 && v.ext==="png") {
continue
}
new_lines.push(line)
}
return new_lines.join("\n")
})(buf.toString("utf8",pos_object,pos_stream).split("\n"))
const pos_endstream = buf.indexOf("endstream",pos_stream) - 1
buf = Buffer.concat([buf.subarray(0,pos_object),Buffer.from(new_meta),streamBuf,buf.subarray(pos_endstream)])
}
writeFileSync(`${__dirname}/tempWork/transformed.pdf`,buf)
await new Promise(resolve=>{
const ok=spawn(`${__dirname}/gswin64c`,["-o",`${__dirname}/tempWork/fixed.pdf`,"-sDEVICE=pdfwrite","-dPDFSETTINGS=/prepress",`${__dirname}/tempWork/transformed.pdf`])
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",()=>{
resolve()
})
})
await new Promise(resolve=>{
const ok=spawn(`${__dirname}/pdftk`,[`${__dirname}/tempWork/fixed.pdf`,"output",`${__dirname}/tempWork/compressed.pdf`,"compress"])
const toConcat=[]
ok.stdout.on("data",chunk=>{
toConcat.push(chunk)
console.log(chunk.toString())
})
ok.stderr.on("data",chunk=>{
console.log(chunk.toString())
})
ok.stdout.on("end",()=>{
resolve()
})
})
})