HtmlOptions.OnLoadScript无法使用ABCPdf

时间:2017-06-22 07:45:20

标签: javascript c# asp.net pdf abcpdf

我有一个用bootstrap创建的html页面,我想用它创建一个PDF,并且我想用ABCPdf创建它。 我试图执行一个javascript脚本来将一些样式应用到我的html中,但它不起作用。有人知道会发生什么吗?

     Doc theDoc = new Doc();
     theDoc.Rect.Inset(5, 20);
     theDoc.HtmlOptions.Timeout = 3000000; 
     theDoc.HtmlOptions.UseScript = true;
     theDoc.HtmlOptions.PageCacheEnabled = false;
     theDoc.HtmlOptions.DoMarkup = true;
     theDoc.HtmlOptions.HostWebBrowser = false;
     theDoc.HtmlOptions.Media = MediaType.Screen;
     theDoc.HtmlOptions.BrowserWidth = 1500;
     theDoc.HtmlOptions.AddLinks = true;
     string script = @"var elemento = document.getElementById('description');"
                      +"for (i = 0; i < elemento.childNodes.length; i++) {"
                      +    "if (elemento.childNodes[i].className == 'row') {"
                      +       " if (elemento.childNodes[i].offsetHeight > 200) {"                                    
                                     +            "elemento.childNodes[i].className += ' saltoPagina';  }  }  }";

    theDoc.HtmlOptions.OnLoadScript = script;
    int theID;
    theID = theDoc.AddImageUrl(url, true, 0, true);

提前致谢。 :)

1 个答案:

答案 0 :(得分:1)

解决:如果你正在使用ABCPdf,你就不能执行javaScript(jQuery永远不会工作)而没有下一行:

 theDoc.HtmlOptions.Engine = EngineType.Gecko;

然后执行你的代码:

theDoc.HtmlOptions.GeckoSubset.OnLoadScript =
                @"(function() {
                        window.ABCpdf_go = false;

                        var elemento = document.getElementById('description');
                        var elementos =  elemento.childNodes;
                        var contador = 0;
                        for (i = 0; i < elementos.length; i++) {  
                            if(elementos[i].className == 'info'){ 
                                contador = contador + elementos[i].offsetHeight;
                                if(contador > 1600) {
                                    contador = elementos[i].offsetHeight;
                                    var childElements = elementos[i].childNodes; 
                                    for(j = 0; j < childElements.length; j++) { 
                                        if (childElements[j].className == 'infoDescrip'  && i != 1){
                                            childElements[j].className += ' saltoPagina';
                                        }        
                                    }
                                }           
                             }   
                        }


                        window.ABCpdf_go = true;
                })();";                 

但是如果使用EngineType.Gecko,则bootstrap librarie不起作用。