Solución:
Puede utilizar Free Html To Pdf Converter de SelectPdf (http://selectpdf.com/community-edition/).
El código para MVC se ve así:
[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;
}
La versión VB.NET MVC del código se puede encontrar aquí: http://selectpdf.com/convert-from-html-to-pdf-in-asp-net-mvc-csharp-and-vb-net/
En breve:
Procesador HTML para PDF usando PdfSharp
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;
}
Respuesta más detallada
El código C # a continuación se puede utilizar en una aplicación MVC para convertir la vista actual a PDF y producir un PDF en un búfer que se puede guardar en el servidor o enviar al navegador para su descarga. El código utiliza la biblioteca evopdf para .net para realizar la conversión de HTML a 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;
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)