Cannot apply CSS to the html string

你。 提交于 2019-12-05 08:22:46

While it's possible to have the html in an external file with evoPDF, I don't recommend it. Instead just inline the styles in the head of the document. When we were setting up the PDF generator for Careers 2.0, I remember that the urls had to be live urls, behind a web server, not just relative link in the same directory structure. There is also a timeout in evo pdf that can cause loss of images if loading takes too long, which also plays nicer with inline everything.

I also recommend passing it fully valid html, not just the snippets you need to generate the view. Behind the scenes (in our version of evoPDF at least), it's just hoisting a browser instance and taking a screenshot. They render a little differently depending on the doctype.

I ran into this issue with EvoPdf. The fix for me was to reference the baseURL from web.config. I was trying to use HttpContext.Current.Request.Url.AbsoluteUri; which worked in two environments but didn't when tested on another server.

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

So you set it specifically and the CSS shows correctly after the change. Works with https too. You can hard code it for testing and not use ConfigurationManager.

        TextWriter outTextWriter = new StringWriter();

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

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

        // Use the current page URL as base URL
        string baseUrl = ConfigurationManager.AppSettings["baseURL"].ToString(); //HttpContext.Current.Request.Url.AbsoluteUri;

        // Convert the page HTML string to a PDF document in a memory buffer
        byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlStringToConvert, baseUrl);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!