├── .gitignore ├── .idea ├── assetWizardSettings.xml ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── compiler.xml ├── deploymentTargetDropDown.xml ├── gradle.xml ├── jarRepositories.xml ├── markdown-navigator-enh.xml ├── markdown-navigator.xml ├── misc.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── ru │ │ └── a402d │ │ └── texttoprint │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── assets │ │ └── html.html │ ├── ic_launcher-web.png │ ├── java │ │ └── ru │ │ │ └── a402d │ │ │ └── texttoprint │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-hdpi │ │ └── print.png │ │ ├── drawable-ldpi │ │ └── print.png │ │ ├── drawable-mdpi │ │ └── print.png │ │ ├── drawable-xhdpi │ │ └── print.png │ │ ├── drawable-xxhdpi │ │ └── print.png │ │ ├── drawable-xxxhdpi │ │ └── print.png │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ ├── ic_launcher_foreground.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── ru │ └── a402d │ └── texttoprint │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/assetWizardSettings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 54 | 55 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 20 | 21 | 22 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 | xmlns:android 32 | 33 | ^$ 34 | 35 | 36 | 37 |
38 |
39 | 40 | 41 | 42 | xmlns:.* 43 | 44 | ^$ 45 | 46 | 47 | BY_NAME 48 | 49 |
50 |
51 | 52 | 53 | 54 | .*:id 55 | 56 | http://schemas.android.com/apk/res/android 57 | 58 | 59 | 60 |
61 |
62 | 63 | 64 | 65 | .*:name 66 | 67 | http://schemas.android.com/apk/res/android 68 | 69 | 70 | 71 |
72 |
73 | 74 | 75 | 76 | name 77 | 78 | ^$ 79 | 80 | 81 | 82 |
83 |
84 | 85 | 86 | 87 | style 88 | 89 | ^$ 90 | 91 | 92 | 93 |
94 |
95 | 96 | 97 | 98 | .* 99 | 100 | ^$ 101 | 102 | 103 | BY_NAME 104 | 105 |
106 |
107 | 108 | 109 | 110 | .* 111 | 112 | http://schemas.android.com/apk/res/android 113 | 114 | 115 | ANDROID_ATTRIBUTE_ORDER 116 | 117 |
118 |
119 | 120 | 121 | 122 | .* 123 | 124 | .* 125 | 126 | 127 | BY_NAME 128 | 129 |
130 |
131 |
132 |
133 |
134 |
-------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/deploymentTargetDropDown.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 19 | 20 | -------------------------------------------------------------------------------- /.idea/jarRepositories.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 10 | 14 | 15 | 19 | 20 | 24 | 25 | 29 | 30 | -------------------------------------------------------------------------------- /.idea/markdown-navigator-enh.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/markdown-navigator.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 19 | 20 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 29 | 51 | 52 | 53 | 54 | 55 | 56 | 58 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or 4 | distribute this software, either in source code form or as a compiled 5 | binary, for any purpose, commercial or non-commercial, and by any 6 | means. 7 | 8 | In jurisdictions that recognize copyright laws, the author or authors 9 | of this software dedicate any and all copyright interest in the 10 | software to the public domain. We make this dedication for the benefit 11 | of the public at large and to the detriment of our heirs and 12 | successors. We intend this dedication to be an overt act of 13 | relinquishment in perpetuity of all present and future rights to this 14 | software under copyright law. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 | IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 | OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 | OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | For more information, please refer to 25 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TextToPrint 2 | ### Demo how print use PrintDocumentAdapter for WebView 3 | 4 | Since other software developers are too lazy to implement printing, 5 | I did it for them. 6 | 7 | The application is the simplest. One screen. WebView and print button. 8 | Gets text through the processing of Intent.View and Intent.SEND ("Open with" and "Share"). Having received the text makes it the simplest html. 9 | 10 | **see** [createWebPrintJob()](https://github.com/402d/TextToPrint/blob/master/app/src/main/java/ru/a402d/texttoprint/MainActivity.java#L375) 11 | 12 | In the settings 4 font sizes. 13 | Font A and Font B are suitable for printing on a thermal printer (58mm roll of cash tape) 14 | Font D - the smallest (80 characters per line on A4 printer) 15 | 16 | The application DOES NOT PRINT yourself. By the print button, a standard PrintDocumentAdapter for WebView is created. 17 | 18 | ## How to use the example of the clipboard. 19 | Select text -> Share -> TextToPrint -> Printer Icon -> Standard Print Preview Dialog. 20 | 21 | ## TextToPrint on Google Play 22 | Get it on Google Play 23 | 24 | 25 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 35 5 | defaultConfig { 6 | applicationId "ru.a402d.texttoprint" 7 | minSdkVersion 21 8 | targetSdkVersion 35 9 | versionCode 9 10 | versionName "1.7" 11 | multiDexEnabled = true 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled true 17 | shrinkResources true 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | compileOptions { 22 | sourceCompatibility JavaVersion.VERSION_1_8 23 | targetCompatibility JavaVersion.VERSION_1_8 24 | } 25 | namespace 'ru.a402d.texttoprint' 26 | } 27 | 28 | dependencies { 29 | implementation 'androidx.appcompat:appcompat:1.7.0' 30 | implementation 'com.google.android.material:material:1.12.0' 31 | implementation fileTree(dir: 'libs', include: ['*.jar']) 32 | testImplementation 'junit:junit:4.13.2' 33 | androidTestImplementation 'androidx.test:runner:1.6.2' 34 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 35 | } 36 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/ru/a402d/texttoprint/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package ru.a402d.texttoprint; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("ru.a402d.texttoprint", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 7 | 8 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/assets/html.html: -------------------------------------------------------------------------------- 1 |

