hendrixstring / android-pdfmyxml Goto Github PK
View Code? Open in Web Editor NEWConvert android XML layouts into PDF document, works on all versions of Android.
License: GNU General Public License v2.0
Convert android XML layouts into PDF document, works on all versions of Android.
License: GNU General Public License v2.0
this error occurs when generate pdf from view. I also tried runonuithread, new Handler(Looper.mainLooper) with runnable, but now work anything
Strangely is taking around 20 seconds to finish a simple small PDF with 7,5Kb size, but sometimes it tooks 1 second, what it might be?
`D/ViewRootImpl@79dead3[MeterActivity]: ViewPostIme pointer 0
D/ViewRootImpl@79dead3[MeterActivity]: ViewPostIme pointer 1
I/System.out: a
D/skia: Encode PNG Singlethread processname=com.uberdomarlon.rebu : 176023 us, width=750, height=1057
D/ScrollView: initGoToTop
D/ScrollView: initGoToTop
D/InputTransport: Input channel constructed: fd=133
D/ViewRootImpl@4d2bf8d[Gerando recibo]: setView = DecorView@826b542[Gerando recibo] TM=true MM=false
D/ViewRootImpl@4d2bf8d[Gerando recibo]: dispatchAttachedToWindow
V/Surface: sf_framedrop debug : 0x4f4c, game : false, logging : 0
D/ViewRootImpl@4d2bf8d[Gerando recibo]: Relayout returned: old=[0,0][0,0] new=[28,780][1052,1202] result=0x7 surface={valid=true 535776014336} changed=true
D/OpenGLRenderer: eglCreateWindowSurface = 0x7cd1dbe980
D/ViewRootImpl@4d2bf8d[Gerando recibo]: MSG_WINDOW_FOCUS_CHANGED 1
D/ViewRootImpl@4d2bf8d[Gerando recibo]: MSG_RESIZED_REPORT: frame=Rect(28, 780 - 1052, 1202) ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
D/ViewRootImpl@79dead3[MeterActivity]: MSG_WINDOW_FOCUS_CHANGED 0
and After 20 seconds...
I/zygote64: ThreadFlipBegin blocked for 45.662ms
I/PDF_MY_XML: add page
I/PDF_MY_XML: pdf 1
pdf 2
D/OpenGLRenderer: eglDestroySurface = 0x7cd1dbe980
D/ViewRootImpl@4d2bf8d[Gerando recibo]: dispatchDetachedFromWindow
D/InputEventReceiver: channel '4c94576 Gerando recibo (client)' ~ Disposing input event receiver.
channel '4c94576 Gerando recibo (client)' ~NativeInputEventReceiver.
D/InputTransport: Input channel destroyed: fd=133
I/PDF_MY_XML: Complete
I/PDF_MY_XML: pdf 3
D/ViewRootImpl@79dead3[MeterActivity]: MSG_WINDOW_FOCUS_CHANGED 1
E/ViewRootImpl: sendUserActionEvent() returned.
I/zygote64: Background concurrent copying GC freed 10538(585KB) AllocSpace objects, 2(4MB) LOS objects, 49% free, 19MB/39MB, paused 539us total 161.984ms`
Hello
I have an error in AbstractViewRenderer initView() on check1.setChecked(true).Also it does not let me use Picasso. Please help me.
android.util.AndroidRuntimeException: Animators may only be run on Looper threads
My Code:
public class SignOutActivity extends AppCompatActivity implements View.OnClickListener {
ImageView ivback;
Button btn_save_form;
TextView header_text;
EditText edt_name;
CheckBox check1;
private String _text = "Razia";
ImageView image1;
boolean checked = true;
String image_path;
AbstractViewRenderer page;
public Handler mHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_out);
header_text = (TextView) findViewById(R.id.header_text);
ivback = (ImageView) findViewById(R.id.ivback);
image1 = (ImageView) findViewById(R.id.image1);
btn_save_form = (Button) findViewById(R.id.btn_save_form);
check1 = (CheckBox) findViewById(R.id.check1);
edt_name = (EditText) findViewById(R.id.edt_name);
btn_save_form.setOnClickListener(this);
ivback.setOnClickListener(this);
image1.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ivback:
finish();
break;
case R.id.image1:
Intent intent = new Intent(this, SigningActivity.class);
startActivityForResult(intent, 100);
break;
case R.id.btn_save_form:
ivback.setVisibility(View.GONE);
btn_save_form.setVisibility(View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
makePdf();
}
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if (requestCode == 100) {
image_path = data.getStringExtra("path");
Picasso.with(getApplicationContext()).load(new File(image_path)).fit().into(image1);
}
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
void makePdf() {
page = new AbstractViewRenderer(SignOutActivity.this, R.layout.activity_sign_out) {
CheckBox check1;
@Override
protected void initView(View view) {
check1 = (CheckBox) view.findViewById(R.id.check1);
edt_name = (EditText) view.findViewById(R.id.edt_name);
header_text = (TextView) findViewById(R.id.header_text);
ivback = (ImageView) view.findViewById(R.id.ivback);
image1 = (ImageView) view.findViewById(R.id.image1);
btn_save_form = (Button) view.findViewById(R.id.btn_save_form);
ivback.setVisibility(View.GONE);
btn_save_form.setVisibility(View.GONE);
image1.setImageURI(Uri.parse(image_path));
edt_name.setText(_text);
// Picasso.with(SignOutActivity.this).load(new File(image_path)).fit().into(image1);
check1.setChecked(checked);
}
};
page.setReuseBitmap(true);
new PdfDocument.Builder(getApplicationContext()).addPage(page).filename("test12").orientation(PdfDocument.A4_MODE.PORTRAIT)
.progressMessage(R.string.app_name).progressTitle(R.string.app_name).renderWidth(1480).renderHeight(2508)
.listener(new PdfDocument.Callback() {
@Override
public void onComplete(File file) {
ivback.setVisibility(View.VISIBLE);
header_text.setGravity(Gravity.CENTER);
btn_save_form.setVisibility(View.VISIBLE);
Log.e(PdfDocument.TAG_PDF_MY_XML, "Complete " + file.getAbsolutePath());
}
@Override
public void onError(Exception e) {
ivback.setVisibility(View.VISIBLE);
header_text.setGravity(Gravity.LEFT);
btn_save_form.setVisibility(View.VISIBLE);
Log.e(PdfDocument.TAG_PDF_MY_XML, "Error");
}
}).create().createPdf(this);
}
}
Hi there.
I try to use your library Android-PdfMyXml, but i got eternal loader when createPdf was called.
I have no error or warning and i tryed use PdfDocument (and PdfDocument.Builder too)
Maybee you can give me advise some solution? I will wery grateful for your help.
My code:
` @OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*PDFPage pdfPage = new PDFPage(this);
pdfPage.setLogo(R.mipmap.ic_launcher);
pdfPage.setTitle(R.string.app_name);
pdfPage.setText(R.string.text_fish);
pdfPage.makePdf("file.pdf");*/
_title = getString(R.string.app_name);
_text = getString(R.string.text_fish);
_logo = R.mipmap.ic_launcher;
makePdf();
Toast.makeText(this, "PDF File created", Toast.LENGTH_SHORT);
}
public void makePdf() {
AbstractViewRenderer page = new AbstractViewRenderer(this, R.layout.page_pdf) {
@Override
protected void initView(View view) {
TextView title = (TextView) view.findViewById(R.id.tvTitle);
TextView message = (TextView) view.findViewById(R.id.tvMessage);
ImageView ivLogo = (ImageView) view.findViewById(R.id.ivLogo);
title.setText(_title);
message.setText(_text);
ivLogo.setImageResource(_logo);
}
};
page.setReuseBitmap(true);
new PdfDocument.Builder(this).addPage(page).filename("test")
.orientation(PdfDocument.A4_MODE.PORTRAIT)
.progressMessage(R.string.app_name)
.progressTitle(R.string.app_name)
.renderWidth(1480).renderHeight(2500)
.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);
/*PdfDocument doc = new PdfDocument(this);
doc.setContext(this);
doc.addPage(page);
doc.setRenderWidth(1480);
doc.setRenderHeight(2500);
doc.setOrientation(PdfDocument.A4_MODE.PORTRAIT);
doc.setFileName(filename);
doc.setInflateOnMainThread(false);
doc.setListener(new PdfDocument.Callback() {
@Override
public void onComplete(File file) {
Log.d(this.getClass().getSimpleName(), "Complete");
}
@Override
public void onError(Exception e) {
Log.d(this.getClass().getSimpleName(), "Error");
}
});
doc.createPdf(this);*/
}`
Hi, i'm traying to load a webview so i can print a web page but it gets me the following error
"A WebView method was called on thread ''. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads."
Any idea on how to implement the webview?
In RelativeLayout its some attributes are not properly rendered like centerInParent. And widgets are not properly placed in pdf as they were in android xml. In LinearLayout also, widgets are not placed properly.
how can i print a view with a recyclerview ?
This is a great library but I can't find a way to add margins and margins given to my parent view are not working.
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="200dp"
android:layout_marginLeft="30dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
tools:context="com.dgheating.mobileapp.Fragments.QuotationAddFragment">
Can you guide please?
Listview content is not getting printed in the pdf. please help me
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: Cannot merge new index 66201 into a non-jumbo instruction!
Any help?
I followed the README sample, but bold text isn't working. There are other manner to do this?
I started using this library three days ago and I noticed some parts in the library that I thought could be improved. And features that weren't really necessary. So I changed them and basically made our own version. Since this is an open source project I would like to discuss them and see if the changes would be candidates for merges into the library.
PdfDocument
has a Builder
, but it is possible to change it's fields after the creation. When a Builder
is provided I would assume that the instance itself is immutable (which I think is a good idea). That thus means that you create a PdfDocument
using the Builder
and then the only thing you can do using the PdfDocument
is adding new pages (which you then can't do in the builder). Was there any specific reason to do it differently? What do you think about changing this behaviour?
Builder
has a context(Context)
method, but wouldn't it be easier to provide the Context
via the constructor? You can't use the PdfDocument
without it.Callback
in the PdfDocument
does provide an onError
call but it does not give back the Exception that occurred, moreover: it does not give any feedback about what happened at all. Wouldn't it be better to pass all possible exceptions that can occur via the Callback
?createPdf(Context)
is called, using another Context
(probably also the one which was provided before). It can come in handy, but it is probably not this library's task to do this. Any developer can then show his/her own dialog or screen before calling createPdf
and hide it again after receiving a response using the listener. Plus: You'd rather want a DialogFragment since it handles the lifecycle of the Dialog and prevents Dialog leakage.The API provided can be used only if we have predefined pages, say I want to add two pages then I would do this:
new PdfDocument.Builder(context).addPage(page1).addPage(page2)
but what if I want to generate pages with code, for example, to print 3 pages of a quotation because the product list is dynamically selected by a user. Then I would need an api like this.
new PdfDocument.Builder(context).addPages(pagesList)
Can you please post a code sniffet on how to create a pdf with multiple.pages dynamically , i mean if someone has a scrollview than how he/she will be able to get the whole long content in the pdf devided in devided in multiple pages , Thanks
Hi,
i'm following the tutorial ("How to use") section but i'm struggle to display/add an image inside a layout (i.e., ImageView). Is that possible? can you point to the right direction?
<LinearLayout
<ImageView
android:id="@+id/iv_hello"
android:layout_width="297px"
android:layout_height="match_parent"
android:background="@drawable/error_rounded_corners"
android:src="@drawable/baselogo"
app:srcCompat="@drawable/baselogo"/>
AbstractViewRenderer page = new AbstractViewRenderer(ctx, R.layout.page1) {
@Override
protected void initView(View view) {
ImageView iv_hello = (ImageView) view.findViewById(R.id.iv_hello);
//iv_hello.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.baselogo));
iv_hello.setImageResource(R.drawable.baselogo);
}
};
Hello @HendrixString hendrix thank you for this library, looking forward on doing donations if things go as expected. I try to add to dependencies but it doesnt compile.
Hi, I noticed that if on Android Studio set Build Variant to DEBUG the process of creating the pdf is slow and does not work.
When I set Build Variant to RELEASE seems to work well
Hello , is this library under gpl v2.0 or gpl v3.0
What I'm wondering is , do I have to make my product opensource after using this library or not?
#how to remove some view from the layout?
I am using these lines in my project -
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:1.0.1
}
hey,
I got a problem when I create a PDF-file of a xml-layout. I´m using an ConstrainLayout with an ImageView in it, where the width is set on "match_parent" - see code below:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivbg_cbd_pic2"
android:layout_width="match_parent"
android:layout_height="138dp"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
app:layout_constraintBottom_toTopOf="@+id/et_color_cbd"
app:layout_constraintTop_toBottomOf="@+id/et_size_cbd"
app:srcCompat="@drawable/xxyyzz" />
</androidx.constraintlayout.widget.ConstraintLayout>
when I create a Pdf the layout in that Pdf-file is stretched in width that the image no longer reach the edge and also compresses the height of the layout. It only happens when i set the width of the imageView to "match_parent" - if I use a fixed value like "400dp" it doesn´t happen.
But I want to use "match_parent" to make sure the image is always shown from the left to the right edge. Even if the screen is a little wider.
I hope you can help me find a solution. Thanks in advance.
How i will be able to save the pdf in a specific location in my phone storage?
The issue im facing is that i have a ScrollView , whenever the pdf is generating only the visible part of the ScrollView is showing in the pdf and rest of the part is not showing. I want to generate another page in the pdf and show the rest part of my ScrollView on that page. Please help me asap :( stucked in this from last two days
I have followed the readme file directions and keep getting the same error:
Failed to resolve: com.github.HendrixString:Android-PdfMyXml:1.0.1
I have tried changing the version number to 1.0.0 and 1.0.8 and get the same error with the new version number. I have included the maven line in my project build gradle file's repository section and the other line in my app build gradle file.
Any idea what I'm doing wrong?
My XML file has multiple hyperlinks which the user will be able to download and share around in the PDF format.
Most of the other tools I found convert the XML into an image first which wont work for openable links.
Does this library also uses similar method or will I be able to open the links?
It takes 1 mins. to produce the PDF... Is it possible to minimize the time or speed up the file generation? I tried this line on my Manifest but same result. Thank you!
<application
android:hardwareAccelerated="true"
...
/>
<?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="@android:color/white">
<TextView
android:id="@+id/tv_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textSize="15px"
android:textColor="@android:color/black"
/>
</RelativeLayout>
`txtFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PdfDocument doc = new PdfDocument(getActivity());
doc.addPage(new AbstractViewRenderer(getActivity(), R.layout.pdf_fragment) {
@Override
protected void initView(View view) {
TextView tv_hello = (TextView) view.findViewById(R.id.tv_hello);
tv_hello.setText("This is TEST..PAGE 1..!!!!");
}
});
doc.addPage(new AbstractViewRenderer(getActivity(), R.layout.pdf_fragment) {
@Override
protected void initView(View view) {
TextView tv_hello = (TextView) view.findViewById(R.id.tv_hello);
tv_hello.setText("This is TEST.PAGE 2...!!!!");
}
});
doc.addPage(new AbstractViewRenderer(getActivity(), R.layout.pdf_fragment) {
@Override
protected void initView(View view) {
TextView tv_hello = (TextView) view.findViewById(R.id.tv_hello);
tv_hello.setText("This is TEST.PAGE 3...!!!!");
}
});
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");
Log.e(TAG, "onComplete: " + file.getAbsolutePath());
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
}
@Override
public void onError(Exception e) {
Log.i(PdfDocument.TAG_PDF_MY_XML, "Error");
}
});
doc.createPdf(getActivity());
}
});
Text getting blur if i zoom the pdf
I can only set text to one textview, how do I do this for the the textviews I have.
I have created view with the concept of Databinding. Now i want to convert that view in PDF with the help of your lib.
it is giving me error like
Process: com.example.the_king.apartmentmanagementsystem, PID: 18215 java.lang.IllegalStateException: DataBinding must be created in view's UI Thread
is it possible to convert my databinding view in to pdf with the help of your library ?
is this possible to upload ur file to jcenter or bintray?
its just picking static View
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.