ASP.NET MVC Générer un PDF à partir d'une vue (Tutoriel d'exemple de code)
Il est possible de servir un fichier ou une chaîne HTML existant, un document PDF existant, ainsi qu'un PDF en ASP.NET MVC. Le tutoriel ci-dessous explique comment convertir facilement une vue MVC en PDF dans votre projet C#.
Tutoriel ASP.NET MVC Générer un PDF à partir d'une vue
- Télécharger ASP.NET MVC Generate PDF from View Library (Générer un PDF à partir d'une vue)
- Installer dans votre Visual Studio
- Servez le PDF dans ASP.NET MVC
- Servir un fichier PDF existant
- Servez un fichier HTML existant ou une chaîne de caractères
Étape 1
1. Installer IronPDF
Pour servir des fichiers PDF existants, des fichiers HTML ou des chaînes de caractères, ainsi que pour servir un PDF en ASP.NET MVC, nous pouvons utiliser la bibliothèque PDF C# d'IronPDF. Téléchargez-le gratuitement pour le développement et commencez avec le tutoriel ci-dessous. Accédez-y viaIronPDF DLL fichier ZIP ou par l'intermédiaire duPaquet NuGet IronPDF.
Install-Package IronPdf
Comment faire Tutoriel
2. Servez le PDF dans ASP.NET MVC
Pour servir un document PDF dans ASP.NET MVC, il faut générer un fichierRésultat du fichier méthode. Avec IronPDF, vous pouvez utiliserCadre ASP.NET MVC pour renvoyer un fichier PDF.
Cette méthode peut ensuite être utilisée par votre contrôleur comme indiqué ci-dessous.
/**
Serve PDF in ASPNET MVC
anchor-serve-pdf-in-asp-net-mvc
**/
public FileResult GetHTMLPageAsPDF(long id) {
//Create a PDF Document
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>");
//return a pdf document from a view
var contentLength = PDF.BinaryData.Length;
Response.AppendHeader("Content-Length", contentLength.ToString());
Response.AppendHeader("Content-Disposition", "inline; filename=Document_" + id + ".pdf");
return File(PDF.BinaryData, "application/pdf;");
}
/**
Serve PDF in ASPNET MVC
anchor-serve-pdf-in-asp-net-mvc
**/
public FileResult GetHTMLPageAsPDF(long id) {
//Create a PDF Document
using var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>");
//return a pdf document from a view
var contentLength = PDF.BinaryData.Length;
Response.AppendHeader("Content-Length", contentLength.ToString());
Response.AppendHeader("Content-Disposition", "inline; filename=Document_" + id + ".pdf");
return File(PDF.BinaryData, "application/pdf;");
}
'''
'''Serve PDF in ASPNET MVC
'''anchor-serve-pdf-in-asp-net-mvc
'''*
Public Function GetHTMLPageAsPDF(ByVal id As Long) As FileResult
'Create a PDF Document
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf and MVC</h1>")
'return a pdf document from a view
Dim contentLength = PDF.BinaryData.Length
Response.AppendHeader("Content-Length", contentLength.ToString())
Response.AppendHeader("Content-Disposition", "inline; filename=Document_" & id & ".pdf")
Return File(PDF.BinaryData, "application/pdf;")
End Function
Pour un exemple plus avancé, vous pourriez utiliser votre vue HTML pour générer une chaîne HTML, puis la convertir en PDF comme indiqué ci-dessus.
3. Servir un fichier PDF existant
Pour servir directement unFichier PDF dans d'autres contextes ASP.NET est également possible.
/**
Serve Existing PDF
anchor-serve-existing-pdf-file
**/
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");
// edit this line to display in browser and change the file name
Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));
// gets our pdf as a byte array and then sends it to the buffer
Response.Flush();
Response.End();
/**
Serve Existing PDF
anchor-serve-existing-pdf-file
**/
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");
// edit this line to display in browser and change the file name
Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"));
// gets our pdf as a byte array and then sends it to the buffer
Response.Flush();
Response.End();
'''
'''Serve Existing PDF
'''anchor-serve-existing-pdf-file
'''*
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition","attachment;filename=""FileName.pdf""")
' edit this line to display in browser and change the file name
Response.BinaryWrite(System.IO.File.ReadAllBytes("MyPdf.pdf"))
' gets our pdf as a byte array and then sends it to the buffer
Response.Flush()
Response.End()
4. Servez un fichier HTML existant ou une chaîne de caractères
/**
Serve Existing HTML File or String
anchor-serve-existing-html-file-or-string
**/
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHTMLFileAsPdf("Project/MyHtmlDocument.html");
// or to convert an HTML string
//var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");
// edit this line to display in browser and change the file name
Response.BinaryWrite( PDF.BinaryData );
Response.Flush();
Response.End();
/**
Serve Existing HTML File or String
anchor-serve-existing-html-file-or-string
**/
var Renderer = new IronPdf.ChromePdfRenderer();
using var PDF = Renderer.RenderHTMLFileAsPdf("Project/MyHtmlDocument.html");
// or to convert an HTML string
//var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment;filename=\"FileName.pdf\"");
// edit this line to display in browser and change the file name
Response.BinaryWrite( PDF.BinaryData );
Response.Flush();
Response.End();
'''
'''Serve Existing HTML File or String
'''anchor-serve-existing-html-file-or-string
'''*
Dim Renderer = New IronPdf.ChromePdfRenderer()
Dim PDF = Renderer.RenderHTMLFileAsPdf("Project/MyHtmlDocument.html")
' or to convert an HTML string
'var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello IronPdf</h1>");
Response.Clear()
Response.ContentType = "application/pdf"
Response.AddHeader("Content-Disposition","attachment;filename=""FileName.pdf""")
' edit this line to display in browser and change the file name
Response.BinaryWrite(PDF.BinaryData)
Response.Flush()
Response.End()