some filename fixes for nicer extraction
This commit is contained in:
parent
bc55b5fdda
commit
5e4de6cc5f
4 changed files with 10 additions and 10 deletions
|
@ -56,8 +56,9 @@ public class ConvertImgPDFController {
|
||||||
// returns bytes for image
|
// returns bytes for image
|
||||||
boolean singleImage = singleOrMultiple.equals("single");
|
boolean singleImage = singleOrMultiple.equals("single");
|
||||||
byte[] result = null;
|
byte[] result = null;
|
||||||
|
String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", "");
|
||||||
try {
|
try {
|
||||||
result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toUpperCase(), colorTypeResult, singleImage, Integer.valueOf(dpi));
|
result = PdfUtils.convertFromPdf(pdfBytes, imageFormat.toUpperCase(), colorTypeResult, singleImage, Integer.valueOf(dpi), filename);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
@ -74,7 +75,7 @@ public class ConvertImgPDFController {
|
||||||
ByteArrayResource resource = new ByteArrayResource(result);
|
ByteArrayResource resource = new ByteArrayResource(result);
|
||||||
// return the Resource in the response
|
// return the Resource in the response
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_convertedToImages.zip")
|
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename + "_convertedToImages.zip")
|
||||||
.contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(resource.contentLength()).body(resource);
|
.contentType(MediaType.APPLICATION_OCTET_STREAM).contentLength(resource.contentLength()).body(resource);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ public class ExtractImagesController {
|
||||||
zos.setLevel(Deflater.BEST_COMPRESSION);
|
zos.setLevel(Deflater.BEST_COMPRESSION);
|
||||||
|
|
||||||
int imageIndex = 1;
|
int imageIndex = 1;
|
||||||
|
String filename = file.getOriginalFilename().replaceFirst("[.][^.]+$", "");
|
||||||
int pageNum = 1;
|
int pageNum = 1;
|
||||||
// Iterate over each page
|
// Iterate over each page
|
||||||
for (PDPage page : document.getPages()) {
|
for (PDPage page : document.getPages()) {
|
||||||
|
@ -81,7 +81,7 @@ public class ExtractImagesController {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write image to zip file
|
// Write image to zip file
|
||||||
String imageName = "Image " + imageIndex + " (Page " + pageNum + ")." + format;
|
String imageName = filename + "_" + imageIndex + " (Page " + pageNum + ")." + format;
|
||||||
ZipEntry zipEntry = new ZipEntry(imageName);
|
ZipEntry zipEntry = new ZipEntry(imageName);
|
||||||
zos.putNextEntry(zipEntry);
|
zos.putNextEntry(zipEntry);
|
||||||
|
|
||||||
|
@ -106,7 +106,7 @@ public class ExtractImagesController {
|
||||||
// Create ByteArrayResource from byte array
|
// Create ByteArrayResource from byte array
|
||||||
byte[] zipContents = baos.toByteArray();
|
byte[] zipContents = baos.toByteArray();
|
||||||
|
|
||||||
return PdfUtils.boasToWebResponse(baos, file.getOriginalFilename().replaceFirst("[.][^.]+$", "") + "_extracted-images.zip", MediaType.APPLICATION_OCTET_STREAM);
|
return PdfUtils.boasToWebResponse(baos, filename + "_extracted-images.zip", MediaType.APPLICATION_OCTET_STREAM);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -69,7 +69,7 @@ public class PdfUtils {
|
||||||
return bytesToWebResponse(bytes, docName, MediaType.APPLICATION_PDF);
|
return bytesToWebResponse(bytes, docName, MediaType.APPLICATION_PDF);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] convertFromPdf(byte[] inputStream, String imageType, ImageType colorType, boolean singleImage, int DPI) throws IOException, Exception {
|
public static byte[] convertFromPdf(byte[] inputStream, String imageType, ImageType colorType, boolean singleImage, int DPI, String filename) throws IOException, Exception {
|
||||||
try (PDDocument document = PDDocument.load(new ByteArrayInputStream(inputStream))) {
|
try (PDDocument document = PDDocument.load(new ByteArrayInputStream(inputStream))) {
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
PDFRenderer pdfRenderer = new PDFRenderer(document);
|
||||||
int pageCount = document.getNumberOfPages();
|
int pageCount = document.getNumberOfPages();
|
||||||
|
@ -107,7 +107,7 @@ public class PdfUtils {
|
||||||
ImageIO.write(image, imageType, baosImage);
|
ImageIO.write(image, imageType, baosImage);
|
||||||
|
|
||||||
// Add the image to the zip file
|
// Add the image to the zip file
|
||||||
zos.putNextEntry(new ZipEntry(String.format("page_%d.%s", i + 1, imageType.toLowerCase())));
|
zos.putNextEntry(new ZipEntry(String.format(filename + "_%d.%s", i + 1, imageType.toLowerCase())));
|
||||||
zos.write(baosImage.toByteArray());
|
zos.write(baosImage.toByteArray());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -404,12 +404,11 @@ document.addEventListener("DOMContentLoaded", function () {
|
||||||
if (!contentDisposition) {
|
if (!contentDisposition) {
|
||||||
//throw new Error('Content-Disposition header not found for file ' + i);
|
//throw new Error('Content-Disposition header not found for file ' + i);
|
||||||
} else {
|
} else {
|
||||||
fileName = contentDisposition.split('filename=')[1].replace(/"/g, '');
|
fileName = decodeURIComponent(contentDisposition.split('filename=')[1].replace(/"/g, ''));
|
||||||
}
|
}
|
||||||
console.log('Received response for file ' + i + ': ' + response);
|
console.log('Received response for file ' + i + ': ' + response);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
let blob = await response.blob();
|
let blob = await response.blob();
|
||||||
if (zipFiles) {
|
if (zipFiles) {
|
||||||
// Add the file to the ZIP archive
|
// Add the file to the ZIP archive
|
||||||
|
|
Loading…
Reference in a new issue