Convert Android view to PDF of A4 size - java

I have an android resume building application. I want to generate a PDF of size A4 from my view. Here's how my layout looks like - At the top I have a Top App Bar, and the whole view in encapsulated in drawer. The main part which contains user's details is encapsulated in nestedScrollView, which contains multiple LinearLayout and TextView. In this screenshot below, I have populated it with mock data, but in actuality, I am fetching data from the Firebase Realtime Database and displaying it on the UI.
I tried to understand iTextPdf solution and multiple question of similar type that has been asked here, but I couldn't find something solid. Please help me out, it would be of great help.
Also, please don't close this question by giving a reason that the question doesn't contain any code. It doesn't because I don't have any. I am trying to solve this problem from scratch. I have tried to describe my problem as much as I could.

try this:
create a WebView and copy the text of your edittext in it:
webview.loadData(youredittext.gettext().tostring, "text/html", "UTF-8");
and convert webview to pdf by below function:
private void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
webView.createPrintDocumentAdapter();
String jobName = getString(R.string.app_name) + " Print Test";
if (printManager != null) {
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
}
after that user can select page size for example A4

There are a lot of libraries that convert layouts to PDF, but let's opt for popular one so we could find answers if we're stuck.
The libraries I listed works like so : They take screenshot of your layout as bitmap image and convert it to pdf.
- 1st solution: iTextPDF https://github.com/itext/itext7 (New Version).
check this detailed tutorial which treates also the case of taking screenshot of a scrollview https://www.codeproject.com/Articles/989236/How-to-Convert-Android-View-to-PDF-2
and this stackoverflow answer https://stackoverflow.com/a/29731275/12802591
- 2nd solution: PdfMyXML library https://github.com/HendrixString/Android-PdfMyXml just follow the steps in the documentation.
They may be other solutions, but these are the popular ones.
Let Me know if it works for you and also if you're stuck. Thank you!

Related

Text imported from realm DB not clickable

I'm helping to build an app which ask you a series of questions and returns the appropriate stone (geological app). The app also has an encyclopedia and a guide.
There are link to explanations in the guide when asking questions and these work fine.
The problem occurs when I want to use the links in the guide itself.
The guide uses a Realm DB for the info and puts it in an expandable list, while the questions use a string resource.
I want to be able to use the link in the text that I put in the Realm DB, but currently it just prints it as a normal string like so
".. ali več različnih mineralnih zrn , nekatere ..."
This is the same code that works fine in the questions section.
The code I use to "try" and convert the strings to links from the DB is bellow.
convertView = layoutInflater.inflate(R.layout.kamnina_item_second, null);
TextView opis = (TextView) convertView.findViewById(R.id.opis_kamnine);
String stone_opis_string = expandableListDetail.get("OPIS_POJMA").get(0);
opis.setText(stone_opis_string);
Linkify.addLinks(opis,Linkify.ALL);
opis.setMovementMethod(LinkMovementMethod.getInstance());
You could try:
String stone_opis_string = expandableListDetail.get("OPIS_POJMA").get(0);
opis.setText(Html.fromHtml(stone_opis_string));
// Linkify.addLinks(opis,Linkify.ALL);
opis.setMovementMethod(LinkMovementMethod.getInstance());

How to add title and content after Facebook deprecation?

Here's some code that successfully shares a link to Facebook after a button click:
public void onClick(View view) {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("A title")
.setContentDescription("Some description.")
.setContentUrl(Uri.parse("www.website.com"))
.build();
shareDialog.show(linkContent);
}
}
Using Android Studio, the ".setContentTitle" and ".setContentDescription" are deprecated, with a line through them. When I post the link, it is shared without the title and description. I assume this is because they are deprecated.
How could I add a title and description in? What were the deprecated terms replaced with? This isn't pre-filling a a post, and it wouldn't make sense for Facebook to get rid of these features completely. I have tried a few different links as the URL, none made a difference to this issue.
Many thanks in advance.
Edit: Please note that meta tags aren't an option, because if I were to link to an app in the Google Play Store, I can't control what tags the page has. I am looking to provide a title/description from the app, as was previously possible using the deprecated features mentioned.
I have found a suitable way around this, though not one that specifically replaces title and description. Another way to automatically add text to a post without pre-filling the user's text box is to use .setQuote().
For example with the code I provided above:
public void onClick(View view) {
if (ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setQuote("This may be used to replace setTitle and setDescription.")
.setContentUrl(Uri.parse("www.website.com"))
.build();
shareDialog.show(linkContent);
}
}
If anyone knows a way to replace the deprecated functions properly, without such a different alternative like the one I just provided, please post it and I'll mark it as solved.
Many thanks.
You can use ShareOpenGraphContent that uses ShareOpenGraphAction and ShareOpenGraphObject.
Look at the code I answered in this question.
https://stackoverflow.com/a/46459350/4107421
That way you can add a title, description and even an image to your post.
It works for ShareDialog.show() but unfortunately on my experience it doesn't on MessageDialog.show()

