Skip to content

Commit

Permalink
Merge pull request #3 from omar908/auto-resize-pdf-page
Browse files Browse the repository at this point in the history
Fix for cutting off images that are longer than the default PDF Page Size
  • Loading branch information
omar908 authored Dec 7, 2024
2 parents eb0a810 + b3a5e9f commit 1a48a9a
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.puchunguita.cbzconverter
import android.net.Uri
import android.os.Environment
import com.itextpdf.io.image.ImageDataFactory
import com.itextpdf.kernel.geom.PageSize
import com.itextpdf.kernel.pdf.PdfDocument
import com.itextpdf.kernel.pdf.PdfWriter
import com.itextpdf.layout.Document
Expand Down Expand Up @@ -252,7 +253,8 @@ private fun createMultiplePdfFromCbz(

PdfWriter(outputFile.absolutePath).use { writer ->
PdfDocument(writer).use { pdfDoc ->
Document(pdfDoc).use { document ->
Document(pdfDoc, PageSize.LETTER).use { document ->
setMarginForDocument(document)
for ((currentImageIndex, imageFile) in imagesToProcess.withIndex()) {
subStepStatusAction(
"Processing part ${index + 1} of $amountOfFilesToExport " +
Expand Down Expand Up @@ -282,7 +284,8 @@ private fun createSinglePdfFromCbz(

PdfWriter(outputFile.absolutePath).use { writer ->
PdfDocument(writer).use { pdfDoc ->
Document(pdfDoc).use { document ->
Document(pdfDoc, PageSize.LETTER).use { document ->
setMarginForDocument(document)
for ((currentImageIndex, imageFile) in zipFileEntriesList.withIndex()) {
subStepStatusAction(
"Processing image file " +
Expand All @@ -297,6 +300,11 @@ private fun createSinglePdfFromCbz(
}
}

private fun setMarginForDocument(document: Document){
// Overriding margins, due to lots of empty space at the bottom of longer pages
document.setMargins(15f, 10f, 15f, 10f) // Top, Right, Bottom, Left
}

private fun extractImageAndAddToPDFDocument(
zipFile: ZipFile,
zipFileEntry: ZipEntry,
Expand All @@ -311,7 +319,14 @@ private fun extractImageAndAddToPDFDocument(
val imageData = ImageDataFactory.create(imageFileByteArray)
val pdfImage = Image(imageData)

// Add the image to the PDF document
// Adjust the PDF page size to match the image dimensions
val pdfPageSize = PageSize(
pdfImage.imageWidth,
pdfImage.imageHeight
)
document.pdfDocument.setDefaultPageSize(pdfPageSize)

// Add the scaled image to the PDF document
document.add(pdfImage)
imageInputStream.close()
} catch (e: Exception) {
Expand Down

0 comments on commit 1a48a9a

Please sign in to comment.