1 [HttpGet, Route("CreatePdf")]
2 public Response CreatePdf()
3 {
4 Response resp = new Response();
5 resp.StartTime = DateTime.Now;
6 try
7 {
8 string pdfPath = @"D:\temp.pdf";
9
10 //.NET Core 在默认情况下是没有注册EncodeProvider,需要我们手动自己去注册
11 //需要先添加CodePagesEncoingProvider的引用,在NuGet包System.Text.Encoding.CodePages里面
12 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
13
14 Document document = new Document();
15 BaseFont bf = BaseFont.CreateFont(@"C:\Windows\Fonts\MSYH.TTC,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
16 Font f = new Font(bf, 9);
17 PdfWriter.GetInstance(document, new FileStream(pdfPath, FileMode.Create));
18 document.Open();
19 Paragraph paragraph = new Paragraph("Hello World! 你好", f);
20 document.Add(paragraph);
21 document.Close();
22
23 resp.FilePath = pdfPath;
24 }
25 catch (Exception ex)
26 {
27 //throw ex;
28 resp.Message = ex.Message;
29 }
30
31 resp.EndTime = DateTime.Now;
32
33 return resp;
34 }