Hi. I have an evalation licence for Aspose Total. I have the following requirement:
Convert an Email with Attachments to PDF; the attachments can be a number of different formats, but in my current scenario, it is a PDF document.
I have tried the following approach:
- Convert the Email to bytes
var input = new java.io.ByteArrayInputStream(bytes);
- Create a Msg object
var msg = new com.aspose.email.MailMessage();
msg = msg.load(input);
- Create PDF from Msg
var output = new java.io.ByteArrayOutputStream();
msg.save("HTMLOutput.html", com.aspose.email.SaveOptions.getDefaultMhtml());
var pdfDoc = new com.aspose.words.Document("HTMLOutput.html");
- Create a MapiEmail message
var outlookMessageFile = new com.aspose.email.MapiMessage();
input = new java.io.ByteArrayInputStream(bytes);
outlookMessageFile = outlookMessageFile.fromMailMessage(input);
- Get the attachments and save to Disc
attachments = outlookMessageFile.getAttachments();
for (var i = 0; i < outlookMessageFile.getAttachments().size(); i++)
{
var outlookMessageAttachment = outlookMessageFile.getAttachments().get_Item(i);
outlookMessageAttachment.save(FILE_PATH_OUT + outlookMessageAttachment.getDisplayName());
}
- Save the PDF Document
var path = FILE_PATH_OUT + 'test.pdf';
pdfDoc.save(path);
However, when I try to add the PDF attachment to the PDF Document, I get an error:
Here is the code:
var fileSpecification = new com.aspose.pdf.FileSpecification(FILE_PATH_OUT + outlookMessageAttachment.getDisplayName());
pdfDoc.getEmbeddedFiles().add(fileSpecification);
Here is the error:
TypeError: Cannot find function getEmbeddedFiles in object Document.
I can’t see a getEmbeddedFiles method in com.aspose.words.Document, but I can see one in com.aspose.pdf.Document
But when I modify the package name to use .pdf instead of .word, I get a different error:
JavaException: com.aspose.pdf.exceptions.InvalidPdfFileFormatException: Incorrect file header
I think this error relates to the following line of code:
aspose.pdf.Document("HTMLOutput.html");
Can you please help?