我有一个图像的base64代码,现在要求使用base64创建一个带有图像的PDF(使用base64代码)并将其作为电子邮件附件附加,而不将其保存在任何物理位置。 这就是我到目前为止所尝试的,我将pdf作为附件,但说它已被破坏。
<%
String b64Image=request.getParameter("img_val");
b64Image = b64Image.replace("data:image/png;base64,", "");
b64Image = b64Image.replace(" ", "+");
/*setting all the email properties
.....
.....
*/
BufferedImage image = ImageIO.read(new ByteArrayInputStream(decodedBytes));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter.getInstance(document, baos);
document.open();
Image image = Image.getInstance(decodedBytes);
image.scalePercent(50);
document.add(image);
document.close();
/*after this how to add as email attachment iwas not sure so tried with below code*/
/*with the below code im getting attachment, when i open pdf it is saying as corrupted*/
byte[] decoded = org.apache.commons.codec.binary.Base64.decodeBase64(b64Image.getBytes());
pdfBodyPart.setDataHandler(new DataHandler(new ByteArrayDataSource(decoded, "application/pdf")));
pdfBodyPart.setFileName("Report.pdf");
pdfBodyPart.setDisposition(MimeBodyPart.ATTACHMENT);
multipart.addBodyPart(pdfBodyPart);
message.setContent(multipart);
%>
如果我做错了,请告诉我。 在此先感谢。
答案 0 :(得分:0)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Document document = new Document(PageSize.A4, 10, 10, 10, 10);
PdfWriter.getInstance(document, baos);
document.open();
Image image = Image.getInstance(decodedBytes);
image.scalePercent(50);
document.add(image);
document.close();
DataSource pdfds = new ByteArrayDataSource(baos.toByteArray(), "application/pdf");
pdfBodyPart.setDataHandler(new DataHandler(pdfds));
//pngBodyPart.setHeader("Content-ID", "<dashboard>");
//pngBodyPart.setDisposition(MimeBodyPart.INLINE);
pdfBodyPart.setFileName("AnnotatedDashboard.pdf");
multipart.addBodyPart(pdfBodyPart);
// Send the complete message parts
message.setContent(multipart);