Base64 图像转换为 PDF 并作为附件添加到 java 邮件
Base64 Image Conversion to PDF and add as an attachment to java mail
我有一个图像的 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);
%>
如果我做错了什么请告诉我。
提前致谢。
我做到了。非常感谢您的建议:)
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);
我有一个图像的 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);
%>
如果我做错了什么请告诉我。 提前致谢。
我做到了。非常感谢您的建议:)
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);