Text To Print

2 |

It is very simple app.

3 | 8 | Font A (32 chars on 58mm Roll)
9 | 123456789 123456789 123456789 12

10 | Font B (42 chars on 58mm Roll)
11 | 123456789 123456789 123456789 123456789 12

12 | Font C (65 chars in line on A4)
13 | 123456789 123456789 123456789 123456789 123456789 123456789 12345

14 | Font D (80 chars in line on A4)
15 | 123456789 123456789 123456789 123456789 123456789 123456789 123456789 1234567890

16 |
17 |
18 | APP SOURCE CODE
19 | https://github.com/402d/TextToPrint
20 | Copyright 2018-2020, 402d
21 | The Unlicense
22 | http://unlicense.org
23 |
24 |
25 | USED OPEN SOURCE LIBRARY
26 | Android In-App Billing v3 Library
27 | https://github.com/anjlab/android-inapp-billing-v3
28 | Copyright 2014 AnjLab
29 | Apache License, 2.0
30 | http://www.apache.org/licenses/LICENSE-2.0
31 | -------------------------------------------------------------------------------- /app/src/main/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/ic_launcher-web.png -------------------------------------------------------------------------------- /app/src/main/java/ru/a402d/texttoprint/MainActivity.java: -------------------------------------------------------------------------------- 1 | package ru.a402d.texttoprint; 2 | 3 | import android.Manifest; 4 | import android.annotation.SuppressLint; 5 | import android.content.ClipData; 6 | import android.content.ClipboardManager; 7 | import android.content.Context; 8 | import android.content.Intent; 9 | import android.content.SharedPreferences; 10 | import android.content.pm.PackageManager; 11 | import android.net.Uri; 12 | import android.os.Build; 13 | import android.os.Bundle; 14 | import android.os.CancellationSignal; 15 | import android.os.ParcelFileDescriptor; 16 | import android.print.PageRange; 17 | import android.print.PrintAttributes; 18 | import android.print.PrintDocumentAdapter; 19 | import android.print.PrintManager; 20 | import android.util.Log; 21 | import android.view.Menu; 22 | import android.view.MenuItem; 23 | import android.webkit.WebView; 24 | import java.io.BufferedReader; 25 | import java.io.IOException; 26 | import java.io.InputStream; 27 | import java.io.InputStreamReader; 28 | import java.nio.charset.StandardCharsets; 29 | import java.util.Locale; 30 | import java.util.Objects; 31 | 32 | import androidx.annotation.NonNull; 33 | import androidx.appcompat.app.AppCompatActivity; 34 | import androidx.appcompat.widget.Toolbar; 35 | import androidx.core.view.MenuCompat; 36 | 37 | import com.google.android.material.floatingactionbutton.FloatingActionButton; 38 | 39 | public class MainActivity extends AppCompatActivity { 40 | private final static String TAG = "Antson"; 41 | private double fontSize = 17; 42 | private double printFontSize = 26; 43 | private static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 777; 44 | 45 | SharedPreferences sPref; 46 | final String SAVED_SIZE = "saved_size"; 47 | 48 | final String htmlHead = "" + 49 | "" + 50 | "" + 57 | "" + 58 | ""; 59 | String htmlBody = ""; 60 | final String htmlFooter = ""; 61 | 62 | WebView webView; 63 | 64 | 65 | 66 | FloatingActionButton fab; 67 | 68 | @Override 69 | protected void onCreate(Bundle savedInstanceState) { 70 | super.onCreate(savedInstanceState); 71 | setContentView(R.layout.activity_main); 72 | Toolbar toolbar = findViewById(R.id.toolbar); 73 | setSupportActionBar(toolbar); 74 | 75 | fab = findViewById(R.id.fab); 76 | fab.setOnClickListener(view -> createWebPrintJob(webView)); 77 | 78 | 79 | webView = findViewById(R.id.wview); 80 | 81 | htmlBody = readAssets("html.html"); 82 | 83 | sPref = getPreferences(MODE_PRIVATE); 84 | int savedSize = sPref.getInt(SAVED_SIZE, 1); 85 | setFont(savedSize); 86 | 87 | } 88 | 89 | @Override 90 | protected void onResume() { 91 | super.onResume(); 92 | fab.setEnabled(true); 93 | } 94 | 95 | // read html body from assets 96 | private String readAssets(@SuppressWarnings("SameParameterValue") String fileName) { 97 | try { 98 | StringBuilder buf = new StringBuilder(); 99 | InputStream inputStream = getAssets().open(fileName); 100 | BufferedReader in = 101 | new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8)); 102 | String str; 103 | 104 | while ((str = in.readLine()) != null) { 105 | buf.append(str); 106 | } 107 | in.close(); 108 | return buf.toString(); 109 | } catch (Exception e) { 110 | return ""; 111 | } 112 | 113 | } 114 | 115 | private void parseIntent(Intent intent) { 116 | 117 | 118 | String action = intent.getAction(); 119 | if (action == null || action.equals(Intent.ACTION_MAIN)) { 120 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8"); 121 | return; 122 | } 123 | if (action.equals(Intent.ACTION_VIEW)) { 124 | fileBePrinted(intent.getData()); 125 | return; 126 | } 127 | 128 | Bundle extras; 129 | 130 | if (action.equals(Intent.ACTION_SEND)) { 131 | extras = intent.getExtras(); 132 | if (extras == null) { 133 | htmlBody = "

Error

ACTION_SEND no Extras

"; 134 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8"); 135 | return; 136 | } 137 | Uri uri = extras.getParcelable(Intent.EXTRA_STREAM); 138 | String stringText = extras.getString(Intent.EXTRA_TEXT); 139 | if (uri != null) { 140 | fileBePrinted(uri); 141 | return; 142 | } 143 | if (stringText == null || stringText.trim().length() == 0) { 144 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8"); 145 | return; 146 | } 147 | stringBePrinted(stringText); 148 | } 149 | 150 | } 151 | 152 | private void stringBePrinted(String s) { 153 | String[] separated = s.split("\n"); 154 | StringBuilder ss = new StringBuilder(); 155 | for (String v : separated) { 156 | ss.append(v); 157 | ss.append("
"); 158 | } 159 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + ss.toString() + htmlFooter, "text/html; charset=utf-8", "utf-8"); 160 | // createWebPrintJob(webView); 161 | } 162 | 163 | private void fileBePrinted(Uri uri) { 164 | BufferedReader br = null; 165 | StringBuilder sb = new StringBuilder(); 166 | String line; 167 | try { 168 | InputStream inputStream = getContentResolver().openInputStream(uri); 169 | br = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream))); 170 | while ((line = br.readLine()) != null) { 171 | sb.append(line).append("
"); 172 | } 173 | 174 | } catch (Exception e) { 175 | sb.append(e.getMessage()); 176 | } finally { 177 | if (br != null) { 178 | try { 179 | br.close(); 180 | } catch (IOException e) { 181 | e.printStackTrace(); 182 | } 183 | } 184 | } 185 | 186 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + sb.toString() + htmlFooter, "text/html; charset=utf-8", "utf-8"); 187 | // createWebPrintJob(webView); 188 | } 189 | 190 | 191 | public boolean onCreateOptionsMenu(@NonNull Menu menu) { 192 | MenuCompat.setGroupDividerEnabled(menu, true); 193 | 194 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 195 | menu.add(0, 1, 1, "Grant READ"); 196 | } 197 | menu.add(1, 7, 2, "Demo text"); 198 | menu.add(1, 9, 4, "Insert clipboard"); 199 | menu.add(2, 2, 6, "Font A"); 200 | menu.add(2, 3, 7, "Font B"); 201 | menu.add(2, 4, 8, "Font C"); 202 | menu.add(2, 5, 9, "Font D"); 203 | return super.onCreateOptionsMenu(menu); 204 | } 205 | 206 | @Override 207 | public boolean onOptionsItemSelected(MenuItem item) { 208 | int id = item.getItemId(); 209 | if (id == android.R.id.home) { 210 | finish(); 211 | } else if (id == 1) { 212 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 213 | if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) 214 | != PackageManager.PERMISSION_GRANTED) { 215 | 216 | requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 217 | MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); 218 | 219 | } 220 | } 221 | } else if (id == 2) { 222 | saveFont(1); 223 | setFont(1); 224 | } else if (id == 3) { 225 | saveFont(2); 226 | setFont(2); 227 | } else if (id == 4) { 228 | saveFont(3); 229 | setFont(3); 230 | } else if (id == 5) { 231 | saveFont(4); 232 | setFont(4); 233 | } else if (id == 7) { 234 | htmlBody = readAssets("html.html"); 235 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + htmlBody + htmlFooter, "text/html; charset=utf-8", "utf-8"); 236 | 237 | } else if (id == 9) { 238 | try { 239 | ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); 240 | ClipData buf = Objects.requireNonNull(clipboard.getPrimaryClip()); 241 | stringBePrinted(buf.getItemAt(0).coerceToText(this).toString()); 242 | } catch (Exception e) { 243 | e.getStackTrace(); 244 | } 245 | 246 | } 247 | return super.onOptionsItemSelected(item); 248 | } 249 | 250 | @SuppressLint("ApplySharedPref") 251 | private void saveFont(int i) { 252 | sPref = getPreferences(MODE_PRIVATE); 253 | SharedPreferences.Editor ed = sPref.edit(); 254 | ed.putInt(SAVED_SIZE, i); 255 | ed.commit(); 256 | } 257 | 258 | private void setFont(int s) { 259 | switch (s) { 260 | case 1: 261 | fontSize = 17; 262 | printFontSize = 26; 263 | break; 264 | case 2: 265 | fontSize = 13; 266 | printFontSize = 20; 267 | break; 268 | case 3: 269 | fontSize = 8.5; 270 | printFontSize = 17; 271 | break; 272 | case 4: 273 | fontSize = 8.5; 274 | printFontSize = 14; 275 | break; 276 | } 277 | 278 | parseIntent(getIntent()); 279 | } 280 | 281 | @Override 282 | public void onRequestPermissionsResult(int requestCode, 283 | @NonNull String[] permissions, @NonNull int[] grantResults) { 284 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 285 | if (requestCode == MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE) { 286 | if (grantResults.length > 0 287 | && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 288 | parseIntent(getIntent()); 289 | } 290 | } 291 | } 292 | 293 | 294 | 295 | public static class PrintDocumentAdapterWrapper extends PrintDocumentAdapter { 296 | 297 | private final PrintDocumentAdapter delegate; 298 | 299 | PrintDocumentAdapterWrapper(PrintDocumentAdapter adapter) { 300 | super(); 301 | this.delegate = adapter; 302 | } 303 | 304 | @Override 305 | public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) { 306 | delegate.onLayout(oldAttributes, newAttributes, cancellationSignal, callback, extras); 307 | Log.d(TAG, "onLayout"); 308 | } 309 | 310 | @Override 311 | public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) { 312 | delegate.onWrite(pages, destination, cancellationSignal, callback); 313 | Log.d(TAG, "onWrite"); 314 | } 315 | 316 | public void onFinish() { 317 | delegate.onFinish(); 318 | Log.d(TAG, "onFinish"); 319 | } 320 | 321 | } 322 | 323 | //create a function to create the print job 324 | private void createWebPrintJob(WebView webView) { 325 | fab.setEnabled(false); 326 | //create object of print manager in your device 327 | PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE); 328 | 329 | //create object of print adapter 330 | PrintDocumentAdapterWrapper printAdapter = new PrintDocumentAdapterWrapper(webView.createPrintDocumentAdapter()); 331 | 332 | //provide name to your newly generated pdf file 333 | String jobName = "Text2Print"; 334 | 335 | //open print dialog 336 | if (printManager != null) { 337 | printManager.print(jobName, printAdapter, new PrintAttributes.Builder().setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0)).build()); 338 | } else { 339 | webView.loadData(String.format(Locale.US, htmlHead, fontSize, printFontSize) + "PrintManager is null" + htmlFooter, "text/html; charset=utf-8", "utf-8"); 340 | 341 | } 342 | } 343 | 344 | } 345 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/drawable-hdpi/print.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-ldpi/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/drawable-ldpi/print.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/drawable-mdpi/print.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/drawable-xhdpi/print.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/drawable-xxhdpi/print.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxxhdpi/print.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/drawable-xxxhdpi/print.png -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 10 | 12 | 14 | 16 | 18 | 20 | 22 | 24 | 26 | 28 | 30 | 32 | 34 | 36 | 38 | 40 | 42 | 44 | 46 | 48 | 50 | 52 | 54 | 56 | 58 | 60 | 62 | 64 | 66 | 68 | 70 | 72 | 74 | 75 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 14 | 20 | 21 | 22 | 23 | 28 | 29 | 30 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-hdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-mdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xxhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_foreground.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/402d/TextToPrint/e6c44917de681c6c5600150865bd41ffbad8237a/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 16dp 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TextToPrint 3 | TextToPrint 4 | Billing not available 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |