title: Need to scale images to fit using XSL FO
body: <p>I am working on a project in which we have charts and a table exported to PDF.  We are using Apache FOP for the PDF generation.  The charts are in SVG and display fine if there's only one.  However, we will have cases in which there are up to four, and in these instances only three of the four are fitting on the generated PDF.  I need all images to fit regardless of number.  I'm afraid simply putting the page in landscape will be a temporary solution for when we need even more to fit.  The sizes of each chart are set in pixels from the svg generated in the client by the Highcharts library.  It appears as though the images are inserted into the pdf without any scaling, even though I set attributes for the instream-foreign-object per an example seen here: <a href="http://xep.xattic.com/xep/testsuite/usecases/image-scale.pdf" rel="nofollow noreferrer">Scaling images using scale-to-fit</a></p>
  
  <p>So I need each chart to "shrink to fit".  I have tried setting the length and width of the  element that encloses each  element that contains an instream-foreign object (I am also including my xsl).  Within the instream-foreign -object I am assigning the svg width &amp; height to the values from the actual chart svg.  I thought maybe this was the problem, but if I don't include these dimensions the charts don't appear in the pdf.  I also tried setting the scaling attribute to "non-uniform" per the question here: <a href="https://stackoverflow.com/questions/1957847/how-to-display-fixed-image-height-and-width-using-xsl-fo-with-apache-fop-0-95">Scale images with fixed height</a> and the results were no better.  It actually stretched a chart across the whole page thus displaying only one of four. Here's my xsl: </p>
  
  <pre><code>    &lt;xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:fo="http://www.w3.org/1999/XSL/Format"&gt;
  &lt;xsl:output method="xml" indent="yes"/&gt;
  &lt;xsl:template match="/"&gt;
      &lt;fo:root&gt;
      &lt;fo:layout-master-set&gt;
          &lt;fo:simple-page-master master-name="exportPage"&gt;
              &lt;fo:region-body /&gt;
              &lt;/fo:simple-page-master&gt;
          &lt;/fo:layout-master-set&gt;
          &lt;fo:page-sequence master-reference="exportPage"&gt;
          &lt;fo:flow flow-name="xsl-region-body"&gt;
              &lt;fo:block id="mainTitleBlock" font-family="arial, helvetica, sans-serif" color="#5c068c" font-weight="bold"&gt;
                  &lt;xsl:value-of  select="exports/export/charts/@mainTitle"/&gt;
              &lt;/fo:block&gt;
              &lt;fo:block id="timestampBlock" font-family="arial, helvetica, sans-serif" font-size="small" color="#6D869F"&gt;
                  &lt;xsl:value-of  select="exports/export/charts/@timeStamp"/&gt;
              &lt;/fo:block&gt;
              &lt;!-- This is where the charts go --&gt;
              &lt;!-- Within this block will be an inline element for each chart--&gt; 
          &lt;fo:block id="chartBlock" width="8.25in" height="6in"&gt;
  
            &lt;xsl:apply-templates select="exports/export/charts/chart"/&gt;
  
          &lt;/fo:block&gt;
                  &lt;fo:block id="tableBlock" &gt;
                      &lt;xsl:apply-templates select="exports/export/table"/&gt;
                  &lt;/fo:block&gt;
              &lt;/fo:flow&gt;
              &lt;/fo:page-sequence&gt;
           &lt;/fo:root&gt;
  &lt;/xsl:template&gt;
     &lt;!-- Creates the table --&gt;
  &lt;xsl:template match="table"&gt;
  &lt;fo:table table-layout="fixed" width="100%" &gt;
      &lt;fo:table-header&gt;
      &lt;fo:table-row&gt;
      &lt;xsl:apply-templates select="tblRow[position() = 1]"/&gt;
      &lt;/fo:table-row&gt;
      &lt;/fo:table-header&gt;
      &lt;fo:table-body&gt;
      &lt;xsl:apply-templates select="tblRow[position() &gt; 1]"/&gt;
      &lt;/fo:table-body&gt;
  &lt;/fo:table&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="hdrCell"&gt;
      &lt;fo:table-cell background-color="#666" border-right-style="solid" border-right-width="1px" border-right-color="white" empty-cells="show"&gt;
      &lt;fo:block color="white" font-family="arial, helvetica, sans-serif" font-size="x-small"&gt;&lt;xsl:value-of select="."/&gt;&lt;/fo:block&gt;
      &lt;/fo:table-cell&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="tblCell"&gt;
      &lt;fo:table-cell border-bottom-style="solid" border-bottom-width="1px" border-bottom-color="#E3E3E3" &gt;
      &lt;fo:block color="#7E7E7E" font-family="arial, helvetica, sans-serif" font-size="x-small"&gt;&lt;xsl:value-of select="."/&gt;&lt;/fo:block&gt;
      &lt;/fo:table-cell&gt;
  &lt;/xsl:template &gt;
  
  &lt;xsl:template match="tblRow[position() &gt; 1]"&gt;
      &lt;fo:table-row&gt;
      &lt;xsl:apply-templates /&gt;
      &lt;/fo:table-row&gt;
  &lt;/xsl:template&gt;
  
  &lt;xsl:template match="chart"&gt;
  &lt;fo:inline&gt;
      &lt;fo:instream-foreign-object xmlns:svg="http://www.w3.org/2000/svg" content-width="scale-to-fit" content-height="100%" width="100%" scaling="uniform"&gt;
      &lt;svg:svg width="{svg:svg/@width}" height="{svg:svg/@height}"&gt;
      &lt;xsl:copy-of select="svg:svg"/&gt;
      &lt;/svg:svg&gt;
      &lt;/fo:instream-foreign-object&gt;
  &lt;/fo:inline&gt;
  &lt;/xsl:template&gt;
  
   &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>What am I missing?  Would it help to display the charts in table-cells (thus breaking the convention of not using tables for layout- the horror!)?  How can I get these to scale and fit in the page?</p>
  
  <p>Thanks,
  Brandt</p>