PluginInterface.require_command("pdftotext")
import commands

class PDFDocument(PluginInterface.File):

	contents = None
	
	PDFDocument_catalogable = [ "contents" ]

	def __init__(self,filename,mimetype,volume):
		PluginInterface.File.__init__(self,filename,mimetype,volume)
		# now we'll read the file contents
		command = "pdftotext -enc UTF-8 -raw %s - 2> /dev/null" % PluginInterface.shell_quote(filename)
		status, output = commands.getstatusoutput(command)
		if status == 0 :
			try: self.contents = PluginInterface.utf8_to_platform(output)
			except (UnicodeDecodeError, UnicodeEncodeError):
				PluginInterface.log("pdftotext-application-pdf").error("Error while converting %s to platform encoding",filename,exc_info=True)
		else:
			PluginInterface.log("pdftotext-application-pdf").warning("pdftotext failed on filename %s with status %d",filename, status)
		
	def __str__(self):
		return "PDF document %s"%(self.get_filename())

def get_factory():
	return PDFDocument

def get_mimetypes():
	return ["application/pdf"]
