How to export an HTML/C#(Razor) page to PDF?

Asked

Viewed 985 times

3

I have some simple tables and need to export them to PDF. I found the library itextsharp, but in the compressed file there are many folders and files and do not know how to add the reference in my application.

I would like help to do the export, whether by this library or another, preferably showing how to add the reference and some example of code.

Later if necessary I add the code I need to export.

2 answers

2


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?

  • using RazorPDF; should solve everything. In the example I put, nor need this using.

  • Forget it, I put it in the wrong place. It’s gone.

  • 1

    +1: Very cool this Razor PDF.

  • Does this "id" variable really need it? And the "model" variable, how do I work with it? What does it need to have?

  • 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.

  • 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?

  • 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

  • But responding, yes, you have to assemble the PDF structure using another Razor file. row and cell are tags of iTextSharp, that assemble tables within the PDF.

  • What is this markup language? From iTextSharp itself? I want to reproduce my document using it. Where do I find the documentation?

  • 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

  • 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?

  • I don’t know if it’s right, but try <cell red="0" green="0" blue="255">

  • 1

    @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.

  • 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.

  • @Adrianocorder This error is from iTextSharp, not Razorpdf2. Please open another question.

Show 11 more comments

0

I tried using Pdfsharp and Itextsharp.

For simple things you need to write a lot of code and it should be encapsulated making your life easier.

I suggest using the Ironpdf library that has good documentation and much easier to use.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.