prasath-ideas2it / flying-saucer

Automatically exported from code.google.com/p/flying-saucer
0 stars 0 forks source link

no way to embed images that come from a byte array when the pdf generated with xhtml #135

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.
Do the usual 
DocumentBuilder builder = 
DocumentBuilderFactory.newInstance().newDocumentBuilder();
      Document document = builder.parse(new StringBufferInputStream(xhtmlString));
      OutputStream os = new FileOutputStream(outputFile);

      ITextRenderer renderer = new ITextRenderer();
      renderer.setDocument(document, null);
      renderer.layout();
      renderer.createPDF(os);

      os.close();

2. but in the xhtmlString, there's <img src/> tag, that only takes a local file 

What is the expected output? What do you see instead?
Need a way to take in a byte array.  In iText it uses an ImageProvider to let 
you get around this.

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by nep...@gmail.com on 25 Mar 2011 at 7:46

GoogleCodeExporter commented 9 years ago
Try this. It assumes that your images are on the classpath:

      String htmlString = readFileAsString(PDFRender.class.getResource("test.html").getFile());
      ITextRenderer renderer = new ITextRenderer();
      ResourceLoaderUserAgent callback = new ResourceLoaderUserAgent(renderer.getOutputDevice());
      callback.setSharedContext(renderer.getSharedContext());
      renderer.getSharedContext().setUserAgentCallback(callback);

      final ByteArrayInputStream htmlIn = new ByteArrayInputStream(htmlString.getBytes());
      final ByteArrayOutputStream xhtmlErr = new ByteArrayOutputStream();
      final PrintWriter printErr = new PrintWriter(xhtmlErr);

      final Tidy tidy = new Tidy();
      tidy.setErrout(printErr);
      Document doc = tidy.parseDOM(htmlIn, null);
      renderer.setDocument(doc, url);
      renderer.layout();
      renderer.createPDF(os);

      os.close();
      os = null;
    } finally {
      if(os != null) {
        try {
          os.close();
        } catch(IOException e) {
          // ignore
        }
      }
    }

  }

  private static class ResourceLoaderUserAgent extends ITextUserAgent {
    public ResourceLoaderUserAgent(ITextOutputDevice outputDevice) {
      super(outputDevice);
    }

    protected InputStream resolveAndOpenStream(String uri) {
      System.out.println("IN resolveAndOpenStream() " + uri);
      InputStream is = super.resolveAndOpenStream(uri);
      if(is == null) {
    //Assumes that the last part is the file name and that the image is on the classpath
        String[] split = uri.split("/");
        String lastPart = split[split.length - 1];
        //Could extend this to look in more places
        is = ResourceLoaderUserAgent.class.getResourceAsStream(lastPart);
      }
      return is;
    }
  }

  private static String readFileAsString(String filePath) throws java.io.IOException {
    byte[] buffer = new byte[(int)new File(filePath).length()];
    BufferedInputStream f = null;
    try {
      f = new BufferedInputStream(new FileInputStream(filePath));
      f.read(buffer);
    } finally {
      if(f != null) {
        try {
          f.close();
        } catch(IOException ignored) {
        }
      }
    }
    return new String(buffer);
  }

Original comment by opticyc...@gmail.com on 2 May 2011 at 1:45