-
Notifications
You must be signed in to change notification settings - Fork 13
/
pdfdiv.lua
77 lines (59 loc) · 2.67 KB
/
pdfdiv.lua
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
#!/usr/local/bin/lua
---------------------------------------------------------------------
--
-- making a visual diff of two pdfs
--
---------------------------------------------------------------------
function visual_div(old, new)
-- stripping file ending from name
doc_old = old:match("(.+).pdf") or old
doc_new = new:match("(.+).pdf") or new
-- getting number of pages in each document ---------------------
local handle = io.popen("pdfinfo " .. doc_old .. ".pdf | grep Pages | awk '{print $2}'")
local pages_old = handle:read("*a")
handle:close()
local handle = io.popen("pdfinfo " .. doc_new .. ".pdf | grep Pages | awk '{print $2}'")
local pages_new = handle:read("*a")
handle:close()
---- converting pdf's into png images ---------------------------
os.execute("convert -density 300 " .. doc_old .. ".pdf " .. doc_old .. ".png")
os.execute("convert -density 300 " .. doc_new .. ".pdf " .. doc_new .. ".png")
-- loop over pages ----------------------------------------------
for i = 0, pages_new-1, 1
do
-- check if page exists in old document ---------------------
if( pages_old+0.0 >= i)
then
-- suffix based on number of pages ----------------------
if( pages_old+0.0 == 1.0 )
then
suffix = ""
else
suffix = "-" .. i
end -- check if only one page
-- calculating diff -------------------------------------
os.execute("compare -compose src " .. doc_old .. suffix .. ".png " .. doc_new .. suffix .. ".png diff" .. suffix .. ".png")
-- getting number of changed pixels ---------------------
local handle = io.popen("convert diff" .. suffix .. ".png -print '%[fx:w*h*(1-mean)]' null:")
local diff_pixel = handle:read("*a")
handle:close()
-- clean up ---------------------------------------------
os.execute("rm " .. doc_new .. suffix .. ".png")
os.execute("rm " .. doc_old .. suffix .. ".png")
-- keeping only images with differences -----------------
if(diff_pixel+0.0 > 0.0)
then
-- print(diff_pixel)
else
os.execute("rm diff" .. suffix .. ".png")
end
end -- check if page exist
end -- loop over pages
end -- end function
---------------------------------------------------------------------
--
-- calling function
--
---------------------------------------------------------------------
-- e.g. lua pdfdiv.lua "document_old" "document_new"
visual_div(arg[1], arg[2])