forked from koreader/kindlepdfviewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfreader.lua
98 lines (91 loc) · 2.79 KB
/
pdfreader.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "unireader"
require "inputbox"
PDFReader = UniReader:new{
filename -- stores the absolute pathname of the open file
}
-- open a PDF file and its settings store
function PDFReader:open(filename)
-- muPDF manages its own cache, set second parameter
-- to the maximum size you want it to grow
local ok
ok, self.doc = pcall(pdf.openDocument, filename, self.cache_document_size)
if not ok then
return false, self.doc -- will contain error message
end
if self.doc:needsPassword() then
local password = InputBox:input(G_height-100, 100, "Pass:")
if not password or not self.doc:authenticatePassword(password) then
self.doc:close()
self.doc = nil
return false, _("wrong or missing password")
end
-- password wrong or not entered
end
local ok, err = pcall(self.doc.getPages, self.doc)
if not ok then
-- for PDFs, they might trigger errors later when accessing page tree
self.doc:close()
self.doc = nil
return false, _("damaged page tree")
end
self.filename = filename
return true
end
----------------------------------------------------
-- highlight support
----------------------------------------------------
function PDFReader:getText(pageno)
local ok, page = pcall(self.doc.openPage, self.doc, pageno)
if not ok then
-- TODO: error handling
return nil
end
local text = page:getPageText()
--Debug("## page:getPageText "..dump(text)) -- performance impact on device
page:close()
return text
end
function PDFReader:getPageLinks(pageno)
local ok, page = pcall(self.doc.openPage, self.doc, pageno)
if not ok then
-- TODO: error handling
return nil
end
local links = page:getPageLinks()
Debug("## page:getPageLinks ", links)
page:close()
return links
end
function PDFReader:init()
self:addAllCommands()
self:adjustCommands()
end
function PDFReader:adjustCommands()
self.commands:add(KEY_S, MOD_ALT, "S",
_("save all attachments on this page"),
function(self)
self:saveAttachments()
end)
end
-- saves all attachments on the current page in the same directory
-- as the file itself (see extr.c utility)
function PDFReader:saveAttachments()
InfoMessage:inform(_("Saving attachments..."), DINFO_NODELAY, 1, MSG_AUX)
local p = io.popen('./extr "'..self.filename..'" '..tostring(self.pageno), "r")
local count = p:read("*a")
p:close()
if count ~= "" then
-- double braces are needed because string.gsub() returns more than one value
count = tonumber((string.gsub(count, "[\n\r]+", "")))
if count == 0 then
InfoMessage:inform(_("No attachments found "), DINFO_DELAY, 1, MSG_WARN)
else
InfoMessage:inform(string.format(_("%d attachment(s) saved "), count),
DINFO_DELAY, 1, MSG_AUX)
end
else
InfoMessage:inform(_("Failed to save attachments "), DINFO_DELAY, 1, MSG_WARN)
end
-- needed because of inform(..DINFO_NODELAY..) above
self:redrawCurrentPage()
end