abhay0648 / pdf_merger

A new Flutter plugin for merge List of PDF files. It supports both android and IOS.
https://pub.dev/packages/pdf_merger
MIT License
6 stars 25 forks source link

An error will be reported when the output path has ".". #4

Open lan2000 opened 2 years ago

lan2000 commented 2 years ago

android 10 pdf_merger 0.0.6

code: final outputDirPath= '/data/user/0/com.auc.pdf_merger_example/cache/TestPDFMergerABCEFG5.png'; CreateImageFromPDFResponse response = await PdfMerger.createImageFromPDF( path: singleFile, outputDirPath: outputDirPath, createOneImage: false);

An error will be reported when the output path has ".". error log: W/System.err(28691): java.io.FileNotFoundException: /data/user/0/com0.auc: open failed: EACCES (Permission denied) W/System.err(28691): at libcore.io.IoBridge.open(IoBridge.java:496) W/System.err(28691): at java.io.FileOutputStream.(FileOutputStream.java:235) W/System.err(28691): at java.io.FileOutputStream.(FileOutputStream.java:125) W/System.err(28691): at com.ril.pdf_merger.CreateImageFromPDF$create$pdfFromMultipleImage$1.invokeSuspend(CreateImageFromPDF.kt:67) W/System.err(28691): at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) W/System.err(28691): at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) W/System.err(28691): at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:571) W/System.err(28691): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) W/System.err(28691): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:678) W/System.err(28691): at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:665) W/System.err(28691): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) W/System.err(28691): at libcore.io.Linux.open(Native Method) W/System.err(28691): at libcore.io.ForwardingOs.open(ForwardingOs.java:167) W/System.err(28691): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252) W/System.err(28691): at libcore.io.ForwardingOs.open(ForwardingOs.java:167) W/System.err(28691): at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:8464) W/System.err(28691): at libcore.io.IoBridge.open(IoBridge.java:482)

CreateImageFromPDF.kt

        for (i in 0 until pageCount) {
                val page: CodecPage = decodeService.getPage(i)
                val rectF = RectF(0.toFloat(), 0.toFloat(), 1.toFloat(), 1.toFloat())

                val bitmap: Bitmap = page.renderBitmap(maxWidth!!, maxHeight!!, rectF)
                pdfImages.add(bitmap)

                if(!createOneImage!!){

                    val splitPath = outputDirPath!!.split(".")[0]
                    val splitPathExt = outputDirPath.split(".")[1];

                    Log.e("----MainActivity outputDirPath", outputDirPath);
                    Log.e("----MainActivity splitPath", splitPath);
                    Log.e("----MainActivity splitPathExt", splitPathExt);
                    print(splitPath)
                    print(splitPathExt)
                    val newPath = """$splitPath$i.$splitPathExt"""
                    pdfImagesPath.add(newPath)
                    val outputStream = FileOutputStream(newPath)
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
                    outputStream.close()
                }
            }

log: E/----MainActivity outputDirPath(28691): /data/user/0/com.auc.pdf_merger_example/cache/TestPDFMergerABCEFG5.png E/----MainActivity splitPath(28691): /data/user/0/com E/----MainActivity splitPathExt(28691): auc

lan2000 commented 2 years ago

Now you must set the width and height of the picture. Can you add a choice to make it use the original width and height.

CreateImageFromPDF.kt

fun create(path: String?, outputDirPath: String?, maxWidth : Int?, maxHeight : Int? , createOneImage : Boolean?)

// val bitmap: Bitmap = page.renderBitmap(maxWidth!!, maxHeight!!, rectF) val bitmap: Bitmap = page.renderBitmap(page.width, page.height, rectF) //use original width and height.

lan2000 commented 2 years ago

Or scale the picture according to the maximum width and height as IOS. public static func resizeImage(img: UIImage, maxWidthGet : Int, maxHeightGet : Int) -> UIImage { var actualHeight: Float = Float(img.size.height) var actualWidth: Float = Float(img.size.width) let maxHeight: Float = Float(maxHeightGet) let maxWidth: Float = Float(maxWidthGet) var imgRatio: Float = actualWidth / actualHeight let maxRatio: Float = maxWidth / maxHeight // let compressionQuality: Float = 0.5 //50 percent compression

    if actualHeight > maxHeight || actualWidth > maxWidth {
        if imgRatio < maxRatio {
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight
            actualWidth = imgRatio * actualWidth
            actualHeight = maxHeight
        }
        else if imgRatio > maxRatio {
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth
            actualHeight = imgRatio * actualHeight
            actualWidth = maxWidth
        }
        else {
            actualHeight = maxHeight
            actualWidth = maxWidth
        }
    }

    let rect = CGRect(x: 0.0, y: 0.0, width: CGFloat(actualWidth),  height: CGFloat(actualHeight))

    let newSize = AVMakeRect(
        aspectRatio: img.size,
        insideRect: rect
     ).size

// UIGraphicsBeginImageContext(rect.size)

    UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
    img.draw(in: CGRect(origin: .zero, size: newSize))
    let scaled = UIGraphicsGetImageFromCurrentImageContext() ?? img
    UIGraphicsEndImageContext()

// img.draw(in:rect) // let img = UIGraphicsGetImageFromCurrentImageContext() // let imageData = img?.jpegData(compressionQuality: CGFloat(compressionQuality)) // UIGraphicsEndImageContext() return scaled }