Vaadin how to print dynamic raw html using PrintUI class?

I'm following the example code which puts the html string into a Label. The html is perfect in the browser, is multiple pages and so on. However when I do a Print Preview (or Print) the printout is limited to only one page and there is vertical scrollbar on the printout.
How do I print multiple pages and remove the scrollbar?
My code in the PrintUI class is only:
setContent(new Label(template, ContentMode.HTML));
The answer can be found at: https://vaadin.com/forum/#!/thread/3869543/3869542
You basically need to resort to pure html. The following code does this and fixes the issue:
private void setSizeUndefined2Print()
{
com.vaadin.ui.JavaScript.getCurrent().execute("document.body.style.overflow = \"auto\";" +
"document.body.style.height = \"auto\"");
UI.getCurrent().setSizeUndefined();
this.setSizeUndefined();
}
You can find more details in the above link.

Display Images in TextView html

I state that I have already read all the other questions but none is right for me.My app retrieves data from a database. If I put in the database in the app does not display the image, while the other tag html yes because i put:
Text = (TextView) this.findViewById(R.article.text);
String formattedText = db.getText();
Text.setText(Html.fromHtml(formattedText));
For images I would like something that the download so that they are always available. I tried to put ImageGetter but with the loading time of an article in the app increased a lot and very often said that Android is not responding (ANR). I also need something that resizes images depending on the display. Any ideas?
Take a look at AQuery download the latest jar add it in your project
and use it like following
AQuerymAQuery;
mAquery=new AQuery(context);
mAquery.id(ImageView).auth(handle).image(ImagePath,true,true,400,0,null,0,0.0f);

Calling Java method from HTML link

I'm currently building a Twitter client in Java using the Twitter4J API. To create a Twitter "timeline", I am currently pulling data from Twitter such as profile images, tweets and usernames, then displaying them in a JTextPane, formatted using HTML. Code example below:
StringBuilder out = new StringBuilder();
try {
List<Status> statuses = HandleEvents.instance().twitter.getHomeTimeline();
out.append("<html>");
for (Status status : statuses)
{
out.append("<img src=\"").append(status.getUser().getProfileImageURL())
.append("\" width=30 height=30><b>").append(status.getUser().getName())
.append(":</b> ").append(status.getText())
.append("<br><br>");
}
out.append("</html>");
tweetsTextPane.setText(out.toString());
This displays a timeline of 20 tweets, separated by two line breaks. Under each tweet, I would like to place a simple hyperlink, called "Retweet", which calls one of my Java methods - HandleEvents.instance().twitter.retweetStatus(status.getId())
How would I got about doing this? Can the call be made directly between the tags, or do I have to make the call using JavaScript?
Any help would be appreciated. Many thanks.
You don't really need to have a hyperlink do you? Since it's a Swing app you could just add a JLabel that only looks like a hyperlink (but if you put in a little effort, it could behave like one as well). Add a listener for mouse clicks on that JLabel and you've can hook your current handler there.
On the other hand, if you do want actual HTML links, what you can do is implement your own HyperlinkListener.
Here are a couple of examples:
http://www.java2s.com/Tutorial/Java/0240__Swing/HyperlinkListenerExample.htm
http://www.java2s.com/Code/Java/Event/DemonstratingtheHyperlinkListener.htm
http://www.devx.com/tips/Tip/12997

Categories