android

pdf

render

android-ndk

mupdf

I am working on some stuff that should be able to read PDF in my app and I want to put PDF view in my custom layout. I had preferred Android PDF Viewer but when I performed zoomIn, zoomOut it takes too much time.

So currently I am supposed to use MuPDF open source project to integrate in my project, it's based on JNI and I am not used to it.

I am using Cygwin to build the library for native code. Hence I am unclear with few things:

  1. how to integrate the MuPDF in my project (as per my question title)?

  2. once I will succeed to integrated it then how can I put PDF reader in my custom view (in the XML or programmaticaly)?

Solution 1

I don't know how to do this in Windows using cygwin, because I'm using Ubuntu for the development. But I think the procedure should be the same.

  1. Download the file mupdf-0.9-source.tar.gz here: http://code.google.com/p/mupdf/downloads/list?q=source
  2. Download the file mupdf-thirdparty.zip
  3. Extract the sources. By default they will be extracted to the folder: mupdf-0.9/
  4. Extract the file mupdf-thirdparty.zip into the folder mupdf-0.9/
  5. Build the project mupdf-0.9 (For windows you should use VS as it is declared in the readme files)
  6. Then go to the folder mupdf-0.9/android/
  7. Run ndk-build
  8. You can get the following errors:

Compile thumb : mupdfthirdparty <= jbig2.c In file included from /home/yury/programming/android/workspace/mupdf-0.9/android/jni/../../thirdparty/jbig2dec/os_types.h:53, from /home/yury/programming/android/workspace/mupdf-0.9/android/jni/../../thirdparty/jbig2dec/jbig2.c:22: /home/yury/software/android-ndk-r6b/platforms/android-8/arch-arm/usr/include/stdint.h:48: error: redefinition of typedef 'int8_t' /home/yury/programming/android/workspace/mupdf-0.9/android/jni/../../thirdparty/jbig2dec/os_types.h:47: note: previous declaration of 'int8_t' was here

The solution is explained here: mupdf for android: ndk-build problem (error: redefinition of typedef....) However, you can simply comment the lines of the definition of types in the file /thirdparty/jbig2dec/os_types.h

After that you will receive two libraries: one static and one shared for your android application.

StaticLibrary  : libmupdfthirdparty.a
SharedLibrary  : libmupdf.so

This was the answer on the first question. Also there is a great step-by-step guide in the android/Readme.txt file.

Now the answer on the second question. In the application for android you can find test project. There are 3 files:

  • MuPDFActivity.java
  • MuPDFCore.java
  • PixmapView.java

Simply copy the last two files in your project. And see an example in MuPDFActivity.java how you can embed mupdf layout in your activity. In this file it is done like:

    PixmapView pixmapView;
//...   
layout = new RelativeLayout(this);
//...
    RelativeLayout.LayoutParams pixmapParams =
                      new RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.FILL_PARENT,
                                RelativeLayout.LayoutParams.FILL_PARENT);
    pixmapParams.addRule(RelativeLayout.ABOVE,100);
    layout.addView(pixmapView, pixmapParams);

    setContentView(layout);

Solution 3

This is How I achieve on my mac (2012, intel i5):

Step 1 : Get mupdf-1.2-source.zip

Step 2 : Get android-ndk-mac-64

Step 3 : unzip both of them in new folder call Android-pdf and rename unzip folder to mupdf and android-ndk (you can call them whatever you like)

Step 4 : open Terminal and use command : cd until you are in android-pdf folder

Step 5 : cd mupdf than command: make (will take about 40 sec. to run all scripts)

Step 6 : cd android (within mupdf dir.)

Step 7 : open finder go to folder Android-pdf that you created than android-ndk drag file call ndk-build into terminal command line and enter (basically adding a path to ndk-build to operate on mupdf lib.)

and after few sec. you should have new folder inside mupdf > android > libs > ... use that in your android project.

