public void generatePdfFromEmail(File inputFile, OutputStream outputStream, String filename, List<Long> pages) {
try {
MailMessage mailMessage;
String extension = FilenameUtils.getExtension(filename);
if (StringUtils.equalsIgnoreCase(extension, "eml") || StringUtils.equalsIgnoreCase(extension, "emlx")) {
mailMessage = MailMessage.load(inputFile.getAbsolutePath());
} else if (StringUtils.equalsIgnoreCase(extension, "msg")) {
try (InputStream msgInput = new FileInputStream(inputFile)) {
MapiMessage mapi = MapiMessage.load(msgInput);
mailMessage = mapi.toMailMessage(new MailConversionOptions());
}
} else {
throw new RuntimeException("Unsupported email file format: " + extension);
}
File tempMhtml = File.createTempFile("email-convert-", ".mhtml");
try (OutputStream tempOut = new FileOutputStream(tempMhtml)) {
mailMessage.save(tempOut, SaveOptions.getDefaultMhtml());
}
try (InputStream inputStream = new FileInputStream(tempMhtml)) {
Document doc = new Document(inputStream);
pages.add((long) doc.getPageCount());
double maxContentWidth = 0;
NodeCollection<Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
for (Shape shape : shapes) {
if (shape.hasImage()) {
maxContentWidth = Math.max(maxContentWidth, shape.getWidth());
}
}
NodeCollection<Table> tables = doc.getChildNodes(NodeType.TABLE, true);
for (Table table : tables) {
if (table.getPreferredWidth() != null)
maxContentWidth = Math.max(maxContentWidth, table.getPreferredWidth().getValue());
}
boolean useLandscape = maxContentWidth > 555;
for (Section section : doc.getSections()) {
PageSetup ps = section.getPageSetup();
ps.setPaperSize(PaperSize.A4);
ps.setOrientation(useLandscape ? Orientation.LANDSCAPE : Orientation.PORTRAIT);
ps.setTopMargin(20);
ps.setBottomMargin(20);
ps.setLeftMargin(20);
ps.setRightMargin(20);
}
for (Table table : tables) {
table.autoFit(AutoFitBehavior.AUTO_FIT_TO_WINDOW);
}
for (Shape shape : shapes) {
if (!shape.hasImage()) continue;
PageSetup ps = shape.getParentParagraph().getParentSection().getPageSetup();
double freeWidth = shape.isInline()
? ps.getPageWidth() - ps.getLeftMargin() - ps.getRightMargin()
: ps.getPageWidth();
double freeHeight = shape.isInline()
? ps.getPageHeight() - ps.getTopMargin() - ps.getBottomMargin()
: ps.getPageHeight();
ImageSize size = shape.getImageData().getImageSize();
boolean tooBig = size.getWidthPoints() > freeWidth
|| size.getHeightPoints() > freeHeight
|| shape.getWidth() > freeWidth
|| shape.getHeight() > freeHeight;
if (tooBig) {
double ratio = freeWidth / size.getWidthPoints();
if (size.getHeightPoints() * ratio > freeHeight) {
ratio = freeHeight / size.getHeightPoints();
}
shape.setAspectRatioLocked(true);
shape.setWidth(size.getWidthPoints() * ratio);
shape.setHeight(size.getHeightPoints() * ratio);
}
}
PdfSaveOptions pdfOptions = new PdfSaveOptions();
pdfOptions.setEmbedFullFonts(true);
pdfOptions.setUseHighQualityRendering(true);
pdfOptions.setImageColorSpaceExportMode(PdfImageColorSpaceExportMode.AUTO);
pdfOptions.setTextCompression(PdfTextCompression.NONE);
pdfOptions.setJpegQuality(100);
doc.save(outputStream, pdfOptions);
} finally {
MediaUtils.silentlyDeleteTempFile(tempMhtml);
}
} catch (Exception ex) {
throw new RuntimeException("Failed to convert email to PDF: " + ex.getMessage(), ex);
}
}`Preformatted text`
Hello @Debasisha,
Could you please share a sample .msg file where the content is being cut off? This will help us reproduce and investigate the issue.
Thank you.
Hi @margarita.samodurova thanks a lot for quick response.
actually i posted my query two time in other place Cutting off right side after converting email to pdf
here @alexey.noskov helped me, the issue is resolved now.
Thank you for letting us know, and feel free to reach out again if you have any other questions.