title: Export Fusionchart to PDF using iTextSharp
body: <p>I would like to export my FusionChart to PDF using iTextSharp. I am able to export to PDF by use of setting the charts exportEnabled = '1', but I want to be able to export the fusionchart from my controller. </p>
  
  <p>I have my entire fusionchart in a str variable and tried the following :</p>
  
  <pre><code>public FileStreamResult ExportPDF(int ProjectID)
      {
          string exportData = String.Format("&lt;html&gt;&lt;body&gt;{0}&lt;/body&gt;&lt;/html&gt;", getGraphXMLData(ProjectID));//This Gets the chart and stores into string
          var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
          using (var input = new MemoryStream(bytes))
          {
              var output = new MemoryStream();
              var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
              var writer = PdfWriter.GetInstance(document, output);
  
              Font headerFont = FontFactory.GetFont("Verdana", 10);
              Font rowfont = FontFactory.GetFont("Verdana", 10);
  
              writer.CloseStream = false;
              document.Open();
  
              var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
              xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
              document.Close();
              output.Position = 0;
  
              return File(output, "application/pdf", "ProfileChart_Report.pdf");
  
              //return new FileStreamResult(output, "application/pdf");
          }
      }
  </code></pre>
  
  <p>I however get an error on this line : <code>document.Close();</code></p>
  
  <p>The error im getting is : </p>
  
  <pre><code>An exception of type 'System.IO.IOException' occurred in iTextSharp.dll but was not handled in user code
  
  Additional information: The document has no pages.
  </code></pre>
  
  <p>Below is the data in the <code>exportData</code> field:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;
  &lt;chart caption='Project Test' yaxisname='Elevation' xaxisname='Accumulated Length' xaxismaxvalue='1000' xaxisminvalue='100' yaxismaxvalue='200' xnumberprefix='' ynumberprefix='' showcanvasborder='1' canvasborderthickness='0.5' canvasborderalpha= '50' showXAxisLine= '0' showformbtn= '1' formAction='#' submitdataasxml='1' baseFontColor='#333333' baseFont='Helvetica Neue, Arial' captionFontSize='14' subcaptionFontSize='14' subcaptionFontBold='0' showBorder='0' bgColor='#ffffff' showValues='0' showShadow='0' canvasBgColor='#ffffff' divlineAlpha='100' divlineColor='#999999' divlineThickness='1' divLineIsDashed='1' divLineDashLen='1' divLineGapLen='1' usePlotGradientColor='0' showplotborder='0' showAlternateHGridColor='0' showAlternateVGridColor='0' legendBgAlpha='0' legendBorderAlpha='0' legendShadow='0' legendItemFontSize='10' legendItemFontColor='#666666' toolTipColor='#ffffff' toolTipBorderThickness='0' toolTipBgColor='#000000' toolTipBgAlpha='80' toolTipBorderRadius='2' toolTipPadding = '5' labeldisplay= 'rotate' slantlabels= '1' exportEnabled='1' &gt; &lt;categories verticallinecolor='666666' verticallinethickness='1' alpha='40' anchorsides='0' anchorradius='0'&gt;&lt;category label='0' x='0' showverticalline='0'/&gt;
  &lt;category label='46.17' x='46.17' showverticalline='0'/&gt;
  &lt;category label='142.98' x='142.98' showverticalline='0'/&gt;
  &lt;category label='543.88' x='543.88' showverticalline='0'/&gt;
  &lt;category label='826.58' x='826.58' showverticalline='0'/&gt;
  &lt;category label='1037.93' x='1037.93' showverticalline='0'/&gt;
  &lt;category label='1106.1' x='1106.1' showverticalline='0'/&gt;
  &lt;category label='1338.1' x='1338.1' showverticalline='0'/&gt;
  &lt;category label='1838.5' x='1838.5' showverticalline='0'/&gt;
  &lt;category label='1889.37' x='1889.37' showverticalline='0'/&gt;
  &lt;category label='2538' x='2538' showverticalline='0'/&gt;
  &lt;category label='2789.04' x='2789.04' showverticalline='0'/&gt;
  &lt;category label='3154.34' x='3154.34' showverticalline='0'/&gt;
  &lt;category label='3345.17' x='3345.17' showverticalline='0'/&gt;
  &lt;category label='4220.75' x='4220.75' showverticalline='0'/&gt;
  &lt;category label='4671.9' x='4671.9' showverticalline='0'/&gt;
  &lt;category label='5057.74' x='5057.74' showverticalline='0'/&gt;
  &lt;category label='5318.51' x='5318.51' showverticalline='0'/&gt;
  &lt;category label='5576.18' x='5576.18' showverticalline='0'/&gt;
  &lt;category label='5958.13' x='5958.13' showverticalline='0'/&gt;
  &lt;category label='6537.92' x='6537.92' showverticalline='0'/&gt;
  &lt;category label='7282.09' x='7282.09' showverticalline='0'/&gt;
  &lt;category label='8178.84' x='8178.84' showverticalline='0'/&gt;
  &lt;category label='8511.22' x='8511.22' showverticalline='0'/&gt;
  &lt;category label='8989.59' x='8989.59' showverticalline='0'/&gt;
  &lt;category label='9442.78' x='9442.78' showverticalline='0'/&gt;
  &lt;category label='9862.78' x='9862.78' showverticalline='0'/&gt;
  &lt;category label='9942.78' x='9942.78' showverticalline='0'/&gt;
  &lt;category label='10402.78' x='10402.78' showverticalline='0'/&gt;
  &lt;category label='10607.78' x='10607.78' showverticalline='0'/&gt;
  &lt;category label='11052.78' x='11052.78' showverticalline='0'/&gt;
  &lt;category label='11267.78' x='11267.78' showverticalline='0'/&gt;
  &lt;category label='11797.78' x='11797.78' showverticalline='0'/&gt;
  &lt;category label='12307.78' x='12307.78' showverticalline='0'/&gt;
  &lt;category label='12572.78' x='12572.78' showverticalline='0'/&gt;
  &lt;category label='13229.78' x='13229.78' showverticalline='0'/&gt;
  &lt;category label='13369.78' x='13369.78' showverticalline='0'/&gt;
  &lt;/categories&gt;
  &lt;dataset  drawline= '1' seriesname= 'Elevation' color= '#0045ff' anchorsides= '0' anchorradius= '0' anchorbgcolor= '#0045ff' anchorbordercolor= '#0045ff'&gt;
  &lt;set y='321' x='0'/&gt;
  &lt;set y='322' x='46.17'/&gt;
  &lt;set y='322.54' x='142.98'/&gt;
  &lt;set y='309.12' x='543.88'/&gt;
  &lt;set y='292.63' x='826.58'/&gt;
  &lt;set y='299.12' x='1037.93'/&gt;
  &lt;set y='298.68' x='1106.1'/&gt;
  &lt;set y='303.49' x='1338.1'/&gt;
  &lt;set y='294.23' x='1838.5'/&gt;
  &lt;set y='295.77' x='1889.37'/&gt;
  &lt;set y='269.98' x='2538'/&gt;
  &lt;set y='274.17' x='2789.04'/&gt;
  &lt;set y='261.71' x='3154.34'/&gt;
  &lt;set y='267.01' x='3345.17'/&gt;
  &lt;set y='243.04' x='4220.75'/&gt;
  &lt;set y='247.05' x='4671.9'/&gt;
  &lt;set y='250.81' x='5057.74'/&gt;
  &lt;set y='246.4' x='5318.51'/&gt;
  &lt;set y='249.62' x='5576.18'/&gt;
  &lt;set y='238.03' x='5958.13'/&gt;
  &lt;set y='253.08' x='6537.92'/&gt;
  &lt;set y='235.53' x='7282.09'/&gt;
  &lt;set y='173.53' x='8178.84'/&gt;
  &lt;set y='189.91' x='8511.22'/&gt;
  &lt;set y='224.23' x='8989.59'/&gt;
  &lt;set y='250' x='9442.78'/&gt;
  &lt;set y='250' x='9862.78'/&gt;
  &lt;set y='253.45' x='9942.78'/&gt;
  &lt;set y='240' x='10402.78'/&gt;
  &lt;set y='235' x='10607.78'/&gt;
  &lt;set y='210' x='11052.78'/&gt;
  &lt;set y='210' x='11267.78'/&gt;
  &lt;set y='195' x='11797.78'/&gt;
  &lt;set y='170' x='12307.78'/&gt;
  &lt;set y='160' x='12572.78'/&gt;
  &lt;set y='150' x='13229.78'/&gt;
  &lt;set y='141.36' x='13369.78'/&gt;
  &lt;/dataset&gt;
  &lt;dataset  drawline= '1' seriesname= 'HGL' color= '#198500' anchorsides= '0' anchorradius= '0' anchorbgcolor= '#198500' anchorbordercolor= '#198500'&gt;
  &lt;set y='321' x='0'/&gt;
  &lt;set y='320.87' x='46.17'/&gt;
  &lt;set y='320.59' x='142.98'/&gt;
  &lt;set y='319.46' x='543.88'/&gt;
  &lt;set y='318.66' x='826.58'/&gt;
  &lt;set y='318.06' x='1037.93'/&gt;
  &lt;set y='317.87' x='1106.1'/&gt;
  &lt;set y='317.22' x='1338.1'/&gt;
  &lt;set y='315.81' x='1838.5'/&gt;
  &lt;set y='315.67' x='1889.37'/&gt;
  &lt;set y='313.84' x='2538'/&gt;
  &lt;set y='313.1' x='2789.04'/&gt;
  &lt;set y='312.02' x='3154.34'/&gt;
  &lt;set y='311.38' x='3345.17'/&gt;
  &lt;set y='308.46' x='4220.75'/&gt;
  &lt;set y='306.96' x='4671.9'/&gt;
  &lt;set y='305.68' x='5057.74'/&gt;
  &lt;set y='304.81' x='5318.51'/&gt;
  &lt;set y='303.95' x='5576.18'/&gt;
  &lt;set y='302.68' x='5958.13'/&gt;
  &lt;set y='300.75' x='6537.92'/&gt;
  &lt;set y='298.27' x='7282.09'/&gt;
  &lt;set y='294.88' x='8178.84'/&gt;
  &lt;set y='293.39' x='8511.22'/&gt;
  &lt;set y='291.25' x='8989.59'/&gt;
  &lt;set y='285.78' x='9442.78'/&gt;
  &lt;set y='281.31' x='9862.78'/&gt;
  &lt;set y='280.46' x='9942.78'/&gt;
  &lt;set y='275.55' x='10402.78'/&gt;
  &lt;set y='273.37' x='10607.78'/&gt;
  &lt;set y='256.15' x='11052.78'/&gt;
  &lt;set y='247.84' x='11267.78'/&gt;
  &lt;set y='227.36' x='11797.78'/&gt;
  &lt;set y='204.03' x='12307.78'/&gt;
  &lt;set y='191.92' x='12572.78'/&gt;
  &lt;set y='156.49' x='13229.78'/&gt;
  &lt;set y='148.94' x='13369.78'/&gt;
  &lt;/dataset&gt;
  &lt;dataset  drawline= '0' seriesname= 'Peak' color= '#ff0000' anchorsides= '3' anchorradius= '5' anchorbgcolor= '#ff0000' anchorbordercolor= '#ff0000'&gt;
  &lt;set y='321' x='0'/&gt;
  &lt;set y='322.54' x='142.98'/&gt;
  &lt;set y='299.12' x='1037.93'/&gt;
  &lt;set y='303.49' x='1338.1'/&gt;
  &lt;set y='295.77' x='1889.37'/&gt;
  &lt;set y='274.17' x='2789.04'/&gt;
  &lt;set y='267.01' x='3345.17'/&gt;
  &lt;set y='250.81' x='5057.74'/&gt;
  &lt;set y='249.62' x='5576.18'/&gt;
  &lt;set y='253.08' x='6537.92'/&gt;
  &lt;set y='253.45' x='9942.78'/&gt;
  &lt;set y='141.36' x='13369.78'/&gt;
  &lt;/dataset&gt;
  &lt;dataset  drawline= '0' seriesname= 'Valve - Change' color= '#198500' anchorsides= '4' anchorradius= '5' anchorbgcolor= '#198500' anchorbordercolor= '#198500'&gt;
  &lt;set y='309.12' x='543.88'/&gt;
  &lt;set y='235.53' x='7282.09'/&gt;
  &lt;set y='235' x='10607.78'/&gt;
  &lt;set y='195' x='11797.78'/&gt;
  &lt;set y='150' x='13229.78'/&gt;
  &lt;/dataset&gt;
  &lt;dataset  drawline= '0' seriesname= 'Valve suggest - Long Node' color= '#fdff00' anchorsides= '4' anchorradius= '3' anchorbgcolor= '#fdff00' anchorbordercolor= '#fdff00'&gt;
  &lt;set y='321' x='0'/&gt;
  &lt;set y='309.12' x='543.88'/&gt;
  &lt;set y='294.23' x='1838.5'/&gt;
  &lt;set y='269.98' x='2538'/&gt;
  &lt;set y='261.71' x='3154.34'/&gt;
  &lt;set y='243.04' x='4220.75'/&gt;
  &lt;set y='247.05' x='4671.9'/&gt;
  &lt;set y='250.81' x='5057.74'/&gt;
  &lt;set y='238.03' x='5958.13'/&gt;
  &lt;set y='253.08' x='6537.92'/&gt;
  &lt;set y='235.53' x='7282.09'/&gt;
  &lt;set y='173.53' x='8178.84'/&gt;
  &lt;set y='189.91' x='8511.22'/&gt;
  &lt;set y='224.23' x='8989.59'/&gt;
  &lt;set y='250' x='9442.78'/&gt;
  &lt;set y='250' x='9862.78'/&gt;
  &lt;set y='240' x='10402.78'/&gt;
  &lt;set y='210' x='11052.78'/&gt;
  &lt;set y='195' x='11797.78'/&gt;
  &lt;set y='170' x='12307.78'/&gt;
  &lt;set y='150' x='13229.78'/&gt;
  &lt;/dataset&gt;
  &lt;/chart&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I specifically want to do it in iTextSharp as I have other exports that im doing that is also using iTextSharp and ultimately want to combine everything into one PDF.</p>
  
  <p>I would appreciate anyones help. Thanks.</p>