Having trouble creating a PDF with ABCPdf using HtmlOptions.OnLoadScript?

Hey everyone, I'm currently working on a bootstrap-based HTML page and trying to generate a PDF from it using ABCPdf. I've been attempting to run a JavaScript script to apply some custom styles to my HTML content, but for some reason, it's not working as expected. Does anyone have any insight into what might be causing this issue?


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

Any help or guidance would be greatly appreciated! Thank you. :)

Answer №1

To solve the issue at hand, when working with ABCPdf, it is imperative to include the following line of code in order to execute JavaScript effectively (note that jQuery may not work as expected without this setup):

 theDoc.HtmlOptions.Engine = EngineType.Gecko;

After implementing the above line, you can proceed to run your code smoothly:

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

                        var element = document.getElementById('description');
                        var childNodes =  element.childNodes;
                        var count = 0;
                        for (i = 0; i < childNodes.length; i++) {  
                            if(childNodes[i].className == 'info'){ 
                                count = count + childNodes[i].offsetHeight;
                                if(count > 1600) {
                                    count = childNodes[i].offsetHeight;
                                    var elements = childNodes[i].childNodes; 
                                    for(j = 0; j < elements.length; j++) { 
                                        if (elements[j].className == 'infoDescrip'  && i != 1){
                                            elements[j].className += ' page-break';
                                        }        
                                    }
                                }           
                             }   
                        }


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

However, please keep in mind that utilizing EngineType.Gecko may cause compatibility issues with the Bootstrap library.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When is it more advantageous to use Next.js instead of plain React?

As someone who is new to the world of React, I've dabbled in creating simple web applications using the React library and the convenient create-react-app command with npx. However, I've come to realize that the Client Side Render (CSR) applicatio ...

Vue transition not functioning properly when hovering over text for changes

When you hover over the circular region on the website, the text and description in the red box will change. To add a fade animation effect when the text changes, I attempted to use <transition> as per the documentation provided in this link. Unfor ...

Employing buttons and state to eliminate an item from a roster

My list is generated using the following code: return (this.state.limit).fill().map((_,index) => { return ( <div key={`${index}`}> Item </div> ) ) I'm wondering how I can implement a button that allows me to remove a specific ...

Issue encountered while attempting to log out a user using Keycloak in a React JS application

I'm currently working on implementing authentication for a React JS app using Keycloak. To manage the global states, specifically keycloackValue and authenticated in KeycloackContext, I have opted to use React Context. Specific Cases: Upon initial r ...

Trouble with displaying an array of React elements in the render() function

While learning React by creating sample projects with Meteor, I've managed to grasp the basics but hit a wall. I'm utilizing a Twitter API to fetch the latest three tweets from the BOINGBOING Twitter page. The API is functional, and I can succes ...

What is the shebang used for in JavaScript?

In one article I came across, it was mentioned that by including #! /usr/bin/python in a file named testScript, running ./testScript from the command line would be the same as running /usr/bin/python testScript This concept seems plausible to me as most s ...

Creating a smooth fading effect for an element within a react component

I am currently working on implementing a fade out warning/error message (styled with Bootstrap) in a React component, however, I am encountering some challenges with the timing of the fade-out effect. Up to this point, the fade out effect is functioning c ...

how to handle form submission using JavaScript's return method

As a developer, I have created a single page that fetches data from a registration page. On this page, each data row has an "add" and "unfriend" button, with the latter initially disabled. When a user clicks the "add" button, a prompt box appears asking ...

Accessing appsettings.json values in .NET Core ConfigurationExploring how

I need assistance with writing a unit test for the following scenario: [TestMethod] public void GetInviteEndPoint_ShouldAccessAppSettings() { //Data extracted from appsettings.test.json var config = InitConfiguration(); var inviteEndPointConfi ...

Is it feasible to capture a screenshot of a URL by using html2canvas?

Is it possible to take a screenshot of a specific URL using html2canvas? For example, if I have the following URLs: mydomain.com/home mydomain.com/home?id=2 mydomain.com/home/2 How can I capture and display the screenshot image on another page? window ...

Is it possible to program Zapier to automatically open a hyperlink that was included in an email?

Currently, I am utilizing Mailparser for extracting information from a booking email, and then leveraging Zapier to input this booking into our system. Within the email, there exists a link that needs to be accessed in order to confirm the booking. I am i ...

Is it possible to verify whether a Node Js query is empty or not?

I've been attempting to determine if a result query is empty or not, conducting thorough research in the process. Below is the code illustrating the query I'm executing and the two methods I employed to check if it's empty. The issue at han ...

How to Parse JSON Data in C# using EmberJS

Currently, I am utilizing ember-data alongside the RESTSerializer to display my model data. Initially, I aimed for it to generate JSONAPI format because it seemed like a straightforward process to handle the conversion on the server end using C#. There is ...

When executing a Javascript POST request to a PHP script, it succeeds when running on

I have a simple code that works fine on my website when the PHP file is linked as "/phpcode.php", but it fails to retrieve data when I place the JavaScript request on another site and use the full link. I am currently using GoDaddy as my hosting provider. ...

Angular 2 with a jssor slider that adjusts seamlessly to different screen

After following the guidance provided in this answer on incorporating jssor into angular2, I have implemented the following JavaScript code snippet in a file and referenced it in angular-cli.json. jssor_1_slider_init = function() { var jssor_1_op ...

What is the common approach for directing a setState Redux action?

I am looking to streamline my state update actions in multiple react-redux reducers by creating a general setState action. This will allow me to have consistent syntax for dispatching both local and redux scope updates. For local updates, I would use this. ...

How is it that my initial response appears correct in the first log, but then suddenly changes to a 1?

I've encountered a strange issue where the response appears correctly in the initial log, but later changes to a '1' when console.log(data); is called. The screenshot illustrates the pattern: https://i.sstatic.net/zaXcg.png If you expand ...

Utilize an enum from a shared library by exporting it and incorporating it into a TypeScript project

Currently, I am in the process of developing a library where a React component is being exported along with some interfaces and an enum. After compiling the typescript project, I realized that the library is specifically for React and not typescript, so I ...

Is the shift key being pressed during the onClick event in React?

To trigger an onClick event only when the meta(mac) / ctrl(win) key is pressed, follow these steps: This is what I attempted: const [shiftOn, setShiftOn] = useState(false) useEffect(() => { document.addEventListener('keydown', (e) => ...

Ways to transfer chosen key to a different template

Is there a way to transfer a selected key to another template for displaying a chart? I've developed a template that exports a multi-line chart, utilizing Axios to retrieve data from an API. In the home page, there's a dropdown menu. When a user ...