2016-08-01 176 views
0

我正在使用Select.HtmltoPdf。 下面ismy代碼:Html轉換爲PDF的PDF轉換

  var directory = @"D:\Folder\"; 
     html = html.Replace("/Content", HttpContext.Request.Url.GetLeftPart(UriPartial.Authority) + HttpContext.Request.ApplicationPath + "/Content"); 
     var filePath = directory + Guid.NewGuid() + ".pdf"; 
     var converter = new HtmlToPdf(1536,1536); 

     converter.Options.EmbedFonts = false; 
     converter.Options.ExternalLinksEnabled = true; 
     converter.Options.InternalLinksEnabled = true; 
     converter.Options.KeepTextsTogether = false; 
     converter.Options.KeepImagesTogether = false; 
     converter.Options.JavaScriptEnabled = true; 

     var pdf = converter.ConvertHtmlString(html, Request.Url.Scheme + "://" + Request.Url.Authority + 
Request.ApplicationPath.TrimEnd('/') + "/"); 

     pdf.Save(filePath); 

我的CSS是在內容/的site.css 拿到HTML,我只是用$( 「#的PrintPage」)HTML()。然後通過Scripts/xx/custom/common.js上的ajax調用將其傳遞給控制器​​。問題是它看起來不像它在瀏覽器上那樣。沒有css被加載。我如何使用select.htmltopdf來呈現css呢?或者如果有其他可用的軟件包可以做同樣的事情。由於

回答

1

要解決我的問題:

因爲我只得到了一定div的html的在我的頁面中,HTML,封頭,HTML的身體部位的arent包括在內。

我做什麼將它們串聯,並通過

<link rel="stylesheet">..... 

<link rel="stylesheet">使用的CSS .....並添加必要的標記

0

我使用的CSS打印所有頁面:

在頭:

<script language="Javascript"> 
    function print(){window.print();} 
</script> 

要打印:

<a href="javascript:window.print()" class="btn_result" > 
    Print 
</a> 

如果我不想要的東西打印我這樣做:

@media print { 
.print_invisible{display:none;} 
} 

display:nonevisibily:hidden,這取決於情況。