Unable to implement CSS styles on the provided HTML code

I'm currently working on integrating evoPDF into my asp.net application. When I click on a part of the HTML file, I send that portion using AJAX. So far, everything is working well up to this point. However, when I try to use the following methods from the EvoPdf API:

 1. GetPdfBytesFromHtmlStream(Stream, Encoding,urlbase)

 2. SavePdfFromHtmlStringToFile(String html,string filename,urlbase)

The HTML chunk I am sending looks something like this:

 <ol class = "lol">
         <li> HEY </li>
         <li> Now </li>

  </ol>

The CSS in the external file is defined as:

 .lol {

      background-color: red;

  }

According to the documentation, the third argument should be the full URL of the original HTML where you extracted the chunk from. Since using localhost/3232 didn't work, I uploaded my app online. Despite this, I can't seem to get the CSS applied to the generated HTML. The documentation also suggests adding:

       <HEAD> <BASE HREF="full url to your html file"> </HEAD>

And then utilizing this method:

        pdfConverter.GetPdfBytesFromHtmlString(String html);

However, none of the above attempts have successfully applied CSS. Any ideas on how to fix this issue?

Answer №1

In my experience with evoPDF, I have found that it is best to avoid having the html in an external file and instead inline the styles in the head of the document. When setting up the PDF generator for Careers 2.0, we discovered that live urls behind a web server were necessary, rather than just relative links in the same directory structure. It's important to note that there is a timeout in evo pdf that can result in loss of images if loading takes too long, so it's beneficial to inline everything.

It is also advisable to pass fully valid HTML, rather than just snippets needed to generate the view. Our version of evoPDF essentially hoists a browser instance and captures a screenshot, rendering differently depending on the doctype used.

Answer №2

While working with EvoPdf, I encountered an issue which was resolved by referencing the baseURL from web.config file. Instead of using HttpContext.Current.Request.Url.AbsoluteUri, I specified the baseURL in the appSettings section as shown below:

<appSettings>
    <add key="baseURL" value="http://your-domain.com/" />
</appSettings>

By setting the baseURL explicitly, the CSS rendering issue was fixed. This approach also works seamlessly with HTTPS. During testing, you can hard code the URL without relying on ConfigurationManager.

TextWriter outTextWriter = new StringWriter();

Server.Execute("Page1.aspx", outTextWriter);
Server.Execute("Page2.html", outTextWriter);

string htmlStringToConvert = outTextWriter.ToString();
outTextWriter.Close();

// Use the base URL specified in appSettings
string baseUrl = ConfigurationManager.AppSettings["baseURL"].ToString();

// Convert the HTML string to a PDF document in memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlStringToConvert, baseUrl);

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

Switching up the size of individual components in the Bootstrap grid system: The ultimate guide

Is it possible to adjust the width of specific elements in Bootstrap's grid system without affecting the default customization? Here is an example of what I am referring to: For example, I would like to make col-xs-1 smaller as seen in the top row ( ...

Extracting unique text values from nested div elements within list items using Selenium with Python

Within the HTML code provided, I am looking to extract the text of three variables (descr_confezione, aic_farmaco, and stato_confezione) for each of the four list items: <ul id="ul_lista_confezioni"> <li style="display: list-item;"> &l ...

Is it possible to use JavaScript to toggle mute and unmute functions on an iPad?

Seeking assistance with managing audio on an iPad using JavaScript (mute/unmute). Any suggestions or advice would be greatly appreciated. ...

Unable to delete the margin of Material-ui TextField

Having trouble removing the padding around material-ui's TextField component when using styled-components and inline styling. Any solutions to this issue? Interestingly, the inline width property is functioning correctly, unlike the styled-component w ...

Implementing smooth transitions on iPad screens with responsive text

I am currently working on a personal project that involves creating images with hover transitions. The transition effect works perfectly on all browsers except for iPads. I'm facing an issue where I can't click on the image due to the presence of ...

