Skip to content

Commit

Permalink
Fix/full invert crash 2942 (#2957)
Browse files Browse the repository at this point in the history
# Description of Changes

Please provide a summary of the changes, including:

- What was changed
- Modified the `convertToBufferedImageTpFile` to use
`File.createTempFile()` instead of writing to `"image.png"` in the
current directory.
- This change ensures the file is saved in the default temporary
directory, preventing permission issues.

- Why the change was made
- Previously, the method attempted to save the file in the current
working directory, which caused permission errors
(`java.io.FileNotFoundException: image.png (Permission denied)`).
 
- Any challenges encountered

Closes #2942

---

## Checklist

### General

- [x] I have read the [Contribution
Guidelines](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/CONTRIBUTING.md)
- [x] I have read the [Stirling-PDF Developer
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md)
(if applicable)
- [ ] I have read the [How to add new languages to
Stirling-PDF](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md)
(if applicable)
- [x] I have performed a self-review of my own code
- [x] My changes generate no new warnings

### Documentation

- [ ] I have updated relevant docs on [Stirling-PDF's doc
repo](https://github.com/Stirling-Tools/Stirling-Tools.github.io/blob/main/docs/)
(if functionality has heavily changed)
- [ ] I have read the section [Add New Translation
Tags](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/HowToAddNewLanguage.md#add-new-translation-tags)
(for new translation tags only)

### UI Changes (if applicable)

- [ ] Screenshots or videos demonstrating the UI changes are attached
(e.g., as comments or direct attachments in the PR)

### Testing (if applicable)

- [x] I have tested my changes locally. Refer to the [Testing
Guide](https://github.com/Stirling-Tools/Stirling-PDF/blob/main/DeveloperGuide.md#6-testing)
for more details.
  • Loading branch information
Abdurrahman-shaikh authored Feb 17, 2025
1 parent c1d7217 commit 82b1ab4
Showing 1 changed file with 63 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,69 @@ public InvertFullColorStrategy(MultipartFile file, ReplaceAndInvert replaceAndIn
@Override
public InputStreamResource replace() throws IOException {

// Create a temporary file, with the original filename from the multipart file
File file = Files.createTempFile("temp", getFileInput().getOriginalFilename()).toFile();

// Transfer the content of the multipart file to the file
getFileInput().transferTo(file);

// Load the uploaded PDF
PDDocument document = Loader.loadPDF(file);

// Render each page and invert colors
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); page++) {
BufferedImage image =
pdfRenderer.renderImageWithDPI(page, 300); // Render page at 300 DPI

// Invert the colors
invertImageColors(image);

// Create a new PDPage from the inverted image
PDPage pdPage = document.getPage(page);
PDImageXObject pdImage =
PDImageXObject.createFromFileByContent(
convertToBufferedImageTpFile(image), document);

PDPageContentStream contentStream =
new PDPageContentStream(
document, pdPage, PDPageContentStream.AppendMode.OVERWRITE, true);
contentStream.drawImage(
pdImage,
0,
0,
pdPage.getMediaBox().getWidth(),
pdPage.getMediaBox().getHeight());
contentStream.close();
}

// Save the modified PDF to a ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.save(byteArrayOutputStream);
document.close();
File file = null;
try {
// Create a temporary file, with the original filename from the multipart file
file = Files.createTempFile("temp", getFileInput().getOriginalFilename()).toFile();

// Transfer the content of the multipart file to the file
getFileInput().transferTo(file);

// Load the uploaded PDF
PDDocument document = Loader.loadPDF(file);

// Render each page and invert colors
PDFRenderer pdfRenderer = new PDFRenderer(document);
for (int page = 0; page < document.getNumberOfPages(); page++) {
BufferedImage image =
pdfRenderer.renderImageWithDPI(page, 300); // Render page at 300 DPI

// Invert the colors
invertImageColors(image);

// Create a new PDPage from the inverted image
PDPage pdPage = document.getPage(page);
File tempImageFile = null;
try {
tempImageFile = convertToBufferedImageTpFile(image);
PDImageXObject pdImage =
PDImageXObject.createFromFileByContent(tempImageFile, document);

PDPageContentStream contentStream =
new PDPageContentStream(
document,
pdPage,
PDPageContentStream.AppendMode.OVERWRITE,
true);
contentStream.drawImage(
pdImage,
0,
0,
pdPage.getMediaBox().getWidth(),
pdPage.getMediaBox().getHeight());
contentStream.close();
} finally {
if (tempImageFile != null && tempImageFile.exists()) {
Files.delete(tempImageFile.toPath());
}
}
}

// Prepare the modified PDF for download
ByteArrayInputStream inputStream =
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
InputStreamResource resource = new InputStreamResource(inputStream);
return resource;
// Save the modified PDF to a ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
document.save(byteArrayOutputStream);
document.close();

// Prepare the modified PDF for download
ByteArrayInputStream inputStream =
new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
InputStreamResource resource = new InputStreamResource(inputStream);
return resource;
} finally {
if (file != null && file.exists()) {
Files.delete(file.toPath());
}
}
}

// Method to invert image colors
Expand All @@ -98,7 +115,7 @@ private void invertImageColors(BufferedImage image) {

// Helper method to convert BufferedImage to InputStream
private File convertToBufferedImageTpFile(BufferedImage image) throws IOException {
File file = new File("image.png");
File file = File.createTempFile("image", ".png");
ImageIO.write(image, "png", file);
return file;
}
Expand Down

0 comments on commit 82b1ab4

Please sign in to comment.