How to use MuPDF with your EXISTING Eclipse project:

  1. Copy the 'jni' folder from the /android folder into your existing Eclipse project.
  2. Copy the /thirdparty folder into the 'jni' folder in your project.
  3. Copy the /cbz folder into the 'jni' folder in your project.
  4. Copy the /draw folder into the 'jni' folder in your project.
  5. Copy the /fitz folder into the 'jni' folder in your project.
  6. Copy the /generated folder into the 'jni' folder in your project.
  7. Copy the /pdf folder into the 'jni' folder in your project.
  8. Copy the /scripts folder into the 'jni' folder in your project.
  9. Copy the /xps folder into the 'jni' folder in your project.
  10. Open 'Android.mk' inside the 'jni' folder.
  11. Change

    MUPDF_ROOT := ..

to

MUPDF_ROOT := $(TOP_LOCAL_PATH)
  1. Save 'Android.mk'.
  2. Open 'Core.mk' inside the 'jni' folder.
  3. Change

    MY_ROOT := ../..

to

MY_ROOT := $(LOCAL_PATH)
  1. Change all the

    ..

in LOCAL_C_INCLUDES to

$(LOCAL_PATH)
  1. Save 'Core.mk'.
  2. Open 'ThirdParty.mk' inside the 'jni' folder.
  3. Change

    MY_ROOT := ../..

to

MY_ROOT := $(LOCAL_PATH)
  1. Change all the

    ..

in LOCAL_C_INCLUDES to

$(LOCAL_PATH)
  1. Save 'ThirdParty.mk'.
  2. Now execute 'ndk-build' in your project's 'jni' directory.
  3. Copy everything in the /android/src folder into the 'src' folder in your project.
  4. Copy everything in the /android/res/drawable folder into the 'res/drawable' folder in your project.
  5. Copy everything in the /android/res/drawable-ldpi folder into the 'res/drawable-ldpi' folder in your project.
  6. Copy everything in the /android/res/drawable-mdpi folder into the 'res/drawable-mdpi' folder in your project.
  7. Copy everything in the /android/res/layout folder EXCEPT main.xml (because if you are copying into an existing project then you should already have your own main.xml or equivalent) into the 'res/layout' folder in your project.
  8. Copy everything in the /android/res/values folder into the 'res/values' folder in your project. If you already have a 'strings.xml' in your existing project, copy everything in between the '' tags in your /android/res/values/strings.xml into your project's strings.xml (paste between the '' tags). Similarly with the 'colors.xml', if you already have a 'colors.xml' in your existing project, copy everything in between the '' tags in your /android/res/values/strings.xml into your project's strings.xml (paste between the '' tags).
  9. Open the 'AndroidManifest.xml' in project.
  10. In between the '' tags paste all the list of activities from

    AndroidManifest.xml inside the /android folder, You should copy from your working copy of AndroidManifest.xml inside mupdf.

  11. MuPDF in now in your existing Eclipse project. To use it, call up com.artifex.mupdf.ChoosePDFActivity.class in your application. This is the main class for MuPDF.

  12. To open pdf with pre-fix file:

    Uri uri = Uri.parse("path to pdf file");

    Intent intent = new Intent(context, MuPDFActivity.class);

    intent.setAction(Intent.ACTION_VIEW);

    intent.setData(uri);

    context.startActivity(intent);

Hope this help :)

Solution 4

Check this project eBookDroid

EBookDroid is an open source (GPL'ed) document viewer for Android based on the VuDroid code base.

Supported the file in following formats:

PDF
DjVu
XPS (OpenXPS)
comics books (cbz,cbr)

Solution 5

I have view pager in my application. I need to show pdf files on each fragment in view pager.is there way to get a pdf view from mupdf ? normally we call mupdf as below

Uri uri = Uri.parse("path to pdf file");

Intent intent = new Intent(context, MuPDFActivity.class);

intent.setAction(Intent.ACTION_VIEW);

intent.setData(uri);

context.startActivity(intent);

So if i use mupdf for my app i have to call MuPDFActivity on each fragment.I think its not a correct way?

Solution 6

The answer given by star18bit pretty much sums it up, but even folllowing that I faced a lot of issues.

Like the make command didn't work for me and I had to build it using Visual Studio. So I listed all the pre-req softwares. Like ANT. Also we need to add the sdk folder's tools and platform-tools folders in PATH.

Also, I had to do the changes in Core2.mk file, the same ones in Core.mk. I can't give all the information here, cause it is a lot, so I referring this link for complete information:

http://howtojava.net/tutorials/android/integrate-mupdf-with-android-in-windows-7