0

I am writing in java, but want to create a dynamic HTML-page for the users. I am using Lowagie to create the document with HTML. I do manage to present the html, but my picture is empty. It just contain the picture-border. Can anyone help me with this? Or tell me another way of creating HTML-pages (preferable by using ByteArrauOutputstream or other outpustreams to display the content).

The code is as follows:

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String orderId = request.getParameter("id1");
    String telephone = request.getParameter("id2");
    response.setHeader("Expires", EXPIRES);

    response.setContentType(CONTENT_TYPE);

    ServletOutputStream out = null;
    ByteArrayOutputStream baos = null;
    try {
        baos = getHtmlTicket(orderId, telephone);

        response.setContentLength(baos.size());
        out = response.getOutputStream();
        baos.writeTo(out);

    }
    catch (Exception e) {
        log.error(e.getMessage(), e);

    }
    finally {
        if (out != null) {
            try {
                out.flush();
            }
            catch (Exception e) {
                log.debug(e.getMessage(), e);
            }
        }
        if (baos != null) {
            try {
                baos.flush();
            }
            catch (Exception e) {
                log.debug(e.getMessage(), e);
            }
        }
        if (out != null) {
            try {
                out.close();
            }
            catch (Exception e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (baos != null) {
            try {
                baos.close();
            }
            catch (Exception e) {
                log.warn(e.getMessage(), e);
            }
        }
    }



  public ByteArrayOutputStream getHtmlTicket(String orderId, String   telephoneTest) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    Document document = new Document();
    Order order = orderService.getOrder(Integer.parseInt(orderId));

    String fileType = "png";
    String filePath = "html/picture.png";


    File myFile = new File(filePath);
    try {
        HtmlWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("Hello World"));

        Image fileImage = Image.getInstance(filePath);

        document.add(fileImage);
        document.add(new Paragraph("osv"));
    }
    catch (DocumentException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }


    document.close();

    return baos;
 }
TorK
  • 567
  • 2
  • 10
  • 27
  • When you say *I am using "Lowagie"*, you frighten me, because **I am Lowagie** and I don't like the idea of being used ;-) Jokes aside, please understand that `HtmlWriter` was not meant to be used in production to create HTML. Its purpose was to have an output format that could easily be debugged. During the development phase of a project, I would create a PDF and an HTML file simultaneously with the purpose to create a PDF. However, a PDF is hard to debug, so when something went wrong, I looked into the HTML to see what went wrong where. The HTML files ware my "debug" files. – Bruno Lowagie Jan 28 '15 at 10:21
  • In more recent versions of iText (you are using [a version that should no longer be used](http://stackoverflow.com/questions/25696851/can-itext-2-1-7-or-earlier-can-be-used-commercially)), PDF creation is more stable and we removed `HtmlWriter` because (1) there was no longer a need for it, and (2) we wanted to avoid that people used it to create HTML for a web site (which was never the intention, the quality of the HTML was just too poor for that purpose). – Bruno Lowagie Jan 28 '15 at 10:24
  • Thank you so much for your answer :) I will not use iText for HTML-page-creation from now on :) But since you are in the business of HTML/PDF creation ;) do you have any other libraris I could use for this? All I need is to present HTML and a picture, built in java :) – TorK Jan 28 '15 at 11:07
  • If it's that simple, why use a library? Why not a simple HTML file with simple placeholders that you replace in Java using simple text manipulation methods. Introducing a Java library for such a simple requirement is overkill. Also: if you're working in a web context, why not use JSP? – Bruno Lowagie Jan 28 '15 at 11:24

0 Answers0