OpenPDF
OpenPDF copied to clipboard
Android support
Android support wiki article: https://github.com/LibrePDF/OpenPDF/wiki/Android-support
So let's list the tasks required for Android support:
-Classes like awt.Color, Point, Dimensiom can be easily replaced. -Classes like PdfGraphics2D need to be rewritten from scratch, since they use so much awt logic. -All the rendering logic is done with awt and needs to be rewritten, retested etc.
There is also: https://github.com/witwall/appengine-awt https://github.com/bedatadriven/appengine-export
- Could we use this?
The original issue submission here:
Hi, thank you for your great work.
Just learnt how to use your library with a sample Java application Then I used some classes I made thanks to your library in an Android environment but disocovered your library is using only AWT without no android.graphics packages suppport.
Any suggestion on how could I move on?
hello,
I am also interested in android compatibility. I want to modify this library, to replace awt with android specific code.
-
I can work on this repo on another branch; create openpdf-awt, openpdf-android modules, in openpdf module we would have abstract classes without graphics logic (ie AbstractImage instead of Image). In openpdf-awt module I would pull awt logic from the base module (ie Image implementation with awt). In openpdf-android module I would develop android specific graphics code (ie Image implementation based on android)
-
(The quick way) I can fork this repo and keep only a small part of the classes / components, only those that I actually need in my project. I can make those classes android compatible. I currently only need paragraphs, images and tables. In time, I will migrate more and more components. Then I can apply option (1).
@andob Well yeah that would be great, I thought to do it myself after asking for compatibility but I later realized I have already too much work to do at college.
However I resolved this missing compatibility by bypassing it : deployed a Servlet on AWS' Elastic Beanstalk using Apache Tomcat and then getting via HTTP response the processed PDF in the Android app
Yeah that's quite a trip, so I hope there'll be android support soon
@andob @Daniele-Comi @andreasrosdal
AFAIU there's no specific need to use AWT in the library - there is just a need to specify a font color, so developers have decided to use AWT Color
. So I guess it's just should be replaced with custom way to specify color to avoid pulling whole AWT JDK module which is completely redundant.
Hello @andob @Daniele-Comi @andreasrosdal Any news on Android support?
I don't have the skill to have this support Android, so hoping someone takes this one soon.
@BobbyRuby hello,
Sorry to dissapoint, but porting to android is a quite difficult task and needs some time. You can find awt references in many places. Classes like awt.Color, Point, Dimensiom can be easily replaced. But classes like PdfGraphics2D need to be rewritten from scratch, since they use so much awt logic. All the rendering logic is done with awt and needs to be rewritten, retested etc.
Coming from a commercial project, we didn't had time for this (and I still don't have enough time), so we explored other options:
- we could use iText 7. It is free if your project is open source, but kind of expensive for Android-based commercial projects
- (this is the option we applied in production) we needed to generate a document when the app was offline. We just used plain html for that. The generated document is created / viewed in html format, not pdf.
So let's list the tasks required for Android support:
-Classes like awt.Color, Point, Dimensiom can be easily replaced. -Classes like PdfGraphics2D need to be rewritten from scratch, since they use so much awt logic. -All the rendering logic is done with awt and needs to be rewritten, retested etc.
There is also: https://github.com/witwall/appengine-awt - Could we use this?
@andreasrosdal @BobbyRuby
Well, we could use windwardadmin/android-awt
which is based on appengine-awt.
I forked it into this repo - andob/android-awt
and converted it to gradle. I tested with my old PDF generating code based on iText, I converted that code to OpenPDF and applied this dependency.
It worked! :)
This is a hack but it seems to work :D. At least with the features I used: generating PDFs with paragraphs, tables, images and normal / bold text with the default font.
@BobbyRuby can you test this on your project and post the issues you found on my repo?
Can you please post a description about how to use OpenPDF with android-awt? android-awt needs maven support also. Thanks @andob !
@andreasrosdal Yes. My repo contains java.awt.* and javax.imageio.* classes. You just have to import both libraries (OpenPDF and the one from my repo). When OpenPDF will try to access awt classes, the JVM will find these classes on android-awt library.
On Android we use Gradle, which is fully compatible with maven.
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
implementation 'com.github.librepdf:openpdf:1.3.8'
implementation 'com.github.andob:android-awt:1.0.0'
}
On Maven this can be configured as
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependency>
<groupId>com.github.andob</groupId>
<artifactId>android-awt</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>com.github.librepdf</groupId>
<artifactId>openpdf</artifactId>
<version>1.3.8</version>
</dependency>
Excellent! Can you please document this on the OpenPDF github wiki?
@andreasrosdal done
I'll give this a shot... Hoping it will do the trick for filling forms. Won't be tonight but will try to get it tested before end of next week.
What do I need to add to my ProGuard setup to make this work? While the code works fine -many thanks to all involved!- in the debug build, the release build fails at the transformClassesAndResourcesWithProguardForRelease step, with the last message being
Warning: there were 143 instances of library classes depending on program classes. You must avoid such dependencies, since the program classes will be processed, while the library classes will remain unchanged. (http://proguard.sourceforge.net/manual/troubleshooting.html#dependency)
But I'm not sure how to avoid those?
@udittmer Please update android-awt
to its latest version, 1.0.1
implementation 'com.github.andob:android-awt:1.0.1'
And add these rules to your proguard-rules.pro
file
-dontwarn org.bouncycastle.**
-dontwarn java.lang.invoke.**
Thanks for the reply, but adding that didn't make a difference. I'm not sure why it would, anyway: suppressing warnings in ProGuard doesn't address the issue it complains about (about library classes depending on program classes), does it?
Is there a demo app (or any app that uses android-awt) available somewhere that shows how it all fits together?
@udittmer well I couldn't replicate your exact error
If I import OpenPDF
and android-awt:1.0.0
and then build with proguard I got the following errors:
There are some missing classes from java.awt.*
, javax.*
packages. I've added those classes in the latest version, 1.0.1.
If you want to use bouncycastle with OpenPDF, you sould import it in your build.gradle file, otherwise If you don't use bouncycastle features, you should add -dontwarn org.bouncycastle.**
in proguard rules, to ignore the warning.
OpenPDF also uses java.lang.invoke.LambdaMetafactory
, which is unavailable on Android. So, yes, supressing warnings don't address your exact issue, but the issues I've encountered when building with proguard.
I made a quick sample project: OpenPDFAndroidSample. Can you clone my repo and run / build for debug / release the sample? On my environment (Android Studio 3.5) works fine.
Thanks very much for the help, I appreciate it. I can build the sample code fine, and after upgrading my tool chain to the latest Gradle version (I was still using 4.10.2), so does my app. Maybe something internal to Gradle changed along the way. Anyway, the problem is solved.
I just saw the samples and was going to share it here! Thanks andob! I will be putting this to good use I think. I'll report back any problems or issues I may find!
# Please add these rules to your existing keep rules in order to suppress warnings.
# This is generated automatically by the Android Gradle plugin.
-dontwarn org.apache.fop.complexscripts.fonts.GlyphSubstitutionTable
-dontwarn org.apache.fop.complexscripts.util.CharScript
-dontwarn org.apache.fop.complexscripts.util.GlyphSequence
-dontwarn org.apache.fop.fonts.apps.TTFReader
-dontwarn org.apache.fop.fonts.truetype.FontFileReader
-dontwarn org.apache.fop.fonts.truetype.TTFFile
-dontwarn org.bouncycastle.jsse.BCSSLParameters
-dontwarn org.bouncycastle.jsse.BCSSLSocket
-dontwarn org.bouncycastle.jsse.provider.BouncyCastleJsseProvider
-dontwarn org.conscrypt.Conscrypt$Version
-dontwarn org.conscrypt.Conscrypt
-dontwarn org.openjsse.javax.net.ssl.SSLParameters
-dontwarn org.openjsse.javax.net.ssl.SSLSocket
-dontwarn org.openjsse.net.ssl.OpenJSSE
There is no official android support in OpenPDF, so using android-awt
is the way to go. Moving this to discussions.