Using the NuGet RazorPDF2 package:
link
This package is mine. Anything is just sending me a bug message, mentioning me in the chat or commenting on any of the questions I've answered, or asking questions about the use.
Example using HTML Views
Controller
public ActionResult Exemplo(int id)
{
var model = context.Registros.FirstOrDefault(x => x.RegistroId == id);
return new PdfActionResult(model);
}
Layout View (View / Shared / _PdfLayout.cshtml)
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Meu Sistema</title>
<style>
html {
font-family: Arial;
}
h1 {
color: blue;
}
table {
border: 1px solid black;
border-spacing: 0px;
}
table tr th {
background-color: gray;
color: white;
padding: 2px;
}
table tr td {
margin: 0px;
padding: 2px;
}
</style>
</head>
<body>
@RenderBody()
</body>
</html>
View
Make an HTML View and normal Razor. Do not forget to specify Layout as follows:
@{
ViewBag.Title = "Título";
Layout = "~/Views/Shared/_PdfLayout.cshtml";
}
Example using iTextSharp 4 tags
Controller
public RazorPDF.PdfResult PdfAction(int id)
{
// Sua lógica para carregar as informações na variável "modelo"
return new RazorPDF.PdfResult(modelo, "PdfAction");
}
Views \ SeuController \ PdfAction.cshtml
@model SeuProjeto.Models.SeuModelo
@{
Layout = "~/Views/Shared/_PdfLayout.cshtml";
}
<paragraph style="font-family:Helvetica;font-size:18;font-style:italic;">
<chunk style="font-weight:bold;">Seu Título</chunk>
</paragraph>
<paragraph style="font-family:Helvetica;">
<chunk>Algumas palavras de cabeçalho</chunk>
</paragraph>
<table width="100%" cellpadding="0.5" cellspacing="0.5" widths="30;60" borderwidth="1.0" left="false" right="false" top="false" bottom="false" red="0" green="0" blue="0">
<row>
<cell>
<chunk style="font-weight:bold;">Seu Label</chunk>
</cell>
<cell>
<chunk style="">@Model.SuaInformacao</chunk>
</cell>
</row>
</table>
_PdfLayout.cshtml
<itext creationdate="@DateTime.Now.ToString()" producer="RazorPDF">
@RenderBody()
</itext>