Using the Nuget Razorpdf2 package:
https://www.nuget.org/packages/RazorPDF2
This is my package. Anything you can do is just send me a message about bugs, mentioning me in chat or comment on any of the questions I answered about it, or ask questions about using.
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 a View in HTML and Razor normal. Don’t forget to specify the 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>
I have already installed by Package Manager Console , which reference should I add on the page for Razorpdf.Pdfresult to be read correctly?
– Joao Paulo
using RazorPDF;
should solve everything. In the example I put, nor need thisusing
.– Leonel Sanches da Silva
Forget it, I put it in the wrong place. It’s gone.
– Joao Paulo
+1: Very cool this Razor PDF.
– Miguel Angelo
Does this "id" variable really need it? And the "model" variable, how do I work with it? What does it need to have?
– Joao Paulo
Not,
id
is just one example.modelo
is a variable that can be a Model, Viewmodel, etc. You will use as the variable@model
of the View, as in a View Razor normal.– Leonel Sanches da Silva
Is it possible to generate with the html of my page, or do I have to assemble the pdf structure from the outside? And that code there using table Row and Cell, what code is that?
– Joao Paulo
There’s another Nuget package called
Rotativa
which generates the PDF from the generated HTML, but this package I do not recommend because it does not work in Azure. In any case, I will pass on the link: http://www.nuget.org/packages/Rotativa– Leonel Sanches da Silva
But responding, yes, you have to assemble the PDF structure using another Razor file.
row
andcell
are tags ofiTextSharp
, that assemble tables within the PDF.– Leonel Sanches da Silva
What is this markup language? From iTextSharp itself? I want to reproduce my document using it. Where do I find the documentation?
– Joao Paulo
It is the same documentation as iText. It does not exist in English. This tutorial exists: http://stderr.org/doc/libitext-java-doc/www/tutorial/ch07.html
– Leonel Sanches da Silva
Most of the documentation is focused on the use of C#, but I followed your line and assembled it by code. But I don’t know for example how to change the color of the bottom of a cell. You know?
– Joao Paulo
I don’t know if it’s right, but try
<cell red="0" green="0" blue="255">
– Leonel Sanches da Silva
@Remember this problem? I made a new package that admits HTML Views for the generation of Pdfs, Razorpdf2. If you want to do some tests, see the answer edition.
– Leonel Sanches da Silva
Here it didn’t work, I followed the above steps: I downloaded Razorpdf2 via Tools > Nuget Pachage Manager, installed iTextSharp 5.5.7, created Controller a View and partial, then an error occurs when rendering html: Details of the Exception: System.IO.Ioexception: The Document has no pages.
– hard123
@Adrianocorder This error is from iTextSharp, not Razorpdf2. Please open another question.
– Leonel Sanches da Silva