无法使用ABCPdf c#将Graph html渲染为Pdf

时间:2016-01-28 07:10:04

标签: c# pdf

我需要你的帮助才能在使用ABCpdf渲染pdf时显示图表。我能够将包含图形的Html文件之一渲染到Pdf中。但我尝试了另一个包含graph的html。它不会导出为pdf。这是Html文件和pdf生成代码。请帮忙

Html:

<!DOCTYPE html>
<html>

  <head>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js'></script>
    <script type='text/javascript' src='http://local.amp.vg/v4u/js/plugin/flot/flot.helper.js'></script>
    <script type='text/javascript' src='http://local.amp.vg/v4u/js/plugin/flot/jquery.flot.tooltip.js'></script>
    <script type='text/javascript' src='http://local.amp.vg/v4u/js/plugin/flot/jquery.flot.cust.js'></script>
    <script type='text/javascript' src='http://local.amp.vg/v4u/js/plugin/flot/jquery.flot.time.js'></script>
    <!-- <script type='text/javascript' src='http://local.amp.vg/v4u/js/plugin/flot/jquery.flot.resize.js'></script>-->
    <script type='text/javascript' src='http://local.amp.vg/v4u/js/plugin/flot/jquery.flot.pie.js'></script>
    <link href='http://local.amp.vg/v4u/css/smartadmin-production.css' rel='stylesheet' />
    <link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css' />
    <link rel='stylesheet' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css' />
    <link href='http://local.amp.vg/v4u/css/custom.css' rel='stylesheet' />
    <script type='text/javascript'>
      function addText(text) {
        var h = document.createElement('h1');
        var t = document.createTextNode(text);
        h.appendChild(t);
        document.body.appendChild(h);
      }

      function onLoad() {
        addText("PDf Generation");
        drawCharts();
      }

      function drawCharts() {
        loadLineChart('summary-activity', {
          "y": ["Sep", "Oct", "Nov", "Dec", "Jan"],
          "x": [{
            "name": "http://122.200.17.73/ISPOutput/Outputs/Project_15272/index.html",
            "color": "#70A4D3",
            "points": [0, 0, 0, 0, 0]
          }, {
            "name": "http://output.newplans.com/PersonalBranding/User_807/articles.html",
            "color": "#F7941D",
            "points": [0, 53, 20, 0, 0]
          }, {
            "name": "http://223.30.2.86/newplansOutput/Outputs/Project_10245/index.html",
            "color": "#C0C1C5",
            "points": [0, 34, 0, 9, 2]
          }, {
            "name": "http://gfdgdf3.com",
            "color": "#A8BB3D",
            "points": [0, 0, 0, 0, 0]
          }, {
            "name": "http://vxcvxcv.cvxcvx77",
            "color": "#22DF55",
            "points": [0, 0, 0, 0, 0]
          }]
        });
        //loadPieChart('pie-chart', [{ "label": "visits", "data": "168" }, { "label": "unique visits", "data": "64" }, { "label": "pages", "data": "8" }]);
      }
      $(document).ready(function() {
        onLoad();
      });

    </script>
  </head>

  <body style='overflow: scroll;'>
    <div id='reportDiv' style='margin: 30px;'>
      <div class='row'>
        <div class='col col-xs-12 col-sm-12 col-md-12 col-lg-12'>
          <h2 style='text-align: center;'>Tracksites Summary</h2>
        </div>
        <div>
          <div class='row'>
            <table id="inbox-table" class="table table-striped table-hover" data-record-count="3">
              <thead>
                <tr>
                  <th class="inbox-data-date hidden-sm">Url</th>
                  <th class="inbox-data-date hidden-sm">Visits</th>
                  <th class="inbox-data-date hidden-sm">Unique</th>
                  <th class="inbox-data-date hidden-sm">Pages</th>
                  <th class="inbox-data-date hidden-sm">Last Activity</th>
                </tr>
              </thead>
              <tbody>
                <tr id="msg1" data-recordid="228" data-name="http://223.30.2.86/newplansOutput/Outputs/Project_10245/index.html">
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="0">
                    <div rel="tooltip" data-placement="right" data-original-title="http://223.30.2.86/newplansOutput/Outputs/Project_10245/index.html">http://223.30.2.86/newplansOutput/Outputs/Project_10245/index.html </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="0">
                    <div rel="tooltip" data-placement="right" data-original-title="45 visits">45 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="0">
                    <div rel="tooltip" data-placement="right" data-original-title="12 unique visits ">12 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="0">
                    <div rel="tooltip" data-placement="right" data-original-title="3 pages">3 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="0">
                    <div rel="tooltip" data-placement="left" data-original-title="last activity on 01/08/2016 02:57:25 pm">01/08/2016 </div>
                  </td>
                </tr>
                <tr id="msg1" data-recordid="227" data-name="http://output.newplans.com/PersonalBranding/User_807/articles.html">
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="1">
                    <div rel="tooltip" data-placement="right" data-original-title="http://output.newplans.com/PersonalBranding/User_807/articles.html">http://output.newplans.com/PersonalBranding/User_807/articles.html </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="1">
                    <div rel="tooltip" data-placement="right" data-original-title="73 visits">73 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="1">
                    <div rel="tooltip" data-placement="right" data-original-title="19 unique visits ">19 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="1">
                    <div rel="tooltip" data-placement="right" data-original-title="4 pages">4 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="1">
                    <div rel="tooltip" data-placement="left" data-original-title="last activity on 11/17/2015 01:26:40 pm">11/17/2015 </div>
                  </td>
                </tr>
                <tr id="msg1" data-recordid="194" data-name="http://122.200.17.73/ISPOutput/Outputs/Project_15272/index.html">
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="2">
                    <div rel="tooltip" data-placement="right" data-original-title="http://122.200.17.73/ISPOutput/Outputs/Project_15272/index.html">http://122.200.17.73/ISPOutput/Outputs/Project_15272/index.html </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="2">
                    <div rel="tooltip" data-placement="right" data-original-title="50 visits">50 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="2">
                    <div rel="tooltip" data-placement="right" data-original-title="33 unique visits ">33 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="2">
                    <div rel="tooltip" data-placement="right" data-original-title="1 pages">1 </div>
                  </td>
                  <td class="inbox-data-date hidden-sm" data-action="view" data-action-item="2">
                    <div rel="tooltip" data-placement="left" data-original-title="last activity on 10/06/2014 12:45:18 pm">10/06/2014 </div>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
          <div class='row'>
            <div class='widget-body no-padding'>
              <div id='summary-activity' class='chart no-padding'></div>
            </div>
          </div>
          <div class='row'>
            <div class='widget-body no-padding'>
              <div id='pie-chart' class='chart no-padding'></div>
            </div>
          </div>
  </body>

