You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Jared Davis <ja...@davisprogramming.com> on 2005/04/04 18:27:20 UTC

Newbie: Svg -> PDF Text producing Huge Files

I'm using Batik and Itext to convert svg files to pdf. It works great with
one problem: the resulting files are large. I think it is due to the font
being drawn as shapes. 

Is there a way to keep the svg text as text through the conversion? 

I didn't use FOP since the readme states it doesn't handle transparency
which I require.

Are there any Transcoding hints?

The Netbeans project and output pdf may be downloaded at mo e . d
avisprogramming.com / s vgpdfsize.zip

Here is the test code - used batik-cvs-05-04-03 and iText (itext-paulo-153)
jars.

When the text is "Hello" the filesize is 1661.
When the text is "HelloAbcde" the filesize is 2569 which works out to about
200 bytes per character. 

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.awt.Graphics2D;
import java.awt.print.*;
import java.io.*;
import org.apache.batik.transcoder.*;
import org.apache.batik.transcoder.print.*;

public class Main {
    
    /** Creates a new instance of Main */
    final String svg = "<?xml version='1.0' encoding='ISO-8859-1'
standalone='no'?>" +
            "<svg width='1188' height='756'
xmlns='http://www.w3.org/2000/svg'>" +
            " <g transform='translate(50,50) scale(5,5)'>" +
            "  <rect x='0.0' y='0' width='200.0' height='100.0'
style='fill:red; stroke:black; stroke-width:.2; fill-opacity: 0.2;' />" +
            "  <rect x='20.0' y='20' width='100.0' height='50.0'
style='fill:red; stroke:black; stroke-width:.2; fill-opacity: 0.4;' />" +
            "  <rect x='40.0' y='40' width='50.0' height='10.0'
style='fill:blue; stroke:black; stroke-width:.2; fill-opacity: 0.1;' />" +
            "  <text x='45.0' y='45' >HelloAbcde</text>" +
            " </g>" +
            "</svg>";
    
    public Main() {
        com.lowagie.text.Document document = new
com.lowagie.text.Document(PageSize._11X17.rotate());
        try {
            PdfWriter writer = PdfWriter.getInstance(document,	new
FileOutputStream("svgsize.pdf"));
            document.open();
            PdfContentByte cb = writer.getDirectContent();
            cb.saveState();
            cb.concatCTM(1.0f, 0, 0, 1.0f , 36, 0 );
            Graphics2D g2 = cb.createGraphics(1188, 756);
            PrintTranscoder prm = new PrintTranscoder();
            TranscoderInput ti = new TranscoderInput( new
StringReader(svg));
            prm.transcode(ti, null);
            PageFormat pg = new PageFormat();
            Paper pp= new Paper();
            pp.setSize( 1188, 756);
            pp.setImageableArea(0, 0, 1188, 756);
            pg.setPaper(pp);
            prm.print(g2, pg, 0);
            g2.dispose();
            cb.restoreState();
            document.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
     }
    public static void main(String[] args) {
        Main m = new Main();
    }
 }


Jared Davis
Davis Programming, Inc



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Newbie: Svg -> PDF Text producing Huge Files

Posted by Thomas DeWeese <Th...@Kodak.com>.
Jared Davis wrote:
> Would it be possible/safe to do something like the following?

   Sure you could do that but I was planning to add
createSubContext to the BridgeContext class it's self,
rather than introducing the interface.  The interface
approach is useful for adding methods to an existing
interface where you don't want to break existing
implementations of the interface but that isn't a
concern with concrete classes.

> 
> Interface CreateSubContext {
>   BridgeContext createSubContext();
> }
> ////////////
> Class BridgeContext .... Implements CreateSubContext 
> {...
>   Public BridgeContext createSubContext() {
>        BridgeContext subCtx = new BridgeContext(getUserAgent(), 
>                                                 getDocumentLoader());
>        subCtx.setGVTBuilder(getGVTBuilder());
>        subCtx.setTextPainter(getTextPainter()); 
>        return subCtx;
>   }
> }
> //////////
>          if (eng != null) {
>              subCtx = (BridgeContext)eng.getCSSContext();
>          } else {
>              if (ctx instanceof CreateSubContext) {
>                 subCtx = (CreateSubContext)ctx.createSubContext();
>              } else {
>                 subCtx = new BridgeContext(ctx.getUserAgent(), 
>                                            ctx.getDocumentLoader());
>                 subCtx.setGVTBuilder(ctx.getGVTBuilder());
>              }
>              subCtx.setDocument(imgDocument);
>              subCtx.initializeDocument(imgDocument);
>          }
> //////////
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


RE: Newbie: Svg -> PDF Text producing Huge Files

Posted by Jared Davis <ja...@davisprogramming.com>.
Would it be possible/safe to do something like the following?

Interface CreateSubContext {
  BridgeContext createSubContext();
}
////////////
Class BridgeContext .... Implements CreateSubContext 
{...
  Public BridgeContext createSubContext() {
       BridgeContext subCtx = new BridgeContext(getUserAgent(), 
                                                getDocumentLoader());
       subCtx.setGVTBuilder(getGVTBuilder());
       subCtx.setTextPainter(getTextPainter()); 
       return subCtx;
  }
}
//////////
         if (eng != null) {
             subCtx = (BridgeContext)eng.getCSSContext();
         } else {
             if (ctx instanceof CreateSubContext) {
                subCtx = (CreateSubContext)ctx.createSubContext();
             } else {
                subCtx = new BridgeContext(ctx.getUserAgent(), 
                                           ctx.getDocumentLoader());
                subCtx.setGVTBuilder(ctx.getGVTBuilder());
             }
             subCtx.setDocument(imgDocument);
             subCtx.initializeDocument(imgDocument);
         }
//////////





---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


Re: Newbie: Svg -> PDF Text producing Huge Files

Posted by Thomas DeWeese <Th...@Kodak.com>.
Hi Jared,

Jared Davis wrote:

> Why does SVGImageElementBridge createSVGImageNode ignore the current
> TextPainter?

