Skip to content

Commit

Permalink
Added support for local file
Browse files Browse the repository at this point in the history
  • Loading branch information
boldijar committed Apr 13, 2023
1 parent 0ba9a83 commit 8730e63
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pdfyView.setPdf(
)
```

You can also load it from assets, by using PdfyType.FROM_ASSETS and using the relative path after assets, or just use the LOCAL_FILE type, and use the absolute file path.

![](https://github.com/boldijar/pdfy/raw/main/gif.gif)

13 changes: 13 additions & 0 deletions pdfy/src/main/java/io/github/boldijar/pdfy/PdfyParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.graphics.pdf.PdfRenderer
import android.os.ParcelFileDescriptor
import io.github.boldijar.pdfy.ui.PdfyImageLoader
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream

class PdfyParser(
Expand All @@ -34,6 +35,7 @@ class PdfyParser(
return when (type) {
PdfyType.FROM_ASSETS -> createPdfFileFromAssets(context)
PdfyType.FROM_INTERNET -> createPdfFileFromInternet(context)
PdfyType.FROM_LOCAL_FILE -> createPdfFromLocalFile(context)
}
}

Expand Down Expand Up @@ -69,6 +71,17 @@ class PdfyParser(
return cachedFile
}

private fun createPdfFromLocalFile(context: Context): File {
val inputStream = FileInputStream(File(path))
val cachedFile = preparePdfFile(context)
val outputStream = FileOutputStream(cachedFile)
inputStream.copyTo(outputStream)
outputStream.flush()
outputStream.close()
inputStream.close()
return cachedFile
}

fun parsePDF(context: Context): Pdfy {
try {
val file = createPdfFile(context)
Expand Down
3 changes: 2 additions & 1 deletion pdfy/src/main/java/io/github/boldijar/pdfy/PdfyType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ package io.github.boldijar.pdfy

enum class PdfyType {
FROM_ASSETS,
FROM_INTERNET
FROM_INTERNET,
FROM_LOCAL_FILE
}

0 comments on commit 8730e63

Please sign in to comment.