├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml └── runConfigurations.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── convertwebviewtopdf │ │ └── demo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── convertwebviewtopdf │ │ │ └── demo │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── 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_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ │ └── xml │ │ └── provider_paths.xml │ └── test │ └── java │ └── com │ └── convertwebviewtopdf │ └── demo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── webviewtopdf ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src ├── androidTest └── java │ └── com │ └── webviewtopdf │ └── ExampleInstrumentedTest.java ├── main ├── AndroidManifest.xml ├── java │ ├── android │ │ └── print │ │ │ └── PdfPrint.java │ └── com │ │ └── webviewtopdf │ │ └── PdfView.java └── res │ └── values │ └── strings.xml └── test └── java └── com └── webviewtopdf └── ExampleUnitTest.java /.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/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pramodkr123/ConvertWebViewToPdfDemo/5ab92e6bcac31b91b42127ef5c70d0a70b47a96f/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 pramodkr123 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ConvertWebViewToPdf 2 | Library to create pdf file from webview 3 | 4 | Add the following to your project level build.gradle: 5 | 6 | allprojects { 7 | repositories { 8 | maven { url "https://jitpack.io" } 9 | } 10 | } 11 | 12 | Add this to your app build.gradle: 13 | 14 | implementation 'com.github.pramodkr123:ConvertWebViewToPdfDemo:1.0.5' 15 | 16 | Permission in Manifest 17 | 18 | 19 | 20 | 21 | If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps. 22 | Add a FileProvider tag in AndroidManifest.xml under tag. 23 | 24 | 25 | 29 | ... 30 | 35 | 38 | 39 | 40 | 41 | 42 | 43 | Then create a provider_paths.xml file in res/xml folder. 44 | 45 | 46 | 49 | 52 | 53 | 54 | Before Create pdf check this permission for Android 11 devices. 55 | 56 | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { 57 | super.onActivityResult(requestCode, resultCode, data) 58 | // check permission is Granting 59 | if (requestCode == 1 && Environment.isExternalStorageManager()){ 60 | //write create pdf code here 61 | } 62 | } 63 | 64 | 65 | 66 | 67 | Sample code : 68 | 69 | final String fileName="Test.pdf"; 70 | File file = new File(getExternalFilesDir("DemoApp"), fileName); 71 | if (file.exists()) file.delete(); 72 | 73 | final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); 74 | progressDialog.setMessage("Please wait"); 75 | progressDialog.show(); 76 | PdfView.createWebPrintJob(MainActivity.this, webView, file.getAbsolutePath(), new PdfView.Callback() { 77 | 78 | @Override 79 | public void success(String path) { 80 | progressDialog.dismiss(); 81 | PdfView.openPdfFile(MainActivity.this, getString(R.string.app_name), "Do you want to open the pdf file?" + fileName, path, "com.convertwebviewtopdf.demo.fileprovider"); 82 | } 83 | 84 | @Override 85 | public void failure(int errorCode) { 86 | progressDialog.dismiss(); 87 | } 88 | }); 89 | 90 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 33 5 | 6 | defaultConfig { 7 | applicationId "com.convertwebviewtopdf.demo" 8 | minSdkVersion 19 9 | targetSdkVersion 33 10 | versionCode 2 11 | versionName "2" 12 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(include: ['*.jar'], dir: 'libs') 24 | implementation 'androidx.appcompat:appcompat:1.3.0' 25 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 26 | implementation 'androidx.core:core:1.8.0' 27 | testImplementation 'junit:junit:4.12' 28 | androidTestImplementation 'androidx.test.ext:junit:1.1.2' 29 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' 30 | implementation project(':webviewtopdf') 31 | } 32 | -------------------------------------------------------------------------------- /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/com/convertwebviewtopdf/demo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.convertwebviewtopdf.demo; 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("com.convertwebviewtopdf.demo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | 18 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 33 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/java/com/convertwebviewtopdf/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.convertwebviewtopdf.demo; 2 | 3 | import android.app.ProgressDialog; 4 | import android.os.Bundle; 5 | import android.webkit.WebView; 6 | import android.widget.Button; 7 | 8 | import com.webviewtopdf.PdfView; 9 | 10 | import java.io.File; 11 | 12 | import androidx.appcompat.app.AppCompatActivity; 13 | 14 | public class MainActivity extends AppCompatActivity { 15 | 16 | 17 | WebView webView; 18 | Button button_convert; 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | 25 | webView=findViewById(R.id.webView); 26 | button_convert=findViewById(R.id.button_convert); 27 | 28 | webView.loadUrl("https://developer.android.com/develop"); 29 | 30 | 31 | button_convert.setOnClickListener(v -> { 32 | final String fileName="Test.pdf"; 33 | File file = new File(getExternalFilesDir("DemoApp"), fileName); 34 | if (file.exists()) file.delete(); 35 | 36 | final ProgressDialog progressDialog=new ProgressDialog(MainActivity.this); 37 | progressDialog.setMessage("Please wait"); 38 | progressDialog.show(); 39 | PdfView.createWebPrintJob(MainActivity.this, webView, file.getAbsolutePath(), new PdfView.Callback() { 40 | 41 | @Override 42 | public void success(String path) { 43 | progressDialog.dismiss(); 44 | PdfView.openPdfFile(MainActivity.this, getString(R.string.app_name), "Do you want to open the pdf file?" + fileName, path, "com.convertwebviewtopdf.demo.fileprovider"); 45 | } 46 | 47 | @Override 48 | public void failure(int errorCode) { 49 | progressDialog.dismiss(); 50 | } 51 | }); 52 | 53 | }); 54 | 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 12 | 13 | 14 |