mirror of
				https://github.com/Frooodle/Stirling-PDF.git
				synced 2025-11-01 01:21:18 +01:00 
			
		
		
		
	Merge pull request #589 from sbplat/main
fix: sequentially convert each pdf page into a BufferedImage to avoid getting out of memory errors for large pdf files
This commit is contained in:
		
						commit
						e5bdd52b7c
					
				@ -203,12 +203,6 @@ public class PdfUtils {
 | 
			
		||||
        try (PDDocument document = PDDocument.load(new ByteArrayInputStream(inputStream))) {
 | 
			
		||||
            PDFRenderer pdfRenderer = new PDFRenderer(document);
 | 
			
		||||
            int pageCount = document.getNumberOfPages();
 | 
			
		||||
            List<BufferedImage> images = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
            // Create images of all pages
 | 
			
		||||
            for (int i = 0; i < pageCount; i++) {
 | 
			
		||||
                images.add(pdfRenderer.renderImageWithDPI(i, DPI, colorType));
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Create a ByteArrayOutputStream to save the image(s) to
 | 
			
		||||
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
 | 
			
		||||
@ -226,8 +220,8 @@ public class PdfUtils {
 | 
			
		||||
                        writer.setOutput(ios);
 | 
			
		||||
                        writer.prepareWriteSequence(null);
 | 
			
		||||
 | 
			
		||||
                        for (int i = 0; i < images.size(); ++i) {
 | 
			
		||||
                            BufferedImage image = images.get(i);
 | 
			
		||||
                        for (int i = 0; i < pageCount; ++i) {
 | 
			
		||||
                            BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType);
 | 
			
		||||
                            writer.writeToSequence(new IIOImage(image, null, null), param);
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
@ -237,11 +231,15 @@ public class PdfUtils {
 | 
			
		||||
                    writer.dispose();
 | 
			
		||||
                } else {
 | 
			
		||||
                    // Combine all images into a single big image
 | 
			
		||||
                    BufferedImage combined = new BufferedImage(images.get(0).getWidth(), images.get(0).getHeight() * pageCount, BufferedImage.TYPE_INT_RGB);
 | 
			
		||||
                    BufferedImage image = pdfRenderer.renderImageWithDPI(0, DPI, colorType);
 | 
			
		||||
                    BufferedImage combined = new BufferedImage(image.getWidth(), image.getHeight() * pageCount, BufferedImage.TYPE_INT_RGB);
 | 
			
		||||
                    Graphics g = combined.getGraphics();
 | 
			
		||||
 | 
			
		||||
                    for (int i = 0; i < images.size(); i++) {
 | 
			
		||||
                        g.drawImage(images.get(i), 0, i * images.get(0).getHeight(), null);
 | 
			
		||||
                    for (int i = 0; i < pageCount; ++i) {
 | 
			
		||||
                        if (i != 0) {
 | 
			
		||||
                            image = pdfRenderer.renderImageWithDPI(i, DPI, colorType);
 | 
			
		||||
			}
 | 
			
		||||
                        g.drawImage(image, 0, i * image.getHeight(), null);
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Write the image to the output stream
 | 
			
		||||
@ -253,8 +251,8 @@ public class PdfUtils {
 | 
			
		||||
            } else {
 | 
			
		||||
                // Zip the images and return as byte array
 | 
			
		||||
                try (ZipOutputStream zos = new ZipOutputStream(baos)) {
 | 
			
		||||
                    for (int i = 0; i < images.size(); i++) {
 | 
			
		||||
                        BufferedImage image = images.get(i);
 | 
			
		||||
                    for (int i = 0; i < pageCount; ++i) {
 | 
			
		||||
                        BufferedImage image = pdfRenderer.renderImageWithDPI(i, DPI, colorType);
 | 
			
		||||
                        try (ByteArrayOutputStream baosImage = new ByteArrayOutputStream()) {
 | 
			
		||||
                            ImageIO.write(image, imageType, baosImage);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user