   This would be an oversight.  I'll fix it but I am wary about trying to
do this right before the release.

> Extending PrintTranscoder to set the TextPainter to PDFTextPainter works -
> sort of. The StrokingTextPainter class is still used on svg files referenced
> by image tags.


>    protected GraphicsNode createSVGImageNode(BridgeContext ctx,
>                                               Element e,
>                                               SVGDocument imgDocument) {
>         CSSEngine eng = ((SVGOMDocument)imgDocument).getCSSEngine();
>         if (eng != null) {
>             subCtx = (BridgeContext)eng.getCSSContext();
>         } else {
>             subCtx = new BridgeContext(ctx.getUserAgent(), 
>                                        ctx.getDocumentLoader());
>             subCtx.setGVTBuilder(ctx.getGVTBuilder());
> //
>             subCtx.setTextPainter(ctx.getTextPainter());  // Added Test Code
> //
>             subCtx.setDocument(imgDocument);
>             subCtx.initializeDocument(imgDocument);
>         }
> 
> ...
> 
> 
>  public class PrintTranscoderWithRealFonts extends PrintTranscoder {
>     
>     /** Creates a new instance of PrintTranscoderWithRealFonts */
>     public PrintTranscoderWithRealFonts() {
>     }
>        protected BridgeContext createBridgeContext() {
>         BridgeContext ctx = new BridgeContext(userAgent);
>         System.out.println("setting new font painter");
>         TextPainter textPainter = null;
>         
>          FontInfo fontInfo = new FontInfo();
>          try {
>            FontSetup.setup(fontInfo);
>          } catch (Exception e) {
>            e.printStackTrace();
>          }
>          System.out.println("is setupValid " + fontInfo.isSetupValid());
>         
>         textPainter = new PDFTextPainter(fontInfo);
>         ctx.setTextPainter(textPainter);
>         
>         return ctx;
>     }
> }
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
> For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org


RE: Newbie: Svg -> PDF Text producing Huge Files

Posted by Jared Davis <ja...@davisprogramming.com>.
Update:

Extending PrintTranscoder to set the TextPainter to PDFTextPainter works -
sort of. The StrokingTextPainter class is still used on svg files referenced
by image tags.

Why does SVGImageElementBridge createSVGImageNode ignore the current
TextPainter?


   protected GraphicsNode createSVGImageNode(BridgeContext ctx,
                                              Element e,
                                              SVGDocument imgDocument) {
        CSSEngine eng = ((SVGOMDocument)imgDocument).getCSSEngine();
        if (eng != null) {
            subCtx = (BridgeContext)eng.getCSSContext();
        } else {
            subCtx = new BridgeContext(ctx.getUserAgent(), 
                                       ctx.getDocumentLoader());
            subCtx.setGVTBuilder(ctx.getGVTBuilder());
//
            subCtx.setTextPainter(ctx.getTextPainter());  // Added Test Code
//
            subCtx.setDocument(imgDocument);
            subCtx.initializeDocument(imgDocument);
        }

...


 public class PrintTranscoderWithRealFonts extends PrintTranscoder {
    
    /** Creates a new instance of PrintTranscoderWithRealFonts */
    public PrintTranscoderWithRealFonts() {
    }
       protected BridgeContext createBridgeContext() {
        BridgeContext ctx = new BridgeContext(userAgent);
        System.out.println("setting new font painter");
        TextPainter textPainter = null;
        
         FontInfo fontInfo = new FontInfo();
         try {
           FontSetup.setup(fontInfo);
         } catch (Exception e) {
           e.printStackTrace();
         }
         System.out.println("is setupValid " + fontInfo.isSetupValid());
        
        textPainter = new PDFTextPainter(fontInfo);
        ctx.setTextPainter(textPainter);
        
        return ctx;
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org