libaums icon indicating copy to clipboard operation
libaums copied to clipboard

PdfDocument writeTo UsbFileOutputStream, File corruption

Open jie65535 opened this issue 3 years ago • 1 comments

Background

I am using libaums to export pdf to u-disk.

Problem

Following the example, I create UsbFileOutputStream/BufferedOutputStream and then use pdfDocument.writeTo(stream) to write the pdf to u-disk.

On the PC, I cannot open this file because it is corrupted.


  • Using the same method, write the pdf to the sdcard, get the file through adb pull, normal.
  • Write the pdf to the memory cache (ByteArray), then copyTo to u-disk, the file is normal.
  • Write pdf directly to u-disk, file corrupted.

Code where problem occurs


val cache = ByteArrayOutputStream().use {
    exportPdf(it)
    it.toByteArray()
}

//  val cache = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), filename)
//  cache.outputStream().use {
//   exportPdf(it)
// }

val file = fs.rootDirectory.createFile(filename)
UsbFileStreamFactory.createBufferedOutputStream(file, fs).use { outStream ->
    cache.inputStream().use { inStream ->
        inStream.copyTo(outStream, fs.chunkSize)
    }
// exportPdf(outStream)  // file corrupted.
}

private fun exportPdf(os: OutputStream) {
  val pdfDocument = PdfDocument()
  try {
      val pageInfo = PdfDocument.PageInfo.Builder(595, 842, 1).create()
      val page = pdfDocument.startPage(pageInfo)
      drawPage(page)
      pdfDocument.finishPage(page)
      pdfDocument.writeTo(os)
      fs.flush()
  } finally {
      pdfDocument.close()
  }
}

I solved the problem temporarily using memcache for now, but I'm wondering why this is happening as I've wasted a whole day with this problem.

More info

The corrupted file is the exact same size as the normal file, but by comparing the files, most of the content is the same, with only a few differences.

Differences1

Differences2

(I wrote some code to convert pdf binary to hex string text for VSCode comparison)

jie65535 avatar Aug 02 '22 02:08 jie65535

This seems to be the same as #346

jie65535 avatar Aug 02 '22 02:08 jie65535

So to summarize, if you use the the usb outputstream and feed it into the pdf library it breaks. But if you write to somewhere else first and then copy the pdf file it is working?

Makes me wonder if there are some methods in the outputstream which are not working properly?

magnusja avatar Nov 22 '22 09:11 magnusja

So to summarize, if you use the the usb outputstream and feed it into the pdf library it breaks. But if you write to somewhere else first and then copy the pdf file it is working?

Yes

As you can see, I tried three methods to output pdf to memory cache, local file and USB storage device respectively. An exception occurred when I only used USB file output stream directly.

jie65535 avatar Nov 22 '22 09:11 jie65535

Is this still happening with v0.9.4? There has been a nasty bug fixed in https://github.com/magnusja/libaums/pull/374

magnusja avatar Mar 11 '23 11:03 magnusja

Is this still happening with v0.9.4? There has been a nasty bug fixed in #374

I will try v0.9.4. My code is still reserved, but it may take a few days. Thank you for your work!

jie65535 avatar Mar 12 '23 09:03 jie65535

The test is completed. v0.9.4 has fixed this problem. Thank you for your work!

jie65535 avatar Mar 15 '23 11:03 jie65535

Awesome thanks for getting back and sorry for that huge delay.

magnusja avatar Mar 20 '23 14:03 magnusja