Android-PdfMyXml


Source link: https://github.com/HendrixString/Android-PdfMyXml

Android-PdfMyXml

convert your android XML layouts into PDF document, works on all versions of Android.

Dependencies

How to use

Option 1: Simply fork or download the project, you can also download and create .aar file yourself.

Option 2: Jitpack

Add Jitpack in your root build.gradle at the end of repositories:

allprojects {

  repositories {

...

maven {
 url "https://jitpack.io" 
}

  
}
 
}

Add to your dependencies:

dependencies {

  compile 'com.github.HendrixString:Android-PdfMyXml:{
Tag
}
' // the latest version is "v1.0.1" 
}

Notable features

  • should work on all Android versions
  • completely scalable
  • supports bitmap re usage.
  • production proved code. Used in a commercial project.

Instructions

1. create XML layouts

First create XML layouts. give it dimensions in pixels (and for all it's sub views) and proportions according landscape or portrait according to ratio 1:1.41.

page1.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

  android:layout_width="2115px"

  android:layout_height="1500px"

  android:background="@color/white">
<TextView android:id="@+id/tv_hello"

  android:textColor="@color/black"

  android:textSize="27px"

  android:textStyle="bold"

  android:padding="6px"/>  </RelativeLayout>

you can create as many as pages/templates as you need.

2. Implement a View renderer

implement your View renderer by extending AbstractViewRenderer or by anonymously instantiating it and injecting the layout id. the initView(View view) will supply you an inflated View automatically. There are other options but I wont cover it now.

AbstractViewRenderer page = new AbstractViewRenderer(context, R.layout.page1) {

  private String _text;

public void setText(String text) {

_text = text;
  
}

@Override
  protected void initView(View view) {

TextView tv_hello = (TextView)view.findViewById(R.id.tv_hello);

 tv_hello.setText(_text);

  
}
 
}
;  // you can reuse the bitmap if you want page.setReuseBitmap(true);
 

3. Build the PDF document

Use PdfDocument or PdfDocument.Builder to add pages and render and run it all at background with progress bar.

PdfDocument doc

= new PdfDocument(ctx);
  // add as many pages as you have doc.addPage(page);
  doc.setRenderWidth(2115);
 doc.setRenderHeight(1500);
 doc.setOrientation(PdfDocument.A4_MODE.LANDSCAPE);
 doc.setProgressTitle(R.string.gen_please_wait);
 doc.setProgressMessage(R.string.gen_pdf_file);
 doc.setFileName("test");
 doc.setInflateOnMainThread(false);
 doc.setListener(new PdfDocument.Callback() {

  @Override
  public void onComplete(File file) {

Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");

  
}

@Override
  public void onError(Exception e) {

Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");

  
}
 
}
);
  doc.createPdf(ctx);
 

or use PdfDocument.Builder

new PdfDocument.Builder(ctx).addPage(page).filename("test").orientation(PdfDocument.A4_MODE.LANDSCAPE)

  .progressMessage(R.string.gen_pdf_file).progressTitle(R.string.gen_please_wait).renderWidth(2115).renderHeight(1500)

  .listener(new PdfDocument.Callback() {

@Override

public void onComplete(File file) {

 Log.i(PdfDocument.TAG_PDF_MY_XML, "Complete");

}

 @Override

public void onError(Exception e) {

 Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");

}

  
}
).create().createPdf(this);

Additional Contributors

License

If you like it -> star or share it with others

Copyright (C) 2016 Tomer Shalev (https://github.com/HendrixString)
 This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>. 

Contact Author

Resources

Give your collections (on any View at all) a fancier look by adding a cool way to check elements. If your list can multi-select elements, you can add this component as a "status indicator". This is basically a customizable view flipper, with a "front" and "rear" faces, and an "accept" image in the "rear" that will animate changes in its state, mostly like the GMail app.

Reservoir is a simple library for Android that allows you to easily serialize and cache your objects to disk using key/value pairs.

Simple and easy to use circular menu widget for Android.

Design pattern samples implemented in Java.

material-ripple allows to add ripple effect wrapper for Android views.

Simple Facebook SDK for Android which wraps original Facebook SDK.

This is a library project which makes the life much easier by coding less code for being able to login, publish feeds and open graph stories, invite friends and more.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes