Im working in a project which requires current html page to convert in pdf and that pdf will automatically save on button click on server and its reference will be save in database.I can convert the view if its data comes from data base but the data in this form is static that means on the view it has so many radio button and text box in which i can write the detail and check the check box on after clicking save button it will save on the server and and its reference will save in the data base.
我在一个项目中工作,需要当前的html页面转换为pdf,pdf将自动保存在服务器上的按钮点击,它的参考将保存在数据库中。如果数据来自数据库但数据可以转换视图这个表单是静态的,这意味着它在视图中有这么多单选按钮和文本框,我可以在其中编写详细信息并在单击保存按钮后选中复选框,它将保存在服务器上,并且其引用将保存在数据中基础。
the reason is that im not saving the data in database is that the report is less use full for the client but if i save the data in data base then the database become very huge and its become complicate to handle. because the report has approx 100 fields. so please if any one can help me in it.
原因是我不保存数据库中的数据是报告对客户端的使用较少,但如果我将数据保存在数据库中,那么数据库变得非常庞大并且处理变得复杂。因为该报告有大约100个字段。所以,如果任何人可以帮助我。
6 个解决方案
#1
4
You can use the Free Html To Pdf Converter from SelectPdf (http://selectpdf.com/community-edition/).
您可以使用SelectPdf的免费Html到Pdf转换器(http://selectpdf.com/community-edition/)。
Code for MVC looks like this:
MVC的代码如下所示:
[HttpPost]
public ActionResult Convert(FormCollection collection)
{
// read parameters from the webpage
string url = collection["TxtUrl"];
string pdf_page_size = collection["DdlPageSize"];
PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);
string pdf_orientation = collection["DdlPageOrientation"];
PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(
typeof(PdfPageOrientation), pdf_orientation, true);
int webPageWidth = 1024;
try
{
webPageWidth = System.Convert.ToInt32(collection["TxtWidth"]);
}
catch { }
int webPageHeight = 0;
try
{
webPageHeight = System.Convert.ToInt32(collection["TxtHeight"]);
}
catch { }
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
// set converter options
converter.Options.PdfPageSize = pageSize;
converter.Options.PdfPageOrientation = pdfOrientation;
converter.Options.WebPageWidth = webPageWidth;
converter.Options.WebPageHeight = webPageHeight;
// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);
// save pdf document
byte[] pdf = doc.Save();
// close pdf document
doc.Close();
// return resulted pdf document
FileResult fileResult = new FileContentResult(pdf, "application/pdf");
fileResult.FileDownloadName = "Document.pdf";
return fileResult;
}
VB.NET MVC version of the code can be found here: http://selectpdf.com/convert-from-html-to-pdf-in-asp-net-mvc-csharp-and-vb-net/
VB.NET MVC版本的代码可以在这里找到:http://selectpdf.com/convert-from-html-to-pdf-in-asp-net-mvc-csharp-and-vb-net/
#2
3
In short:
简而言之:
HTML Renderer for PDF using PdfSharp
使用PdfSharp的PDF HTML呈现器
public static Byte[] PdfSharpConvert(String html)
{
Byte[] res = null;
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
return res;
}
更详细的答案
#3
2
There is special nuget package RazorPDF. It simple works. RazorPDF site
有特殊的nuget包RazorPDF。它很简单。 RazorPDF网站
#4
2
The C# code below can be used in a MVC application to convert the current view to PDF and produce a PDF in a buffer that can be saved on server or sent to browser for download. The code is using evopdf library for .net to perform the HTML to PDF conversion:
下面的C#代码可以在MVC应用程序中使用,将当前视图转换为PDF并在缓冲区中生成PDF,可以保存在服务器上或发送到浏览器进行下载。代码使用.net的evopdf库来执行HTML到PDF的转换:
[HttpPost]
public ActionResult ConvertCurrentPageToPdf(FormCollection collection)
{
object model = null;
ViewDataDictionary viewData = new ViewDataDictionary(model);
// The string writer where to render the HTML code of the view
StringWriter stringWriter = new StringWriter();
// Render the Index view in a HTML string
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Index", null);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewData,
new TempDataDictionary(),
stringWriter
);
viewResult.View.Render(viewContext, stringWriter);
// Get the view HTML string
string htmlToConvert = stringWriter.ToString();
// Get the base URL
String currentPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Convert_Current_Page/ConvertCurrentPageToPdf".Length);
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Convert the HTML string to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Convert_Current_Page.pdf";
return fileResult;
}
#5
1
There are a number of html to pdf converters for .NET available out there. I can recommend ExpertPdf (www.html-to-pdf.net).
有许多用于.NET的html到pdf转换器。我可以推荐ExpertPdf(www.html-to-pdf.net)。
Code looks something like this:
代码看起来像这样:
PdfConverter pdfConverter = new PdfConverter();
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(url);
There is an online demo here: http://www.html-to-pdf.net/free-online-pdf-converter.aspx
这里有一个在线演示:http://www.html-to-pdf.net/free-online-pdf-converter.aspx
#6
1
use ABCpdf
dll, here in text area we can write html code and on a button click corresponding pdf will be shown.ABCpdf
trail version is easily available for downloding, adding linlk to download the ABCpdf
dll https://www.websupergoo.com/download.htm
使用ABCpdf dll,这里在文本区域我们可以编写html代码并在按钮上单击相应的pdf将显示.ABCpdf跟踪版本可以轻松下载,添加linlk下载ABCpdf dll https://www.websupergoo.com/ download.htm
index.cshtml
index.cshtml
@using (Html.BeginForm("covertopdf", "simple", FormMethod.Post))
{
<p style="margin-top:50px">
Input Html: @Html.TextArea("Htmlcontent", new { @class = "form-control",@cols="160" , @rows="20"})<br />
<input type="submit" class="btn-primary" value="Convertopdf" />
</p>
}
SimpleController.cs
SimpleController.cs
public class SimpleController : Controller
{
public class FileViewModel
{
public byte[] Content { get; set; }
public string Extension { get; set; }
public string FileName { get; set; }
}
[HttpPost]
[ValidateInput(false)]
public FileStreamResult covertopdf(string Htmlcontent)
//public FileStreamResult covertopdf(file fo)
{
var result = ExecuteAction(() =>
{
var fileViewmodel = new FileViewModel
{
Content = ConvertHtmlToPdf(Htmlcontent),
//Content= ConvertHtmlToPdf(fo.cont),
Extension = "application/pdf",
FileName = "Policy Information.pdf"
};
return fileViewmodel;
}, "covertopdf");
// return result;
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
// Content is the file
Stream stream = new MemoryStream(result.Content);
return new FileStreamResult(stream, "application/pdf")
{
};
}
public T ExecuteAction<T>(Func<T> action, string method)
{
try
{
return action.Invoke();
}
catch (Exception ex)
{
return default(T);
}
}
protected byte[] ConvertHtmlToPdf(string html, string header = null, string footer = null, bool isPageNumberInFooter = false)
{
// Create ABCpdf Doc object
var doc = new Doc();
if (header == null && footer == null)
doc.Rect.Inset(20, 20);
else
doc.Rect.String = "0 70 600 760"; /*padding from left, padding from bottom, width from left, height from bottom*/
// Add html to Doc
//html = "<html><head></head><body></body></html>";
int theId = doc.AddImageHtml(html);
// Loop through document to create multi-page PDF
while (true)
{
if (!doc.Chainable(theId))
break;
doc.Page = doc.AddPage();
theId = doc.AddImageToChain(theId);
}
var count = doc.PageCount;
/*****************Footer area******************/
if (footer != null)
{
var newfooter = "";
doc.Rect.String = "40 20 580 50";
for (int i = 1; i <= count; i++)
{
doc.PageNumber = i;
if (isPageNumberInFooter)
{
newfooter = footer.Replace("PageNumber", "Page " + i.ToString() + " of " + count.ToString());
int id = doc.AddImageHtml(newfooter);
while (true)
{
if (!doc.Chainable(id))
break;
id = doc.AddImageToChain(id);
}
}
else
doc.AddText(footer);
}
}
/*****************Footer area******************/
// Flatten the PDF
for (int i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.Flatten();
}
var pdf = doc.GetData();
doc.Clear();
// Get PDF as byte array. Couls also use .Save() to save to disk
return pdf;
}
}
#1
4
You can use the Free Html To Pdf Converter from SelectPdf (http://selectpdf.com/community-edition/).
您可以使用SelectPdf的免费Html到Pdf转换器(http://selectpdf.com/community-edition/)。
Code for MVC looks like this:
MVC的代码如下所示:
[HttpPost]
public ActionResult Convert(FormCollection collection)
{
// read parameters from the webpage
string url = collection["TxtUrl"];
string pdf_page_size = collection["DdlPageSize"];
PdfPageSize pageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pdf_page_size, true);
string pdf_orientation = collection["DdlPageOrientation"];
PdfPageOrientation pdfOrientation = (PdfPageOrientation)Enum.Parse(
typeof(PdfPageOrientation), pdf_orientation, true);
int webPageWidth = 1024;
try
{
webPageWidth = System.Convert.ToInt32(collection["TxtWidth"]);
}
catch { }
int webPageHeight = 0;
try
{
webPageHeight = System.Convert.ToInt32(collection["TxtHeight"]);
}
catch { }
// instantiate a html to pdf converter object
HtmlToPdf converter = new HtmlToPdf();
// set converter options
converter.Options.PdfPageSize = pageSize;
converter.Options.PdfPageOrientation = pdfOrientation;
converter.Options.WebPageWidth = webPageWidth;
converter.Options.WebPageHeight = webPageHeight;
// create a new pdf document converting an url
PdfDocument doc = converter.ConvertUrl(url);
// save pdf document
byte[] pdf = doc.Save();
// close pdf document
doc.Close();
// return resulted pdf document
FileResult fileResult = new FileContentResult(pdf, "application/pdf");
fileResult.FileDownloadName = "Document.pdf";
return fileResult;
}
VB.NET MVC version of the code can be found here: http://selectpdf.com/convert-from-html-to-pdf-in-asp-net-mvc-csharp-and-vb-net/
VB.NET MVC版本的代码可以在这里找到:http://selectpdf.com/convert-from-html-to-pdf-in-asp-net-mvc-csharp-and-vb-net/
#2
3
In short:
简而言之:
HTML Renderer for PDF using PdfSharp
使用PdfSharp的PDF HTML呈现器
public static Byte[] PdfSharpConvert(String html)
{
Byte[] res = null;
using (MemoryStream ms = new MemoryStream())
{
var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
pdf.Save(ms);
res = ms.ToArray();
}
return res;
}
更详细的答案
#3
2
There is special nuget package RazorPDF. It simple works. RazorPDF site
有特殊的nuget包RazorPDF。它很简单。 RazorPDF网站
#4
2
The C# code below can be used in a MVC application to convert the current view to PDF and produce a PDF in a buffer that can be saved on server or sent to browser for download. The code is using evopdf library for .net to perform the HTML to PDF conversion:
下面的C#代码可以在MVC应用程序中使用,将当前视图转换为PDF并在缓冲区中生成PDF,可以保存在服务器上或发送到浏览器进行下载。代码使用.net的evopdf库来执行HTML到PDF的转换:
[HttpPost]
public ActionResult ConvertCurrentPageToPdf(FormCollection collection)
{
object model = null;
ViewDataDictionary viewData = new ViewDataDictionary(model);
// The string writer where to render the HTML code of the view
StringWriter stringWriter = new StringWriter();
// Render the Index view in a HTML string
ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, "Index", null);
ViewContext viewContext = new ViewContext(
ControllerContext,
viewResult.View,
viewData,
new TempDataDictionary(),
stringWriter
);
viewResult.View.Render(viewContext, stringWriter);
// Get the view HTML string
string htmlToConvert = stringWriter.ToString();
// Get the base URL
String currentPageUrl = this.ControllerContext.HttpContext.Request.Url.AbsoluteUri;
String baseUrl = currentPageUrl.Substring(0, currentPageUrl.Length - "Convert_Current_Page/ConvertCurrentPageToPdf".Length);
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Convert the HTML string to a PDF document in a memory buffer
byte[] outPdfBuffer = htmlToPdfConverter.ConvertHtml(htmlToConvert, baseUrl);
// Send the PDF file to browser
FileResult fileResult = new FileContentResult(outPdfBuffer, "application/pdf");
fileResult.FileDownloadName = "Convert_Current_Page.pdf";
return fileResult;
}
#5
1
There are a number of html to pdf converters for .NET available out there. I can recommend ExpertPdf (www.html-to-pdf.net).
有许多用于.NET的html到pdf转换器。我可以推荐ExpertPdf(www.html-to-pdf.net)。
Code looks something like this:
代码看起来像这样:
PdfConverter pdfConverter = new PdfConverter();
pdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
byte[] downloadBytes = pdfConverter.GetPdfFromUrlBytes(url);
There is an online demo here: http://www.html-to-pdf.net/free-online-pdf-converter.aspx
这里有一个在线演示:http://www.html-to-pdf.net/free-online-pdf-converter.aspx
#6
1
use ABCpdf
dll, here in text area we can write html code and on a button click corresponding pdf will be shown.ABCpdf
trail version is easily available for downloding, adding linlk to download the ABCpdf
dll https://www.websupergoo.com/download.htm
使用ABCpdf dll,这里在文本区域我们可以编写html代码并在按钮上单击相应的pdf将显示.ABCpdf跟踪版本可以轻松下载,添加linlk下载ABCpdf dll https://www.websupergoo.com/ download.htm
index.cshtml
index.cshtml
@using (Html.BeginForm("covertopdf", "simple", FormMethod.Post))
{
<p style="margin-top:50px">
Input Html: @Html.TextArea("Htmlcontent", new { @class = "form-control",@cols="160" , @rows="20"})<br />
<input type="submit" class="btn-primary" value="Convertopdf" />
</p>
}
SimpleController.cs
SimpleController.cs
public class SimpleController : Controller
{
public class FileViewModel
{
public byte[] Content { get; set; }
public string Extension { get; set; }
public string FileName { get; set; }
}
[HttpPost]
[ValidateInput(false)]
public FileStreamResult covertopdf(string Htmlcontent)
//public FileStreamResult covertopdf(file fo)
{
var result = ExecuteAction(() =>
{
var fileViewmodel = new FileViewModel
{
Content = ConvertHtmlToPdf(Htmlcontent),
//Content= ConvertHtmlToPdf(fo.cont),
Extension = "application/pdf",
FileName = "Policy Information.pdf"
};
return fileViewmodel;
}, "covertopdf");
// return result;
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
// Content is the file
Stream stream = new MemoryStream(result.Content);
return new FileStreamResult(stream, "application/pdf")
{
};
}
public T ExecuteAction<T>(Func<T> action, string method)
{
try
{
return action.Invoke();
}
catch (Exception ex)
{
return default(T);
}
}
protected byte[] ConvertHtmlToPdf(string html, string header = null, string footer = null, bool isPageNumberInFooter = false)
{
// Create ABCpdf Doc object
var doc = new Doc();
if (header == null && footer == null)
doc.Rect.Inset(20, 20);
else
doc.Rect.String = "0 70 600 760"; /*padding from left, padding from bottom, width from left, height from bottom*/
// Add html to Doc
//html = "<html><head></head><body></body></html>";
int theId = doc.AddImageHtml(html);
// Loop through document to create multi-page PDF
while (true)
{
if (!doc.Chainable(theId))
break;
doc.Page = doc.AddPage();
theId = doc.AddImageToChain(theId);
}
var count = doc.PageCount;
/*****************Footer area******************/
if (footer != null)
{
var newfooter = "";
doc.Rect.String = "40 20 580 50";
for (int i = 1; i <= count; i++)
{
doc.PageNumber = i;
if (isPageNumberInFooter)
{
newfooter = footer.Replace("PageNumber", "Page " + i.ToString() + " of " + count.ToString());
int id = doc.AddImageHtml(newfooter);
while (true)
{
if (!doc.Chainable(id))
break;
id = doc.AddImageToChain(id);
}
}
else
doc.AddText(footer);
}
}
/*****************Footer area******************/
// Flatten the PDF
for (int i = 1; i <= doc.PageCount; i++)
{
doc.PageNumber = i;
doc.Flatten();
}
var pdf = doc.GetData();
doc.Clear();
// Get PDF as byte array. Couls also use .Save() to save to disk
return pdf;
}
}