Chụp xem render (html) như là một chuỗi và vượt qua nó để Winnovative chương trình tạo PDF mà là một thực sự công cụ tốt giúp bảo toàn định dạng phức tạp. Đây là đoạn mã để tạo PDF từ html bắt
string test="\r\n\r\n<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\r\n\r\n<html xmlns=\"http://www.w3.org/1999/xhtml\" >\r\n<head>\r\n <link rel=\"Stylesheet\" href=\"../../Content/style.css\" type=\"text/css\" />\r\n <title>Cover Page</title>\r\n <style type=\"text/css\">\r\n html, body\r\n {\r\n\t font-family: Arial, Helvetica, sans-serif;\r\n\t font-size: 13pt;\r\n\t padding: 0px;\r\n\t margin: 0px;\r\n\t background-color: #FFFFFF;\r\n\t color: black;\r\n\t width: 680px;\r\n }\r\n </style>\r\n</head>\r\n<body>\r\n <div>\r\n Ssotest Ssotest, \r\n </div> \r\n</body>\r\n</html>\r\n"
FileStreamResponseContext response = new FileStreamResponseContext();
Response .clear();
Document doc = new Document();
doc.DocumentInformation.CreationDate = DateTime.Now;
doc.DocumentInformation.Title = "Test Plan";
doc.DocumentInformation.Subject = "Test Plan";
doc.CompressionLevel = CompressionLevel.NormalCompression;
doc.Margins = new Margins(0, 0, 0, 0);
doc.Security.CanPrint = true;
doc.ViewerPreferences.HideToolbar = false;
doc.ViewerPreferences.FitWindow = false;
string baseUrl = String.Format("http://localhost{0}/", Request.Url.Port == 80?"":":" + Request.Url.Port.ToString());
PdfPage docTestPlan = doc.AddPage(PageSize.Letter, new Margins(0, 0, 0, 0), PageOrientation.Portrait);
// passing the string test returned from the string writer
HtmlToPdfElement htmlToPdf = new HtmlToPdfElement(test, baseUrl);
htmlToPdf.FitWidth = false;
docTestPlan.AddElement(htmlToPdf);
/******************************************
* put doc in a memory stream for return */
response.FileDataStream = new MemoryStream();
doc.Save(response.FileDataStream);
doc.Close();
response.FileDataStream.Position = 0;
return new FileStreamResult(response.FileDataStream, "application/pdf");
If you need to capture the controller action follow my post here.[http://stackoverflow.com/questions/5553674/capturing-html-to-string-in-mvc2-0][1]
Bạn có thể nhận được các mã số mẫu từ trang web chính thức của tác giả Winnovative PDF.
"plz giúp tôi giải quyết vấn đề này" - trong việc giải quyết vấn đề, chính xác? –
@Tom tạo pdf từ html phức tạp hoặc trên bay hoặc từ các tệp html đã lưu cần lưu giữ tất cả định dạng được xác định qua css –
Có thể là một ý tưởng hay để liệt kê những gì bạn đã cố gắng để người khác không đăng câu trả lời những thứ bạn đã từ chối. – Buildstarted