1
1
package no.nav.dagpenger.pdf
2
2
3
3
import no.nav.dagpenger.io.Detect.isPdf
4
- import org.apache.pdfbox.Loader
5
- import org.apache.pdfbox.io.IOUtils
6
- import org.apache.pdfbox.io.RandomAccessReadBuffer
4
+ import org.apache.pdfbox.io.MemoryUsageSetting
7
5
import org.apache.pdfbox.multipdf.PDFMergerUtility
8
6
import org.apache.pdfbox.multipdf.Splitter
9
7
import org.apache.pdfbox.pdmodel.PDDocument
10
8
import org.apache.pdfbox.rendering.PDFRenderer
11
9
import java.awt.image.BufferedImage
10
+ import java.io.ByteArrayInputStream
12
11
import java.io.ByteArrayOutputStream
13
12
import java.io.Closeable
14
13
import java.io.InputStream
@@ -19,7 +18,7 @@ sealed class PDFDocument constructor(val document: PDDocument) : Closeable {
19
18
companion object {
20
19
fun load (bytes : ByteArray ): PDFDocument {
21
20
return try {
22
- ValidPDFDocument (Loader .loadPDF (bytes))
21
+ ValidPDFDocument (PDDocument .load (bytes))
23
22
} catch (e: Exception ) {
24
23
InvalidPDFDocument (e)
25
24
}
@@ -28,7 +27,7 @@ sealed class PDFDocument constructor(val document: PDDocument) : Closeable {
28
27
fun load (inputStream : InputStream ): PDFDocument {
29
28
return try {
30
29
inputStream.buffered().use { buffered ->
31
- ValidPDFDocument (Loader .loadPDF( RandomAccessReadBuffer ( buffered) ))
30
+ ValidPDFDocument (PDDocument .load( buffered))
32
31
}
33
32
} catch (e: Exception ) {
34
33
InvalidPDFDocument (e)
@@ -41,8 +40,8 @@ sealed class PDFDocument constructor(val document: PDDocument) : Closeable {
41
40
return ByteArrayOutputStream ().use { os ->
42
41
PDFMergerUtility ().also {
43
42
it.destinationStream = os
44
- it.addSources(pages.map { page -> RandomAccessReadBuffer (page) })
45
- it.mergeDocuments(IOUtils .createMemoryOnlyStreamCache ())
43
+ it.addSources(pages.map { page -> ByteArrayInputStream (page) })
44
+ it.mergeDocuments(MemoryUsageSetting .setupMainMemoryOnly ())
46
45
}
47
46
load(os.toByteArray())
48
47
}
0 commit comments