2012-03-05 01:46:16 +00:00
|
|
|
require "unireader"
|
2012-03-19 23:10:19 +00:00
|
|
|
require "inputbox"
|
2011-12-07 23:45:39 +00:00
|
|
|
|
2012-03-19 17:59:36 +00:00
|
|
|
PDFReader = UniReader:new{}
|
2011-12-07 23:45:39 +00:00
|
|
|
|
|
|
|
-- open a PDF file and its settings store
|
2012-03-19 23:10:19 +00:00
|
|
|
function PDFReader:open(filename)
|
|
|
|
-- muPDF manages its own cache, set second parameter
|
|
|
|
-- to the maximum size you want it to grow
|
|
|
|
local ok
|
2012-03-31 12:09:50 +00:00
|
|
|
ok, self.doc = pcall(pdf.openDocument, filename, self.cache_document_size)
|
2012-03-19 23:10:19 +00:00
|
|
|
if not ok then
|
|
|
|
return false, self.doc -- will contain error message
|
|
|
|
end
|
|
|
|
if self.doc:needsPassword() then
|
2012-04-09 07:42:19 +00:00
|
|
|
local password = InputBox:input(G_height-100, 100, "Pass:")
|
2012-03-19 23:10:19 +00:00
|
|
|
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
|
|
|
|
return true
|
2011-12-07 23:45:39 +00:00
|
|
|
end
|
2012-04-11 20:52:48 +00:00
|
|
|
|
2012-04-12 03:23:22 +00:00
|
|
|
----------------------------------------------------
|
|
|
|
-- highlight support
|
|
|
|
----------------------------------------------------
|
2012-04-11 20:52:48 +00:00
|
|
|
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()
|
2012-04-13 21:54:06 +00:00
|
|
|
--print("## page:getPageText "..dump(text)) -- performance impact on device
|
2012-04-11 20:52:48 +00:00
|
|
|
page:close()
|
|
|
|
return text
|
|
|
|
end
|