html到pdf不会显示图像文件夹中的图像

时间:2019-05-07 12:13:53

标签: java html pdf

我正在创建一个.jar文件以插入bpm上的java连接器。 这个想法是让.jar从html生成pdf,而html使用来自Image文件夹的图像并作为电子邮件附件发送。

从eclipse运行时,PDF会发送html和图像,一旦我导出.jar并尝试在eclipse之外使用它,它就不会显示图像。

        System.out.println("I am about to get resource image001");
        BufferedImage imageone = ImageIO.read(new FileInputStream("Images/image001.jpg"));       
        File outputfile = new File("Images/image001.jpg");
        ImageIO.write(imageone, "jpg", outputfile);          
        System.out.println("image 1 " + outputfile.toURL());
        String one = "Images/image001.jpg";
        System.out.println("got image 1");


        System.out.println("I am about to get rousrce image002");
        //BufferedImage bufferedImage2 = ImageIO.read(jamesNew.class.getResource("/Images/image002.gif"));
        BufferedImage imagwtwo = ImageIO.read(new FileInputStream("Images/image002.gif"));
        File outputfile1 = new File("image002.gif");
        ImageIO.write(imagwtwo, "gif", outputfile1);
        System.out.println("image 2 " + outputfile1.toURL());
        String two = "Images/image002.gif";
        System.out.println("got image 2");

        System.out.println("I am about to get rousrce image003");
        //BufferedImage bufferedImage3 = ImageIO.read(jamesNew.class.getResource("/Images/image003.gif"));
        BufferedImage imagwthree = ImageIO.read(new FileInputStream("Images/image003.gif"));
        File outputfile2 = new File("image003.gif");
        ImageIO.write(imagwthree, "gif", outputfile2);  
        System.out.println("image 3 " + outputfile2.toURL());
        String three = "Images/image003.gif";
        System.out.println("got image 3");

        System.out.println("I am about to get rousrce image004");
        //BufferedImage bufferedImage4 = ImageIO.read(jamesNew.class.getResource("/Images/image006.gif"));
        BufferedImage imagefour = ImageIO.read(new FileInputStream("Images/image006.gif"));
        File outputfile3 = new File("image006.gif");
        ImageIO.write(imagefour, "gif", outputfile3);   
        System.out.println("image 4 " + outputfile3.toURL());
        String four = "Images/image006.gif";
        System.out.println("got image 4");

        System.out.println("I am about to get rousrce image005");
        //BufferedImage bufferedImage5 = ImageIO.read(jamesNew.class.getResource("/Images/logo1.png"));
        BufferedImage imagwfive = ImageIO.read(new FileInputStream("Images/logo1.png"));
        File outputfile4 = new File("logo1.png");
        ImageIO.write(imagwfive, "png", outputfile4);
        System.out.println("image 5 " + outputfile4.toURL());
        String five = "Images/logo1.png";
        System.out.println("got image 5");


        System.out.println("CLASS PATHS SET..........");





        String named = null;
        String total = null;
        String completehtml1 = null;
        String style = "<style type=\""+"text/css" + "\" >" + ".centerImage { text-align:center; display:block; } </style>";
        String feed = "<html><body>";
        System.out.println("STRINGS SET..........");

        completehtml = feed + style + "<div style=\"text-align:center;\">" + "<img src=\"" + outputfile + "\" />" + "<p></p>" + "<img src=\"" + outputfile1 + "\" +  style=\"width:400px;height:150px;" + "\" />" + "<p><h3 style=\"text-align:center;"+"\">" + "THIS IS TO CERTIFY" +  "</h3></p>" + "<p><h3 style=\"text-align:center;"+"\">"+ user + "</h3></p>" + "<p>" + "</p>" + "<p>" + "<img src=\"" + outputfile2 + "\" />" + "</p>" + "<p>" + "</p>" + "<p><h3 style=\"text-align:center;"+"\">"+ dateTXT + " " + date + "</h3></p>" + "<p><h3 style=\"text-align:center;"+"\">" + "SIGNED" + "</h3></p>" + "<p>" + "</p>" + "<p>" + "<img src=\"" + outputfile3 + "\" + style=\"width:150px;height:150px;"+"\" />" + "</p>" + "<p>" + "</p>" + "<p>" + "<img src=\"" + outputfile4 + "\" />" + "</p>" + "<p></p>" + "<p></p>" + "</div>" + htmlend;
        total = completehtml + named + completehtml1 + htmlend;


        System.out.println("complete HTML" + completehtml);
        System.out.println("STARTED!!!");
        ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
        ObjectOutputStream file = new ObjectOutputStream(baos1);

        Document document = new Document();
        PdfWriter pdfwriter = PdfWriter.getInstance(document, baos1);      
        document.open();
        System.out.println("DOCUMENT OPEN");
        document.addAuthor("James Parish");

        XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
        worker.parseXHtml(pdfwriter, document, new StringReader(completehtml));
       // worker.parseXHtml(pdfwriter, document, new FileInputStream("Images/image001.jpg"));


        System.out.println("NEW PARAGRAPH");
        System.out.println("PARAGRAPH ADDED");

        document.close();
        System.out.println("CLOSED DOCUMENT!");
        file.close();
        System.out.println("CLOSED FILE");
        System.out.println("pdf is" + file);
        System.out.println("ENDED!!!");



        Email from = new Email(msgfrom);
        Email to = new Email(msgto);
        Content content = new Content("text/plain", msgbody);
        Mail mail = new Mail(from, subject, to, content);

        ByteArrayOutputStream fileData = null;
        byte[] testing  = baos1.toByteArray();

        try {
            fileData = baos1;

        Attachments attachments3 = new Attachments();
        Base64 x = new Base64();
        String imageDataString = Base64.encodeBase64String(testing);
        attachments3.setContent(imageDataString);

        attachments3.setType("application/pdf");//"application/pdf"
        attachments3.setFilename("Certificate.pdf");
        attachments3.setDisposition("attachment");
        attachments3.setContentId("Banner");
        mail.addAttachments(attachments3);

        System.out.println("BASE64 ENCODED ATTACHMENT " + imageDataString);

0 个答案:

没有答案