Inject the css file into the PDFMake styling configuration

My current task involves converting a string with HTML markup into a PDF using the HTML-to-pdfmake and pdf make libraries. The HTML code includes numerous classes and ids, with corresponding styles defined in a CSS file. Rather than manually adding all the styles to the content definition section, I am wondering if there is a way to pass the CSS file to the styles attribute in pdfmake.

This is my current setup:

   let documentDefinition = {
    pageSize: "A4",
    pageOrientation: "portrait",
    footer: footer,
    header: header,
    content: [
      {
        text: htmlToPdfmake(documentBody, {
          window: window,
        }),
      },
    ],
    styles: {
      header: {
        fontSize: 22,
        bold: true,
      },
      anotherStyle: {
        italics: true,
        alignment: "right",
      },
    },
  }

I aim to streamline this process by linking these styles directly from a CSS file, which contains a significant amount of styling rules. The desired implementation would look something like this:

   let documentDefinition = {
    pageSize: "A4",
    pageOrientation: "portrait",
    footer: footer,
    header: header,
    content: [
      {
        text: htmlToPdfmake(documentBody, {
          window: window,
        }),
      },
    ],
    styles: {
      path: "./styles/style.css"

    }
     
  }

Answer №1

Just a heads up: pdfmake doesn't fully support CSS styles and the html-to-pdfmake module can only handle a limited number of CSS properties.

CSS is capable of creating intricate designs, but this framework is only equipped to handle basic HTML / CSS. The support for CSS styles is restricted and may not work with all values in every case:

With that being said, if you want to separate this portion of your configuration, you can relocate it to a different JS (or even JSON) file and load it using your preferred module loader, just like you would with any regular JS or JSON file.

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

Tips for retrieving the text value on keyboard enter press without triggering form submission

I am currently working on a feature where hitting the Enter key in a textbox should not automatically submit the form. The textbox is located within a form: @Html.TextBoxFor(model => model.ID, new { @class = "form-control input-sm", placehold ...

The content of a Puppeteer page mysteriously disappears when transferred to another function

My current project involves using Puppeteer for web scraping on my website. I encountered a strange issue where the await page.content() works fine when I console log the content, but turns out to be null when passed as an argument to another JavaScript ...

Hide jQuery dropdown when mouse moves away

I am working on designing a navigation bar with dropdown functionality. Is there a way to hide the dropdown menu when the mouse pointer leaves both the navbar menu and the dropdown itself? I have tried using hover, mouseenter, and mouseleave but due to my ...

Adding multiple text as label and sublabel in a React MUI Tooltip can be achieved by utilizing the appropriate

I'm struggling to incorporate a MaterialUI Tooltip into my React application with two separate text lines. The first line should serve as the main title, while the second line functions as a sublabel. In this screenshot provided, you can see where I ...

Having Multiple Login Forms on a Single Page

I have encountered an issue with my login form code. It works perfectly as a single form, but I need to have 20 of these forms on one page. Despite my efforts to duplicate the code for each new form and adjusting the IDs, I am unable to make more than one ...

Limit the amount of text displayed on the main section of the webpage

I am working on creating a simple webpage with three different sections styled like this: .top { position:absolute; left:0; right:0; height: 80px; } .left { position:absolute; ...

Unable to choose an option from the panel

I am currently working on a menu and panel setup (you can view the demo here: http://jsfiddle.net/vals/j2LrQ/1/). The visibility of the panel depends on the selected item from the menu. However, I am facing an issue where the panel disappears when I move ...

Responding to a particular message within the Telegraf framework

I am in the process of creating a telegram bot using the telegraf framework (which is based on Node.js). I would like the bot to automatically reply when someone sends the message good morning. Despite my extensive search efforts, I have not yet been able ...

Retrieve the output of a JavaScript function and submit it as extra form data

I am working on a JavaScript function that looks like this: <script type="text/javascript"> function doSomething() { var s = 'some data' return s; } </script> and @using (Html.BeginForm(new { data_to_send = ...

Achieving Dynamic Alignment in CSS: How to Responsively Align a Div to the Width of its Parent Using Padding and Margins

My dilemma could not be clearer - I am struggling to properly align 4 divs within a width of 1150px (the maximum width of the content div). When resizing the screen, I want them to adjust accordingly: 4 in a row when possible, then 3 in the center, and so ...

Real-time webpage featuring dynamically updating text sourced from a file

After spending 5 hours attempting this on my own, I've decided to reach out for help. I am in need of creating a page that will automatically update itself and display the content of a file when it changes. For example, let's say we have a file ...

Is there a way to consolidate all Vue.js routes, templates, and components into a single file?

I am currently working on a Vue.js example where I aim to consolidate everything into a single file. My goal is to demonstrate the efficiency of using a small silo that can serve multiple routes. Here is what I have accomplished so far: <html> <h ...

The PrimeNg confirmDialog is updating my navbar with some modifications

Despite my navbar working well and being fully responsive, I encountered an issue where opening a confirm dialog in the background caused the navbar width to expand to 800px even though the screen width was 1480px, creating empty space on the right side, a ...

What is the best way to assign an image to a div using JavaScript or jQuery?

I'm attempting to create a notification box similar to this one: https://i.sstatic.net/HIJPJ.png As shown, there is a .gif image positioned at the top that will be hidden once the content loads. Now, I want to insert the following image into a <d ...

Looking to transfer the name of the clicked link to a modal in order to execute a PHP/SQL query within the modal window

I am currently utilizing Twitter Bootstrap's modal feature. Within my table, I am mapping MAC addresses to vendor names using the following code: <tbody> <?php foreach ($rowarr as $k => $v) { ?> & ...

The animation in my SVG comes to a halt at a certain point

I am having an issue with my SVG element (shown below) where it stops animating after a certain period of time. I want the animation to continue indefinitely like in this jsfiddle. .path { stroke-dasharray: 20; animation: dash 10s linear; } @ ...

Guide to implementing the onchange event in JavaScript for this specific scenario

How can the onchange event be utilized in this scenario using JavaScript? First, input data into the input with id="one". Subsequently, the data in the input with id="two" will be updated to match the data in the input with id="one". However, when the da ...

Is it possible to transform a tuple type into a union?

Is it possible to map a tuple's generic type to a union type? type TupleToUnion<T> = T[keyof T]; // This will include all values in the tuple const value: TupleToUnion<[7, "string"]> = 2; // This assignment should not be permitted since ...

Understanding Javascript Scoping

<html> <head> <title></title> <script type="text/javascript"> var x = 1; function y() { x = 10; return; function z() {} } </script> </head> <body> <script type="text/javascri ...

Keep an eye out for classes that are added dynamically

Are there any event listeners available that can watch for and execute code when a class is dynamically added to an element? I'm working within a WordPress CMS and a plugin I am using adds classes dynamically. I want to be able to detect this and then ...