我正在使用以下代码在.NET Core API中生成并保存PDF文档。它可以工作并生成PDF,但是PDF缺少了HTML本身包含的所有样式。如何链接CSS文件或启用HTML中的CSS渲染?
[HttpGet]
public ActionResult GeneratePDF()
{
string contentRootPath = _hostingEnvironment.ContentRootPath;
var folderName = Path.Combine(contentRootPath, FileHandler.InvoicesAttachmentsPath);
if (!Directory.Exists(folderName))
Directory.CreateDirectory(folderName);
string html = PDF.GetPdfHTMLString();
StringReader sr = new StringReader(html.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
using (MemoryStream memoryStream = new MemoryStream())
{
//PdfWriter writer = PdfWriter.GetInstance(pdfDoc, memoryStream, FileMode.Create);
PdfWriter.GetInstance(pdfDoc, new FileStream(Path.Combine(folderName, "mypdf.pdf"), FileMode.Create));
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
}
return Ok("");
}