"Divs of the same type automatically adjust size to fit within the

I am currently exploring the possibility of creating a versatile component that can be customized based on the needs of its parent element, whether through bootstrap or traditional flexbox / CSS grid. I want to determine the level of reusability this compo ...

Emphasize specific letters in a word by making them bold, according to the user

In my app, there is a search feature that filters data based on user input and displays a list of matching results. I am trying to make the text that was searched by the user appear bold in the filtered data. For example, if the user searches 'Jo&apos ...

Permit the use of HTML tags within a CSS-only tooltip

I have created a unique CSS tooltip. <span data-tooltip="The security code is a 3-digit number<br />on the back of your Visa or Mastercard debit or credit card, or a 4-digit number on the front of your American Express card.">Help</span> ...

"Enhance Your Design with Hovering CSS Backgrounds

Seeking assistance with changing the background image on span hover. If anyone can assist, I have included the complete code for the website below. Take a look at the full website code here: Pastebin CSS Pastebin JavaScript ...

Unable to implement Bootstrap 5 Hamburger Animation - Looking for solutions

SOLVED I successfully resolved the issue :) The challenge was: $(function () { $('.navbar-toggler-button').on('click', function () { $('.animated-hamburger').toggleClass('open'); //Chrome expert mode ...

Issues with displaying iframes on iOS devices, particularly iPhones

There is a strange issue I am encountering with iframes not displaying on certain phones while working perfectly on others. (They show up fine on all android devices) (However, they do not work on iphone 6 and 7 but surprisingly work on 7 plus, 7S, and a ...

The functionality of the code in a stack snippet may differ from that in a standalone HTML file

My code works perfectly on a stack snippet, but when I insert it into my server or an .html file, the refresh button shrinks! I have copied and pasted the code exactly as it is. Are there any specific snippet features that need to be added for it to work, ...

What is the best way to retrieve the identifier of the checkbox that was recently selected by the user with jQuery?

My asp.net repeater is binding the member id to the checkbox id within a fieldcontain of jquery mobile. <div data-role="fieldcontain" style="margin: 0px"> <fieldset data-role="controlgroup"> <asp:Repeater ID="rpt" runat="server" ...

How to position an element to the right in Bootstrap 4 without causing it to move to a new line immediately?

As a newcomer to Bootstrap, I have come across only one individual who has raised this issue before. Unfortunately, they did not receive a satisfactory solution, prompting me to bring up the question once more. My dilemma involves two button groups. When ...

SQL Server database is showing question marks instead of HTML symbols

Recently, I observed that special HTML symbols like: ★ are appearing as question marks in my database records. I have set the type of the field as varchar and am utilizing Microsoft SQL 2008 for my database management. Is there a solution to this symbo ...

Display a button within a table depending on the content of adjacent cells

Below is the code snippet I'm currently working with: <tbody *ngIf="packages"> <tr *ngFor="let package of packages"> <td class='checkbox'> <label class="css-control css-co ...

The text box is intersecting with the photo

Struggling to craft a clean layout with an upvote button, I encountered an issue where the textarea appears to overlap with the image. Below is my sample code snippet: .my-component-row { display: flex; justify-content: center; align-items: center ...

Constructing a horizontal slider using vertically floating divs

Struggling to create a basic horizontal image slider using overflow:hidden and floating divs. Despite efforts, the divs keep stacking vertically instead of horizontally. Numerous online examples have been attempted without success. HTML: <div id="slid ...

Struggling with implementing responsive mobile buttons that stack in a column? Here's the solution

My content is nearly responsive on all screen sizes, but I'm having trouble getting the buttons to stack neatly in a column. Can anyone provide suggestions on how to fix this issue? You can view the current progress below. Thank you in advance for any ...

What is the process of adding background color to text?

Is there a more efficient way to style this text without relying on the span tag? I am looking to add a background color to my text. Code: .subject { color: gold; background: gray; } <h2>Some Subjects</h2> <ol> <li style="bac ...