【问题标题】:How do i add a barcode to html which will be converted to PDF using DinkToPdf如何将条形码添加到 html 中,然后使用 DinkToPdf 将其转换为 PDF
【发布时间】:2022-01-18 12:22:39
【问题描述】:

我正在处理一个 ASP.NET Core MVC 项目,并且我正在执行一项将视图转换为 pdf 的操作。

我想在 html 中生成一个 QRBarCode,我发现这个库 https://ironsoftware.com/csharp/barcode/tutorials/csharp-barcode-image-generator/ 可以生成一个 QRBar 代码,但它只适用于 .NET 而不是 .NET Core。

这就是我将视图转换为 PDF 的方式:

public async Task<IActionResult> AccreditationLetter(int accreditationId)
{
        //get model
        var htmlContent = View("AccreditationLetter", model).ToHtml(HttpContext);

        var file = await GenerateAccreditationLetter(htmlContent, $"Accreditation Letter");

        return File(file, "application/pdf", "Accreditation Letter.pdf");
}

private async Task<byte[]> GenerateAccreditationLetter(string htmlContent, string title)
{
    var settings = new GlobalSettings
        {
            ColorMode = ColorMode.Color,
            Orientation = Orientation.Portrait,
            PaperSize = PaperKind.A4,
            Margins = new MarginSettings { Top = 3 },
            DocumentTitle = title
        };

    var objectSettings = new ObjectSettings
        {
            PagesCount = true,
            HtmlContent = htmlContent,
            WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = null }
        };

    var pdf = new HtmlToPdfDocument()
        {
            GlobalSettings = settings,
            Objects = { objectSettings }
        };

    return converter.Convert(pdf);
}

我需要生成一个二维码并将其添加到AccreditationLetter方法中的html中。任何帮助将不胜感激

【问题讨论】:

    标签: c# asp.net-core-mvc pdf-generation


    【解决方案1】:

    IronBarcode 支持 .NET Core 2x 和 3x、.NET Standard 和 .NET Framework 4x,完全支持 Azure,如网站上的 Features 包所示。

    IronBarcode NuGet package 表示支持:

    • .Net Framework 4.0 +
    • .Net Core 2.0 +
    • .Net 5
    • .Net 6

    如果您只想要带有条形码的 PDF,则根本不需要使用 dinkpdf - IronBarcode 只需使用以下命令即可导出为 PDF:

    // Save to a new PDF
    MyBarCode.SaveAsPdf("MyBarCode.Pdf");
    
    /// Add the barcode to any page or pages of an existing PDF
    MyBarCode.StampToExistingPdfPage("ExistingPDF.pdf", 200, 50, 1);  
    
    // position x=200 y=50 on page 1
    MyBarCode.StampToExistingPdfPages("ExistingPDF.pdf", 200, 50, new[] { 1, 2, 3 }, "Password123");  // multiple pages of an encrypted PDF
    

    对于export barcodes as HTML,这可以是独立的 HTML 图像标记(可以在 HTML 文件、ASPX 或 MVC 视图中提供。不需要图像资源,该标记将整个图像嵌入其 Src 内容中)

    string ImgTag = MyBarCode.ToHtmlTag();
    

    或者将图像转换为 Html/CSS 数据 URI。

    string DataURI = MyBarCode.ToDataUrl();
    

    【讨论】:

      猜你喜欢
      • 2013-05-16
      • 2011-03-03
      • 2016-08-25
      • 1970-01-01
      • 2015-01-10
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      相关资源
      最近更新 更多