gmanny / Pechkin

.NET Wrapper for WkHtmlToPdf static DLL. Allows you to utilize full power of the library.
401 stars 128 forks source link

External CSS not working #66

Open emiliepoccioli opened 10 years ago

emiliepoccioli commented 10 years ago

Hi, My HTML files use external CSS links such as : (My application uses MVC 5) I am struggling to get the CSS intepreted in the PDF as it seems to not recognize the path. On the C# side I added SetUserStylesheetUri(cssPath) that doesn't seem to do anything Here is the code on the C# side: public void GeneratePDF(string htmlContent) {

         string cssPath = Server.MapPath("~/Content/Test.css");
         byte[] pdf = new Pechkin.Synchronized.SynchronizedPechkin(
         new Pechkin.GlobalConfig()).Convert(
             new Pechkin.ObjectConfig()
            .SetLoadImages(true)
            .SetPrintBackground(true)
            .SetAllowLocalContent(true)
            .SetScreenMediaType(true)
            .SetUserStylesheetUri(cssPath)
            .SetCreateExternalLinks(true), htmlContent);

         using (FileStream file = System.IO.File.Create(@"C:\TEMP\Output.pdf"))
         {
             file.Write(pdf, 0, pdf.Length);
         }
     }

So, Can we generate PDF with external CSS links? I tried adding directly the CSS on my HTML pages to test if it works, and it did. But I can't put the entire CSS on my HTML pages If so, how can I do it with external stylesheets?

Thanks for your help!

bcampardou commented 9 years ago

Hi,

I had the same problem but it seems that it is not a bug. In fact the css are loaded in WkHtmlToPdf not with Pechkin so virtual path are wrong. You need to specify a complete Url or Uri.

With absolute Uri, it works for me !

emiliepoccioli commented 9 years ago

Thanks for your answer. I managed to make it work in a different way! Thanks

tvolge commented 8 years ago

@emiliepoccioli How did you managed to make it work?

endjinns commented 5 years ago

@emiliepoccioli How did you managed to make it work?

I got it working by reading my entire stylesheet into a string and then appending that to the HTML which gets fed into pechkin. Worked a treat!

string cssPath = Server.MapPath("~/Content/Site.css"); string cssString = `File.ReadAllText(cssPath); HTML = "" + HTML;

vishalpandeyknp commented 4 years ago

This pattern is not resolving in view when complete html is passed to pechkin when working with https: protocal. With http: all works fine.

   @Styles.Render(Url.AbsoluteUrl("Content/themes/base/css"))
   @Styles.Render(Url.AbsoluteUrl("Content/themes/css"))
   @Scripts.Render(Url.AbsoluteUrl("Scripts/library"))
   @Scripts.Render(Url.AbsoluteUrl("Scripts/Common"))