免责声明:我为 SelectPdf 工作。
SelectPdf 确实支持 ASP.NET Core + Razor 页面。示例可从 SelectPdf 网站下载。
这里的示例代码:
@page
@model SelectPdf.Samples.Pages.ConvertUrlToPdfModel
@{
Layout = "~/Pages/_Layout.cshtml";
ViewData["Title"] = "SelectPdf Free Html To Pdf Converter for .NET Core - Convert from Url to Pdf - C# / ASP.NET Core MVC6";
ViewData["Description"] = "SelectPdf Convert from Url to Pdf Sample for C# ASP.NET MVC. Pdf Library for .NET with full sample code in C# and VB.NET.";
ViewData["Keywords"] = "convert from url to pdf, pdf library, sample code, html to pdf, pdf converter";
}
<form method="post">
<article class="post type-post status-publish format-standard hentry">
<header class="entry-header">
<h1 class="entry-title">SelectPdf Free Html To Pdf Converter for .NET Core - Convert from Html to Pdf - C# / ASP.NET Core MVC6 Sample</h1>
</header>
<!-- .entry-header -->
<div class="entry-content">
<p>
This sample shows how to use SelectPdf html to pdf converter to convert an url to pdf, also setting a few properties.
</p>
<p>
Url:<br />
<input type="text" style="width: 90%;" value="https://selectpdf.com" asp-for="TxtUrl" />
</p>
<div class="col2">
Pdf Page Size:<br />
<select asp-for="DdlPageSize" asp-items="Model.PageSizes"></select>
<br />
<br />
Pdf Page Orientation:<br />
<select asp-for="DdlPageOrientation" asp-items="Model.PageOrientations"></select><br />
<br />
</div>
<div class="col2">
Web Page Width:<br />
<input type="text" style="width: 50px;" value="1024" asp-for="TxtWidth" /> px<br />
<br />
Web Page Height:<br />
<input type="text" style="width: 50px;" value="" asp-for="TxtHeight" /> px<br />
(leave empty to auto detect)<br />
<br />
</div>
<div class="col-clear"></div>
<p>
<input type="submit" name="BtnConvert" value="Create PDF" class="mybutton" />
</p>
</div>
<!-- .entry-content -->
</article>
</form>
// C# code below
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace SelectPdf.Samples.Pages
{
public class ConvertUrlToPdfModel : PageModel
{
public void OnGet()
{
DdlPageSize = "A4";
}
[BindProperty]
public string TxtUrl { get; set; }
[BindProperty]
public string DdlPageSize { get; set; }
public List<SelectListItem> PageSizes { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "A1", Text = "A1" },
new SelectListItem { Value = "A2", Text = "A2" },
new SelectListItem { Value = "A3", Text = "A3" },
new SelectListItem { Value = "A4", Text = "A4" },
new SelectListItem { Value = "A5", Text = "A5" },
new SelectListItem { Value = "Letter", Text = "Letter" },
new SelectListItem { Value = "HalfLetter", Text = "HalfLetter" },
new SelectListItem { Value = "Ledger", Text = "Ledger" },
new SelectListItem { Value = "Legal", Text = "Legal" },
};
[BindProperty]
public string DdlPageOrientation { get; set; }
public List<SelectListItem> PageOrientations { get; } = new List<SelectListItem>
{
new SelectListItem { Value = "Portrait", Text = "Portrait" },
new SelectListItem { Value = "Landscape", Text = "Landscape" },
};
[BindProperty]
public string TxtWidth { get; set; }
[BindProperty]
public string TxtHeight { get; set; }
public IActionResult OnPost()
{
// read parameters from the webpage
PdfPageSize pageSize =
(PdfPageSize)Enum.Parse(typeof(PdfPageSize), DdlPageSize, true);
PdfPageOrientation pdfOrientation =
(PdfPageOrientation)Enum.Parse(typeof(PdfPageOrientation),
DdlPageOrientation, true);
int webPageWidth = 1024;
try
{
webPageWidth = System.Convert.ToInt32(TxtWidth);
}
catch { }
int webPageHeight = 0;
try
{
webPageHeight = System.Convert.ToInt32(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(TxtUrl);
// 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;
}
}
}