tft每日頭條

 > 圖文

 > netcore讀取pdf内容

netcore讀取pdf内容

圖文 更新时间:2024-08-15 19:16:04

  比如我們需要ASP.NET Core 中需要通過pdf來進行某些簡單的報表開發,随着這并不難,但還是會手忙腳亂的去搜索一些資料,那麼恭喜您,這篇帖子會幫助到您,我們就不會再去浪費一些寶貴的時間。

  在本文中我們将要使用DinkToPDF來處理我們在.NET Core Web 程序中進行構建PDF文檔!就現在我們不多說,直接開始有趣的部分。

前言

  您可以通過創建PDF文檔在我的倉庫中,獲取源代碼,歡迎給個免費的Star...

  現在我們創建一個.NET Core 3.0 項目,至于是mvc、API、這些我并不在意。創建項目後直接Nuget安裝DinkToPDF。随後您需要下載我的代碼倉庫中的“NativeLibrary”文件夾,在其中,我們将找到兩個文件32bit和64bit,因此我們需要為操作系統選擇合适的庫。我們将從64位文件夾中選擇文件。

  最後,我們需要啟動該庫,并且IOC DinkToPDF。

public void ConfigureServices(IServiceCollection services) { var context = new CustomAssemblyLoadContext(); context.LoadUnmanAgedLibrary(Path.Combine(Directory.GetCurrentdirectory(), "libwkhtmltox.dll")); services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools())); services.AddControllers(); }

建立實體

在真實情況的項目中,我們可以從數據庫中收集數據或從其他API接收數據。但是為了簡單起見,我們将從本地存儲中收集PDF文檔的數據。随後,我們将創建一個HTML模闆并将其存儲在PDF文檔中。

public class Employee { public string Name { get; set; } public string LastName { get; set; } public int Age { get; set; } public string Gender { get; set; } }

再随後,我們要創建一個新的文件夾Services和裡面兩類文件 DataServices.cs 和 TemplateGenerator.cs 。完整的結構應如下所示:

public class DataServices { public static List<Employee> GetAllEmployess() => new List<Employee> { new Employee { Name="Hao Zi Zhang", LastName="Turner", Age=35, Gender="Male"}, new Employee { Name="Yu Chen", LastName="Markus", Age=22, Gender="Female"}, new Employee { Name="Jian Zhi Chu", LastName="Martins", Age=40, Gender="Male"}, new Employee { Name="ElderJames", LastName="Packner", Age=30, Gender="Female"}, new Employee { Name="BlaZui", LastName="Doe", Age=45, Gender="Male"} }; }

其中添加服務中返回了某些數據,用于模拟服務。我們要生成一個HTML模闆,因此我們需要修改 TemplateGenerator.cs 文件:

public class TempleGenertor { public static string GetHTMLString() { var employees = DataServices.GetAllEmployess(); var sb = new StringBuilder(); sb.Append(@" <html> <head> </head> <body> <div class='header'><h1>This is the generated PDF report!!!</h1></div> <table align='center'> <tr> <th>Name</th> <th>LastName</th> <th>Age</th> <th>Gender</th> </tr>"); foreach (var emp in employees) { sb.AppendFormat(@"<tr> <td>{0}</td> <td>{1}</td> <td>{2}</td> <td>{3}</td> </tr>", emp.Name, emp.LastName, emp.Age, emp.Gender); } sb.Append(@" </table> </body> </html>"); return sb.ToString(); } }

如果想要指定css樣式,則可以創建某些文件夾,随後在API通過服務器路徑來抉擇配置。

.header { text-align: center; color: green; padding-bottom: 35px; } table { width: 80%; border-collapse: collapse; } td, th { border: 1px solid gray; padding: 15px; font-size: 22px; text-align: center; } table th { background-color: green; color: white; }

  就是這樣,我們有用于HTML創建的HTML模闆。現在,我們可以繼續執行Controller邏輯。

[Route("api/PdfCreator")] [ApiController] public class PdfCreatorController : ControllerBase { private IConverter _converter; public PdfCreatorController(IConverter converter) { _converter = converter; } [HttpGet] public IActionResult CreatePDF() { var globalSettings = new GlobalSettings { ColorMode = ColorMode.Color, Orientation = Orientation.Portrait, PaperSize = PaperKind.A4, Margins = new MarginSettings { Top = 10 }, DocumentTitle = "PDF Report" }; var objectSettings = new ObjectSettings { PagesCount = true, HtmlContent = TempleGenertor.GetHTMLString(), WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = Path.Combine(Directory.GetCurrentDirectory(), "assets", "style.css") }, HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }, FooterSettings = { FontName = "Arial", FontSize = 9, Line = true, Center = "Report Footer" } }; var pdf = new HtmlToPdfDocument() { GlobalSettings = globalSettings, Objects = { objectSettings } }; var file = _converter.Convert(pdf); return File(file, "application/pdf"); } }

代碼說明

在上面的代碼中,我們首先通過使用 IConverter 接口将注冊的 Converter 與依賴注入注入到構造函數中。然後,我們創建兩個對象 globalSettings , objectSettings 并将它們用作 HtmlToPdfDcoument 屬性中的配置。

現在讓我們來說說 GlobalSettings 和 ObjectSettings 類。

關于GlobalSettings類

它括了PDF文檔的整體配置屬性。我們僅使用其中的幾個屬性來設置顔色模式,方向,紙張尺寸,文檔标題等…但它還有還多屬性。

關于ObjectSettings類

ObjectSettings由相關的PDF文檔的内容的屬性。因此,我們可以配置頁面計數器的可見性,頁眉和頁腳的格式,文檔的正文内容(HtmlContent屬性)或的Web設置。

HtmlContent屬性是此類的非常重要的屬性。它包含我們生成的HTML模闆,并顯示PDF文檔的主體。

WebSettings也非常重要,尤其是如果我們有一個外部CSS文件來進行樣式設置時。在此屬性中,我們可以配置文檔的編碼并提供CSS文件的路徑。如果我們檢查此屬性,我們将發現更多可以配置的設置,例如PDF文檔的背景,文字大小 等等..

啟動項目

通過路由定位到我們的API中,重定向PDF打印界面。

netcore讀取pdf内容(.netcore中生成PDF方法)1

 一切看起來都是那麼完美,就這樣我們就可以輕松的在ASP.NET Core中構建PDF文檔并且還可以完美适配相關邏輯和某些文檔設置!!

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关圖文资讯推荐

热门圖文资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved