From f58fb991ed4389f811c82c209d7993a2a47c224d Mon Sep 17 00:00:00 2001 From: Benoit DUMONT Date: Sat, 12 Aug 2023 09:46:16 +0200 Subject: [PATCH] Fixed IO Exception. #2475 --- .../core/service/pdf/impl/PDFService.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java b/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java index 6795de5f88..ed9909ffc0 100644 --- a/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java +++ b/source/src/main/java/org/cerberus/core/service/pdf/impl/PDFService.java @@ -381,6 +381,8 @@ public String generatePdf(Tag tag) throws FileNotFoundException { return dest; } catch (ParseException | CerberusException | JSONException ex) { LOG.error(ex, ex); + } catch (Exception ex) { + LOG.error(ex, ex); } return null; } @@ -395,11 +397,11 @@ private Text getTextFromString(String text, int fontSize, boolean isBold) { } } - private Table getImageTable(List controlFileList, String mediaPath) { + private Table getImageTable(List fileList, String mediaPath) { Table tableTmp = null; // We count the nb of images in the file list. int nbImages = 0; - for (TestCaseExecutionFile controlFile : controlFileList) { + for (TestCaseExecutionFile controlFile : fileList) { if (controlFile.isImage()) { nbImages++; } @@ -408,18 +410,28 @@ private Table getImageTable(List controlFileList, String if (nbImages > 0) { tableTmp = new Table(new float[]{150, 500}); - for (TestCaseExecutionFile controlFile : controlFileList) { + for (TestCaseExecutionFile controlFile : fileList) { if (controlFile.isImage()) { // Load screenshots to pdf. ImageData imageData; try { - imageData = ImageDataFactory.create(mediaPath + controlFile.getFileName()); - Image image = new Image(imageData).scaleToFit(500, 200); - tableTmp.addCell(new Cell().add(new Paragraph().add(getTextFromString(controlFile.getFileDesc(), 7, false)).setTextAlignment(TextAlignment.LEFT)) - .setBorder(Border.NO_BORDER).setVerticalAlignment(VerticalAlignment.MIDDLE)) - .addCell(new Cell().add(image.setBorder(Border.NO_BORDER).setHorizontalAlignment(HorizontalAlignment.RIGHT)).setBorder(Border.NO_BORDER)); + File f = new File(mediaPath + controlFile.getFileName()); + if (f.exists()) { + imageData = ImageDataFactory.create(mediaPath + controlFile.getFileName()); + Image image = new Image(imageData).scaleToFit(500, 200); + tableTmp.addCell(new Cell().add(new Paragraph().add(getTextFromString(controlFile.getFileDesc(), 7, false)).setTextAlignment(TextAlignment.LEFT)) + .setBorder(Border.NO_BORDER).setVerticalAlignment(VerticalAlignment.MIDDLE)) + .addCell(new Cell().add(image.setBorder(Border.NO_BORDER).setHorizontalAlignment(HorizontalAlignment.RIGHT)).setBorder(Border.NO_BORDER)); + + }else{ + tableTmp.addCell(new Cell().add(new Paragraph().add(getTextFromString(controlFile.getFileDesc(), 7, false)).setTextAlignment(TextAlignment.LEFT)) + .setBorder(Border.NO_BORDER).setVerticalAlignment(VerticalAlignment.MIDDLE)) + .addCell(new Cell().add(new Paragraph().add(getTextFromString("File no longuer exist !!!", 7, false)).setTextAlignment(TextAlignment.RIGHT)).setBorder(Border.NO_BORDER)); + } } catch (MalformedURLException ex) { LOG.error(ex, ex); + } catch (Exception ex) { + LOG.error(ex, ex); } }