在C#中我试图从HTML自动生成和下载PDF,但它没有生成。
byte[] bytes = null;
MemoryStream memoryStream = new MemoryStream();
RetailerInvoice Data = new RetailerInvoice();
try
{
var _ResultId = dataTypeConverter.ConvertToGuid(RetailerId);
string TempHtml = "<html><head><title></title></head><bodyHello World</body></html>";
StringReader Sr = new StringReader(TempHtml);
Document pdfDoc = new Document(PageSize.A4,10f,10f,10f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writter = PdfWriter.GetInstance(pdfDoc, memoryStream);
pdfDoc.Open();
htmlparser.Parse(Sr);
pdfDoc.Close();
pdfDoc.Dispose();
Response.ContentType = "application/pdf";
Response.AddHeader("content-dispostion", "attachment;filename-Invoive.pdf");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.Write(pdfDoc);
Response.End();
bytes = memoryStream.ToArray();
memoryStream.Write(bytes, 0, bytes.Length);
memoryStream.Position = 0;
}
catch (Exception ex)
{
Data = null;
}
return new FileStreamResult(memoryStream, "application/pdf");