wkhtmltopdf - convert html code to pdf directly in C#

前端 未结 5 1217
失恋的感觉
失恋的感觉 2021-01-02 10:42

I know this tool looks up on a url and converts the repsponse to pdf. How do I convert a

 content..  

into a pdf?

5条回答
  •  心在旅途
    2021-01-02 11:31

    if you want to print specific part of html with Css

    install Nuget packages:

    Pechkin.Synchronized

    StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
      pnlprint.RenderControl(hw);
        StringReader sr = new StringReader(sw.ToString());
       string cssPath = Server.MapPath("~/css/style1.css");
    
        string cssString = File.ReadAllText(cssPath);
         cssPath = Server.MapPath("~/css/bootstrap.css");
        string cssString2 = File.ReadAllText(cssPath);
        cssString += cssString2;
        GlobalConfig gc = new GlobalConfig();
        byte[] pdfContent = new SimplePechkin(new GlobalConfig()).Convert(@"" + sw.ToString() + "");
    

    here you can easily understand the working of wkhtmltopdf https://ourcodeworld.com/articles/read/366/how-to-generate-a-pdf-from-html-using-wkhtmltopdf-with-c-in-winforms

提交回复
热议问题