Skip to content

Commit

Permalink
Merge pull request Afur#53 from nikitadol/master
Browse files Browse the repository at this point in the history
Update android plugin
  • Loading branch information
Afur authored Sep 26, 2021
2 parents b44541f + e44fb61 commit d56aaaa
Show file tree
Hide file tree
Showing 31 changed files with 467 additions and 179 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
.pub/
pubspec.lock

/.idea/
.idea/
build/

# generated files not to be checked in
flutter_export_environment.sh
.flutter-plugins-dependencies
Flutter.podspec
Flutter.podspec
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
1 change: 1 addition & 0 deletions android/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.DS_Store
/build
/captures
.idea/
26 changes: 16 additions & 10 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
group 'com.afur.flutterhtmltopdf'
group 'com.afur.flutter_html_to_pdf'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.3.21'
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 28
compileSdkVersion 30

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

kotlinOptions {
jvmTarget = '1.8'
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}

defaultConfig {
minSdkVersion 16
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
lintOptions {
disable 'InvalidPackage'
}
}

Expand Down
2 changes: 2 additions & 0 deletions android/gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
org.gradle.jvmargs=-Xmx1536M
android.useAndroidX=true
android.enableJetifier=true
5 changes: 5 additions & 0 deletions android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-all.zip
3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.afur.flutterhtmltopdf">

package="com.afur.flutter_html_to_pdf">
</manifest>
49 changes: 31 additions & 18 deletions android/src/main/kotlin/android/print/PdfPrinter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,43 @@ class PdfPrinter(private val printAttributes: PrintAttributes) {
}


fun print(printAdapter: PrintDocumentAdapter, path: File, fileName: String, callback: PdfPrinter.Callback) {
fun print(
printAdapter: PrintDocumentAdapter,
path: File,
fileName: String,
callback: Callback
) {
// Support for min API 16 is required
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
printAdapter.onLayout(null, printAttributes, null, object : PrintDocumentAdapter.LayoutResultCallback() {
printAdapter.onLayout(
null,
printAttributes,
null,
object : PrintDocumentAdapter.LayoutResultCallback() {

override fun onLayoutFinished(info: PrintDocumentInfo, changed: Boolean) {
printAdapter.onWrite(arrayOf(PageRange.ALL_PAGES), getOutputFile(path, fileName),
CancellationSignal(), object : PrintDocumentAdapter.WriteResultCallback() {
override fun onLayoutFinished(info: PrintDocumentInfo, changed: Boolean) {
printAdapter.onWrite(arrayOf(PageRange.ALL_PAGES),
getOutputFile(path, fileName),
CancellationSignal(),
object : PrintDocumentAdapter.WriteResultCallback() {

override fun onWriteFinished(pages: Array<PageRange>) {
super.onWriteFinished(pages)
override fun onWriteFinished(pages: Array<PageRange>) {
super.onWriteFinished(pages)

if (pages.isEmpty()) {
callback.onFailure()
}
if (pages.isEmpty()) {
callback.onFailure()
}

File(path, fileName).let {
callback.onSuccess(it.absolutePath)
}
File(path, fileName).let {
callback.onSuccess(it.absolutePath)
}

}
})
}
}, null)
}
})
}
},
null
)
}
}
}
Expand All @@ -47,7 +60,7 @@ private fun getOutputFile(path: File, fileName: String): ParcelFileDescriptor {
path.mkdirs()
}

File(path, fileName).let{
File(path, fileName).let {
it.createNewFile()
return ParcelFileDescriptor.open(it, ParcelFileDescriptor.MODE_READ_WRITE)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.afur.flutter_html_to_pdf

import android.content.Context
import androidx.annotation.NonNull

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result

/** FlutterHtmlToPdfPlugin */
class FlutterHtmlToPdfPlugin: FlutterPlugin, MethodCallHandler {
private lateinit var channel : MethodChannel
private lateinit var applicationContext: Context

override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "flutter_html_to_pdf")
channel.setMethodCallHandler(this)

applicationContext = flutterPluginBinding.applicationContext
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
if (call.method == "convertHtmlToPdf") {
convertHtmlToPdf(call, result)
} else {
result.notImplemented()
}
}

override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
channel.setMethodCallHandler(null)
}

private fun convertHtmlToPdf(call: MethodCall, result: Result) {
val htmlFilePath = call.argument<String>("htmlFilePath")

HtmlToPdfConverter().convert(htmlFilePath!!, applicationContext, object : HtmlToPdfConverter.Callback {
override fun onSuccess(filePath: String) {
result.success(filePath)
}

override fun onFailure() {
result.error("ERROR", "Unable to convert html to pdf document!", "")
}
})
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.afur.flutterhtmltopdf
package com.afur.flutter_html_to_pdf

import android.app.Activity
import android.annotation.SuppressLint
import android.content.Context
import android.os.Build
import android.print.PdfPrinter
import android.print.PrintAttributes
Expand All @@ -17,29 +18,30 @@ class HtmlToPdfConverter {
fun onFailure()
}

fun convert(filePath: String, activity: Activity, callback: Callback) {
val webView = WebView(activity.applicationContext)
@SuppressLint("SetJavaScriptEnabled")
fun convert(filePath: String, applicationContext: Context, callback: Callback) {
val webView = WebView(applicationContext)
val htmlContent = File(filePath).readText(Charsets.UTF_8)
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setAllowFileAccess(true);
webView.settings.javaScriptEnabled = true
webView.settings.javaScriptCanOpenWindowsAutomatically = true
webView.settings.allowFileAccess = true
webView.loadDataWithBaseURL(null, htmlContent, "text/HTML", "UTF-8", null)
webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
super.onPageFinished(view, url)
createPdfFromWebView(webView, activity, callback)
createPdfFromWebView(webView, applicationContext, callback)
}
}
}

fun createPdfFromWebView(webView: WebView, activity: Activity, callback: Callback) {
val path = activity.applicationContext.filesDir
fun createPdfFromWebView(webView: WebView, applicationContext: Context, callback: Callback) {
val path = applicationContext.filesDir
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

val attributes = PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
.setMinMargins(PrintAttributes.Margins.NO_MARGINS).build()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(PrintAttributes.Resolution("pdf", "pdf", 600, 600))
.setMinMargins(PrintAttributes.Margins.NO_MARGINS).build()

val printer = PdfPrinter(attributes)

Expand Down

This file was deleted.

29 changes: 29 additions & 0 deletions example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

linter:
# The lint rules applied to this project can be customized in the
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
# included above or to enable additional rules. A list of all available lints
# and their documentation is published at
# https://dart-lang.github.io/linter/lints/index.html.
#
# Instead of disabling a lint rule for the entire project in the
# section below, it can also be suppressed for a single line of code
# or a specific dart file by using the `// ignore: name_of_lint` and
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
# producing the lint.
rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule
# prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
13 changes: 13 additions & 0 deletions example/android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
gradle-wrapper.jar
/.gradle
/captures/
/gradlew
/gradlew.bat
/local.properties
GeneratedPluginRegistrant.java

# Remember to never publicly share your keystore.
# See https://flutter.dev/docs/deployment/android#reference-the-keystore-from-the-app
key.properties
**/*.keystore
**/*.jks
Loading

0 comments on commit d56aaaa

Please sign in to comment.