-12

I followed this/this to Print Receipts in part of POS(Point of Sale) from EPSON Printer

Here I am getting data Json from URL (inside the Json Object I am getting a html print template):

{
    "response": {
        "status": "<table>.... </table>"
    }
}

so with intent I used the above json response to a string and converted it to html:

method = "addFeedLine";
mPrinter.addFeedLine(1);

textData.append("Test print Sample string\n");**//this is sample text**

textData.append(Html.fromHtml(status + "\n"));
**//this is JSON response which is nothing but HTML code, so I am converting it to string**

Over there I have used status as a string so that whatever the content is inside that string, it is printed.

If it's is not a html but just a plain text I will print it like this

method = "addFeedLine";
mPrinter.addFeedLine(1);
textData.append(status);

Here is an example of what status looks like

"status": "The store list Sample\nSTORE DIRECTOR – XYZ\n01/01/01 16:58 6153 05 0191 134\nST# 21 OP# 001 TE# 01 TR# 747\n------------------------------\n400 OHEIDA 3PK SPRINGF 9.99 R\n410 3 CUP BLK TEAPOT 9.99 R\n445 EMERIL GRIDDLE/PAN 17.99 R\n438 CANDYMAKER ASSORT 4.99 R\n474 TRIPOD 8.99 R\n433 BLK LOGO PRNTED ZO 7.99 R\n458 AQUA MICROTERRY SC 6.99 R\n493 30 L BLK FF DRESS 16.99 R\n407 LEVITATING DESKTOP 7.99 R\n441 ** Blue Overprint P 2.99 R\n476 REPOSE 4 PCPM CHOC 5.49 R\n461 WESTGATE BLACK 25 59.99 R\n------------------------------\nSUBTOTAL 160.38\nTAX 14.43\nTOTAL 174.81\nCASH 200.00\nCHANGE 25.19\n------------------------------\nPurchased item total number\nSign Up and Save!\nWith Preferred Saving Card\n"

Now, here I have a plain HTML page:

Search Images Maps Play YouTube News Gmail Drive More »
Web History | Settings | Sign in

                  Louisa May Alcott’s 184th birthday

       [                                                         ] Advanced
                                                                   searchLanguage
                   [Google Search][I'm Feeling Lucky]              tools

 Advertising ProgrammesBusiness Solutions+GoogleAbout GoogleGoogle.com

                       © 2016 - Privacy - Terms

I need to print this from an url.

Can anyone suggest me how to print this plain text? There is no HTML tags and no JSON data.

Daniel Puiu
  • 962
  • 6
  • 21
  • 29
Whats Going On
  • 1,379
  • 6
  • 20
  • 49
  • those mentioned links are not accessible.. so better to post the name and proper description of the sdk you are using rather than referring the links like you did. – Maveňツ Dec 09 '16 at 04:56
  • I have Updated Link at bottom we have download link of sdk.....All the Persons Who downvoted Please upvote and All the Answers People Please Upvote I have already loosed in bounty – Whats Going On Dec 10 '16 at 10:30

4 Answers4

1

By Html.fromHtml method you can convert HTML to String -

String strToHtml = Html.fromHtml(htmlContentInStringFormat)

Log.e(TAG,"strToHtml :: "+strToHtml);
being_sohelkhan
  • 141
  • 2
  • 11
0

If you really want to print it like the html it really is, I recommend you to get the primary html code status (before parsing it and so on) and to push it into a WebView like this:

webview.loadDataWithBaseURL("", status, "text/html", "UTF-8", "");

Otherwise, if you just want to print it into the screen, you can use a simple TextView to do that by text_view.setText(textData.toString())

zapotec
  • 2,628
  • 4
  • 31
  • 53
  • Yes I want to Print Not view... Its theremal Print – Whats Going On Dec 08 '16 at 06:12
  • 1
    Please, explain yourself better. First of all, what do you mean by "Print" (is it to print in the screen? or to send it for a printing service?). Second of all, what do you mean when you say "or How to add this plain text to a string", cause you already have the "status" String. Do you really need to send the String to a given printing service? – zapotec Dec 08 '16 at 08:51
  • I dont Konw how do you print Thermal print screen ?????? I said Its thermal Print and I followed EPSON thermal Printer SDK Here there is no webview – Whats Going On Dec 13 '16 at 03:56
0

This is the original html value:

String  htmldescription = school2.getJSONObject(0).getString("description");

This is the html formatted value:

Spanned spanned = Html.fromHtml(formattedText);

And this is the String conversion:

String formattedText = spanned.toString();

Got it from here: how to save encoded html in string
If this doesn't work out you should check out the developer docs

Hope it Helps, Good Luck!

Community
  • 1
  • 1
Shreshth Kharbanda
  • 1,777
  • 14
  • 24
0

You can use a simple Regex to convert HTML template to a plain text. It detects all types of HTML tags, but there may be loopholes.

For example:

// Regex pattern
private static final String STR_PATTERN = "\\<[^\\>]*\\>";

public static String htmlToPlainText(final String template) {

  // replaceAll(String regex, String replacement)
  return (template.replaceAll(STR_PATTERN, ""));
}

I hope it helps

Derek Wang
  • 10,098
  • 4
  • 18
  • 39
Mable John
  • 4,518
  • 3
  • 22
  • 35