Base64 encoded images

Oct 18, 2010 at 3:03 PM
Edited Oct 19, 2010 at 6:50 AM

I'm currently trying to include base64 encoded images in a pdf and cannot seem to get fo.net to accept the syntax. I've used Apache fop successfully from the command line but FO.NET doesn't seem to want to play. Anyone got any ideas?

<fo:external-graphic content-width="46px" content-height="20px" src="data:image/jpeg;base64,'base64 string goes here'"/>

Thanks,

Ian

Coordinator
Dec 4, 2010 at 1:26 PM

Hi Ian

That feature was not present in FOP at the time FO.NET was ported so I am afraid it is unimplemented.

You might want to take a look at FonetDriver.ImageHander.  This is a delegate that you can use to provide your own custom method of resolving images urls to byte arrays:

        /// <summary>
        ///     The delegate subscribers must implement to handle the loading 
        ///     of image data in response to external-graphic formatting objects.
        /// </summary>
        public delegate byte[] FonetImageHandler(string src);

You could write an implementation that extracts and returns the base64 encoded data.

Mark

Jan 11, 2011 at 1:50 PM

Hellow is possible make an example or sample of the delegate use

Jul 18, 2011 at 5:19 PM

Great hint and with a great API everything is possible :-)

Here is an example of how we handled  adding barcode images using FonetImageHandler:

usage (inside src is EAN13 barcode value):

<fo:block>
  <fo:external-graphic src="9771473968012"></fo:external-graphic>
</fo:block>

 In code:

        private static byte[] ImageHandler(string input) 
        {
            long result;
            if (string.IsNullOrEmpty(input) || input.Length != 13 || !long.TryParse(input, out result))
            {
                return null//fo performs normal processing
            }
 
            return BarcodeImager.CreateBarcodeImage(input, 36);
        }

 and to make use of the ImageHandler:

       MemoryStream foTemplate = new MemoryStream(File.ReadAllBytes("fotemplate.fo"));
       MemoryStream output = new MemoryStream();
       FonetDriver driver = FonetDriver.Make();
       driver.ImageHandler = new FonetDriver.FonetImageHandler(ImageHandler);//to create barcode images
       driver.Render(foTemplate, output);

Of course, one may argue that instead of just numbers, some formatting hint would've been better, but we're happy just the way it is :)

Btw... in case someone needs it, the BarcodeImager is using the iTextSharp dll like this:

public static byte[] CreateBarcodeImage(string barcode, int barHeight)
        {
            BarcodeEAN barCodeEan = new BarcodeEAN();
 
            barCodeEan.CodeType = iTextSharp.text.pdf.Barcode.EAN13;
            barCodeEan.Code = barcode;
            barCodeEan.BarHeight = barHeight;

            var image = barCodeEan.CreateDrawingImage(Color.Black, Color.White);
            MemoryStream ms = new MemoryStream();
            image.Save(ms, ImageFormat.Gif);
            return ms.GetBuffer();

        }


Thanks again for this greatgreat fonet!!