Integrate MuPDF Reader in an app
Asked Answered
M

6

46

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)?

Manatee answered 14/12, 2011 at 6:53 Comment(13)
Believe me bro if you are going to integrate it then get ready for atleast a hectic weekRodolforodolph
@Rodolforodolph :: oh really ? have u implemented that ?Manatee
yup not that only almost every PDF library I worked on that but you will not achieve what you want all are slow and no documentationRodolforodolph
@Rodolforodolph :: great that's seems interesting ..can you make me clear that whatever i want dose it feasible ? if something positive then wish me best luck :)Manatee
Wait recently I find a library that does all for you let me checkRodolforodolph
@Rodolforodolph ::ok looking forward for your reply .tnxManatee
@dhams are you allowed to use mupdf in a commercial application?Chatterton
@Chatterton No, its not fully let you use it for commercial purpose , you have to convince them by mailing detail to [email protected]Manatee
@Rodolforodolph "Wait recently I find a library that does all for you let me check" .... what's that library?!Ectogenous
@dhams Did you find a good solution?!Ectogenous
@Mr.Hyde Accepted answer already worked for meManatee
@dhams But i want to have curl/flip effect! Do you have any idea or sample code?!Ectogenous
i was searching for a library which would also allow me to select a word or basically lets me store a word in a variable from the pdf and let me perform some operations. Any help is really appreciatedJudsonjudus
S
49

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);
Strega answered 21/12, 2011 at 9:24 Comment(7)
Yep, I wanted to mention this but this is not what the question about )Strega
I am also doing same steps but i goe the following error after compiling rtifex_mupdf_MuPDFCore_gotoPageInternal': D:/android-ndk-r8/mupdf-1.0-source/android//jni/mupdf.c:102: error: 'fz_matrix' undeclared (first use in this function) D:/android-ndk-r8/mupdf-1.0-source/android//jni/mupdf.c:102: error: expected ';' D:/android-ndk-r8/mupdf-1.0-source/android//jni/mupdf.c:104: error: undeclared (first use in this function) D:/android-ndk-r8/mupdf-1.0-source/android//jni/mupdf.c:104: error: 'dev' undecl.. & many more.Can you Help me to solve this problem.Hux
Can you ask a separate question and put the whole stack of errors there?Strega
use mupdf in windows cigwin refer this https://mcmap.net/q/358824/-error-in-integration-of-mupdf-libraryBrushoff
The newer versions of mupdf does not have PixMapView classSokoto
i tried your solution. but getting this error. java.lang.UnsatisfiedLinkError: No implementation found for long com.archaio.airs.MuPDFCore.openFile(java.lang.String) (tried Java_com_archaio_airs_MuPDFCore_openFile and Java_com_archaio_airs_MuPDFCore_openFile__Ljava_lang_String_2)Dysprosium
My application is crash and get below error, 01-01 17:25:50.357: E/AndroidRuntime(14330): java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.artifex.mupdfdemo-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "libmupdf.so" How can i solve this issue?Pruritus
T
7

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 :)

Trustful answered 18/7, 2013 at 23:53 Comment(2)
Step 6 : cd android (within mupdf dir.) -bash: cd: android: No such file or directoryCasaba
@SazzadHossainKhan It's platform/android nowFricke
R
5

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)
Rodolforodolph answered 14/12, 2011 at 8:34 Comment(7)
i got compile time error when i was trying to generate the lib file using cygwin .. MyDirectory/EBookDroid/jni/djvu/JPEGDecoder.cpp:73:21: error: jconfig.h: No such file or directoryManatee
there must be some path issues search in the src folder if this file exist or or just download the apk and extract lib from that as simple as thatRodolforodolph
@Rodolforodolph so which is the fastest and better library among all pdf libraries?Junejuneau
have you tried this its the fastest but from this you have to extract the pdf module which is hectic its best one out there which is fastestRodolforodolph
I had tried this one but its having problem in zooming the pdf, Force Close with Bitmap exceeds VM.Junejuneau
Actually that depends upon the PDF if it contains too many hi-res images then it will go for my project it has to render mostly vector text and some images so it works fine with that and yes zooming has some bug you have to modify the java code for thatRodolforodolph
Since version 2 The EBookDroid become a closed source application. All 3d-party GPL code are deleted or moved to separate programs.Casaba
I
0

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?

Implausible answered 3/10, 2014 at 7:21 Comment(0)
F
0

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

Fourhanded answered 9/1, 2015 at 11:18 Comment(4)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes.Promontory
I have added more information in the answer, and given the link because I can't write all the steps here. Also it is polite to ask someone to change their comment instead of directly deleting it.Fourhanded
You've attempted to answer a 3-year old question with multiple (upvoted) answers. Moreover you're somewhat adding information to another answer. This is perhaps better-suited as a comment rather than a full-fledged answer. That's my opinion... other's may differ and that's also fine.Promontory
It might be 3 year old, but it was still a problem for me despite the upvoted answers. And none of these answers helped me. That's why I thought to add it as a separate answer.Fourhanded

© 2022 - 2024 — McMap. All rights reserved.