Eva Wythien
Eva Wythien

Reputation: 51

HtmlOptions.OnLoadScript doesnt works to create PDF with ABCPdf

I have a html page created with bootstrap, and i want to create a PDF with it and for this i'm using ABCPdf. I'm trying to execute a javascript script to apply some styles to my html, but it doesn't works. Someone knows what happens?

     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);

Thanks in advance. :)

Upvotes: 2

Views: 748

Answers (1)

Eva Wythien
Eva Wythien

Reputation: 51

Solve: If you are using ABCPdf, you cant execute javaScript (jQuery never works) without the next line:

 theDoc.HtmlOptions.Engine = EngineType.Gecko;

And then executes your code:

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;
                })();";                 

But if you use EngineType.Gecko, bootstrap librarie doesn't works.

Upvotes: 1

Related Questions