0

I have ".svg" formatted image, I want to include those images in "PDF" by using 'itext' library and 'batik' library to write PDF file and 'itext' supports only for jpeg/png/tiff/gif image formats.

 try{
        PdfWriter writer = PdfWriter.getInstance(document,
        new FileOutputStream("E://svg.pdf"));
        document.open();
        document.add(new Paragraph("SVG Example"));

        int width = 250;
        int height = 250;
        PdfContentByte cb = writer.getDirectContent();
        PdfTemplate template = cb.createTemplate(width,height);         
        Graphics2D g2 = template.createGraphics(width,height);          

        PrintTranscoder prm = new PrintTranscoder();
        TranscoderInput ti = new TranscoderInput("E://firoz_trend.xml");//also tried E://firoz_trend.svg
        prm.transcode(ti, null);

        PageFormat pg = new PageFormat();
        Paper pp= new Paper();
        pp.setSize(width, height);
        pp.setImageableArea(0, 0, width, height);
        pg.setPaper(pp);
        prm.print(g2, pg, 0); 
        g2.dispose(); 

        ImgTemplate img = new ImgTemplate(template);           
        document.add(img);

  } catch (Exception e) {
     System.out.println("CreatePdf "+e);
  }

It gives an exception org.apache.batik.transcoder.TranscoderException: null Enclosed Exception: Unable to make sense of URL for connection please help me for this

  • Find a tool that can interpret SVG (Batik, Salamander,...) and that can render an SVG to a `Graphics2D` object. Use that library to render the SVG to iText's `PdfGraphics2D` object and you're done. There are some examples in the official documentation, but they are somewhat outdated because they depend on a mighty old Batik version. – Bruno Lowagie Jan 20 '15 at 07:59
  • thank you for your response please can you give me the url that should i refer –  Jan 20 '15 at 08:16

1 Answers1

1

The constructor of TranscoderInput that takes a String expects a URI, and you're passing in a filename, so it fails.

Open the file yourself before passing it to the constructor:

TranscoderInput ti = new TranscoderInput(new FileInputStream("E:\\firoz_trend.svg"));

(Note, in your own code, make sure that the file is closed at the end of your method to prevent file-descriptor leaks)

Erwin Bolwidt
  • 30,799
  • 15
  • 56
  • 79
  • Thanks for solution.... Now this code works fine upto prm.print(g2, pg, 0); but from this statement can't execute remaining statement. please give me any suggestion. –  Jan 21 '15 at 10:01
  • In particular the parts about voting for answers and the accepted answer. – Erwin Bolwidt Jan 21 '15 at 14:33