</html>

Pdf生成代码:

 public string createPdf(User user, string filename, double w, double h, out string _error)
        {
            _error = "New doc is not created\n";
            string pdfFilePath = "", strPdfUrl = "";
            using (var theDoc = new WebSupergoo.ABCpdf8.Doc())
            {
                _error += "New doc now created\n";
                try
                {
                    int iID = Convert.ToInt32(1);
                    string strType = "Type";
                    string strInfo = "Info";
                    theDoc.SetInfo(iID, strType, strInfo);
                    string strPageDimension = "";
                    theDoc.HtmlOptions.Timeout = 20000;
                    theDoc.HtmlOptions.RetryCount = 3;                            

                    theDoc.MediaBox.Height = h;
                    theDoc.MediaBox.Width = w;
                    strPageDimension = "0 0 " + w.ToString() + " " + h.ToString();
                    theDoc.MediaBox.String = strPageDimension;
                    theDoc.Rect.String = strPageDimension;
                    theDoc.Rect.Left = 0;
                    theDoc.Rect.Top = 0;
                    theDoc.Rect.Width = w;
                    theDoc.Rect.Height = h;
                    theDoc.AddFont("HelveticaNeue");
                    theDoc.EmbedFont("HelveticaNeue");
                    theDoc.HtmlOptions.UseNoCache = true; 

                    theDoc.HtmlOptions.Engine = EngineType.Gecko;
                    theDoc.HtmlOptions.UseScript = true;
                    theDoc.HtmlOptions.UseActiveX = true;              
                    theDoc.HtmlOptions.UseScript = true;
                    theDoc.HtmlOptions.OnLoadScript = "(function(){window.ABCpdf_go = false; setTimeout(function(){window.ABCpdf_go = true;}, 5000); })();"; //

                    theDoc.Rendering.DotsPerInch = 96;
                    theDoc.HtmlOptions.PageCacheEnabled = true;
                    theDoc.HtmlOptions.HideBackground = false;

                    string strHtmlUrl = Installs.Current.GetUrlPath(DirectoryType.Html, filename+".html", false);

                    int theID = theDoc.AddImageUrl(strHtmlUrl);

                    while (true)
                    {
                        theDoc.FrameRect();
                        if (!theDoc.Chainable(theID))
                            break;
                        theDoc.Page = theDoc.AddPage();
                        theID = theDoc.AddImageToChain(theID);
                    }    

                    for (int i = 1; i <= theDoc.PageCount; i++)
                    {
                        theDoc.PageNumber = i;
                        theDoc.Flatten();
                    }

                    pdfFilePath = GetPhysicalFile(DirectoryType.Pdf, null, filename + ".pdf");
                    var pdffilename = System.IO.Path.GetFileName(pdfFilePath);
                    var path = System.IO.Path.GetDirectoryName(pdfFilePath);
                    var file = System.IO.Path.Combine(path, filename + ".pdf");

                    if (!System.IO.Directory.Exists(path))
                        Directory.CreateDirectory(path);

                    if (File.Exists(file))
                        File.Delete(file);

                    theDoc.Save(pdfFilePath);
                    strPdfUrl = Installs.Current.GetUrlPath(DirectoryType.Pdf, filename + ".pdf",false);
                }
                catch (Exception ex)
                {
                    _error += ex.Message.ToString();
                    File.Delete(pdfFilePath);
                    return "";

                }

                return strPdfUrl;
            }
        }

0 个答案:

没有答案