I have a custom python script using pdf2Image which I am trying to run on an android phone. I have tried these two ways
- Use an android app that can run python scripts on android.
- Create an android app and integrate my python code (using chaquopy).
But in both the cases, I am getting
W/System.err: com.chaquo.python.PyException: PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?
My scripts are running seamlessly on ubuntu terminal (using python <scriptFileName.py>
) but when I try to do it on android, it is giving me the above issue
My trouble is that I am not able to understand how can I install poppler, or add it to PATH in android.
Following is my script that converts the PDF into JPEG images (Module name = pdf2Img.py):
from pdf2image import convert_from_bytes
import sys
import os
import shutil
import io
def extractImagesFromPdf(fileContents):
BASE_DIR = 'in_pdfs'
EXTENSION = '.pdf'
OUT_DIR = 'images'
IMG_EXT = '.jpg'
sep = "/"
if not os.path.exists(getDirectoryPath(BASE_DIR)):
os.mkdir(getDirectoryPath(BASE_DIR))
pages = convert_from_bytes(fileContents)
if os.path.exists(getDirectoryPath(OUT_DIR)):
shutil.rmtree(getDirectoryPath(OUT_DIR))
os.mkdir(getDirectoryPath(OUT_DIR))
page_num = 1
for page in pages:
page.save(getDirectoryPath(OUT_DIR) + sep + str(page_num) + IMG_EXT, 'JPEG')
page_num = page_num + 1
def getDirectoryPath(dir):
base_path = os.path.dirname(__file__)
return base_path + "/" + dir
This is how I am invoking it
public class MainActivity extends AppCompatActivity {
private static final int PICK_PDF_FILE = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void selectAndUploadFile(View view) {
Intent intent= new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("application/pdf");
startActivityForResult(intent, PICK_PDF_FILE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
super.onActivityResult(requestCode, resultCode, resultData);
if (requestCode == PICK_PDF_FILE
&& resultCode == Activity.RESULT_OK) {
Uri inPDF = null;
if (resultData != null) {
inPDF = resultData.getData();
Log.i("SelectAndUpload", "onActivityResult: " + inPDF);
if (!Python.isStarted()) {
Python.start(new AndroidPlatform(MainActivity.this));
}
Python py = Python.getInstance();
PyObject module = py.getModule("pdf2Img");
try {
InputStream iStream = getContentResolver().openInputStream(inPDF);
byte[] inputData = getBytes(iStream);
PyObject result = module.callAttr("extractImagesFromPDF", inputData);
} catch (Exception e) {
Log.i("Ex", "Exception!!!");
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
}
public byte[] getBytes(InputStream inputStream) throws IOException {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, len);
}
return byteBuffer.toByteArray();
}
}
And the error is coming at line
pages = convert_from_path(BASE_DIR + sep + fileName + EXTENSION)
Few references: