참고
https://www.baeldung.com/java-pdf-creation
itext 를 이용하는 방법
라이센스 문제가 있다. 상업적 이용은 라이센스를 구매하거나, 구매하지 않는다면 내 소스코드를 공개해야한다. (AGPL)
implementation 'com.itextpdf:itextg:5.5.10' // images to pdf
// or implementation 'com.itextpdf:itextpdf:5.5.13.4' // images to pdf
import com.itextpdf.text.Document
import com.itextpdf.text.Image
import com.itextpdf.text.PageSize
import com.itextpdf.text.pdf.PdfWriter
import java.io.ByteArrayOutputStream
import java.io.FileInputStream
// itextpdf
fun writeImagesForItext(uriStrList: List<String>, outputUri: Uri): Int {
var count: Int = 0
try {
val document = Document(PageSize.A4, 15f, 15f, 15f, 15f)
val pdfWriter = PdfWriter.getInstance(document, contentResolver.openOutputStream(outputUri))
document.open()
val stream = ByteArrayOutputStream()
for (uriStr in uriStrList) {
try {
stream.reset() // for reuse
val uri = Uri.parse(uriStr)
val bitmap: Bitmap = if (uri.scheme != "content") {
BitmapFactory.decodeStream(FileInputStream(uriStr))
} else {
contentResolver.openInputStream(uri).use {
BitmapFactory.decodeStream(it)
}
}
var quality = Settings.writePdfImageQuality.value
if (quality < 5) quality = 5
if (quality > 100) quality = 100
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream)
if (true) {
val image = Image.getInstance(stream.toByteArray())
document.setPageSize(image)
document.newPage()
image.setAbsolutePosition(0f, 0f)
document.add(image)
}
bitmap.recycle()
count++
} catch (ex: Exception) {
if (BuildConfig.DEBUG) {
DUtils.alert(ex.toString())
}
}
}
// close
document.close()
pdfWriter?.close()
} catch (ex: Exception) {
if (BuildConfig.DEBUG) {
DUtils.alert(ex.toString())
}
}
return count
}
Apache pdfbox를 이용하는 방법
https://pdfbox.apache.org/download.html 를 이용할 수 있지만 Android에서는 바로 사용할 수 없다. Android으로 랩핑한 라이브러리를 이용한다. https://github.com/TomRoush/PdfBox-Android
implementation 'com.tom-roush:pdfbox-android:2.0.27.0'
import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.tom_roush.pdfbox.pdmodel.PDPage
import com.tom_roush.pdfbox.pdmodel.PDPageContentStream
import com.tom_roush.pdfbox.pdmodel.common.PDRectangle
import com.tom_roush.pdfbox.pdmodel.graphics.image.PDImageXObject
// apache pdfbox
fun writeImagesForPdfBox(uriStrList: List<String>, outputUri: Uri): Int {
var count: Int = 0
try {
val document = PDDocument()
val stream = ByteArrayOutputStream()
for (uriStr in uriStrList) {
try {
stream.reset() // for reuse
val uri = Uri.parse(uriStr)
val bitmap: Bitmap = if (uri.scheme != "content") {
BitmapFactory.decodeStream(FileInputStream(uriStr))
} else {
contentResolver.openInputStream(uri).use {
BitmapFactory.decodeStream(it)
}
}
var quality = Settings.writePdfImageQuality.value
if (quality < 5) quality = 5
if (quality > 100) quality = 100
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, stream)
if (true) {
val page = PDPage(PDRectangle(bitmap.width.toFloat(), bitmap.height.toFloat()))
document.addPage(page)
val img = PDImageXObject.createFromByteArray(document, stream.toByteArray(), "test")
val contentStream = PDPageContentStream(document, page)
contentStream.drawImage(img, 0f, 0f)
contentStream.close()
}
bitmap.recycle()
count++
} catch (ex: Exception) {
if (BuildConfig.DEBUG) {
DUtils.alert(ex.toString())
}
}
}
document.save(contentResolver.openOutputStream(outputUri));
document.close();
} catch (ex: Exception) {
if (BuildConfig.DEBUG) {
DUtils.alert(ex.toString())
}
}
return count
}