ASP.NET 关于 Linux itextsharp 问题的核心

ASP.NET Core on Linux problem with itextsharp

我们有一个小型 ASP.NET Core 3.1 网络应用程序。我们使用 itextsharp 库 (5.5.13) 生成简单的 PDF 并将其导出。

在调试(和 Windows 服务器上)一切正常,但在 Linux 服务器(Debian 10)上发布时,生成 PDF 时出现错误:

Error: The document has no pages.

Stack trace: 
at iTextSharp.text.pdf.PdfPages.WritePageTree() 
at iTextSharp.text.pdf.PdfWriter.Close() 
at iTextSharp.text.pdf.PdfDocument.Close() 
at iTextSharp.text.Document.Close() 
at eDov.Web.Helpers.Reports.Dovolilnice.GetPdfTest() in C:\*\Reports\Dovolilnica.cs:line 173 
at eDov.Web.Controllers.DovolilniceController.TestPdf(Int32 id) in C:\*\Controllers\DovolilniceController.cs:line 184 
at lambda_method(Closure , Object , Object[] ) 
at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() 
--- End of stack trace from previous location where exception was thrown --- 
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() 
--- End of stack trace from previous location where exception was thrown --- 
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) 
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync() 
--- End of stack trace from previous location where exception was thrown --- 
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope) 
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger) 
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) 
at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)

调用生成 PDF 的控制器代码:

public IActionResult TestPdf(int id)
{
    var file = new Dokument
    {
        Naziv = string.Format("Dovolilnica - {0}.pdf", id.ToString()),
        Extension = ".pdf",
        ContentType = "application/pdf",
    };

    //this is the line that the error from stack trace is referring
    file.Contents = Helpers.Reports.Dovolililnice.GetPdfTest();  

    return File(file.Contents, file.ContentType, file.Naziv);
}

以及生成 PDF 的代码:

public static byte[] GetPdfTest()
{
    byte[] result = null;

    // custom velikost nalepke
    // 1 inch = 72px
    var pgSize = new iTextSharp.text.Rectangle(176f, 135f);

    MemoryStream pdfData = new MemoryStream();
    iTextSharp.text.Document document = new iTextSharp.text.Document(pgSize, 7.5f, 7.5f, 7.5f, 7.5f);
    PdfWriter pdfWriter = PdfWriter.GetInstance(document, pdfData);
    pdfWriter.ViewerPreferences = PdfWriter.PageModeUseOutlines;

    document.Open();

    document.Add(new Paragraph("Create test Pdf Document"));

    pdfWriter.CloseStream = false;

    document.Close();
    //this is the line that the error from stack trace is referring
    pdfData.Position = 0;

    result = pdfData.ToArray();
    pdfData.Dispose();

    return result;
}

有没有人在 Linux 上发布时成功使用 itextsharp(低于版本 7)?

我们无法在 Linux 上运行此功能,因此我们尝试使用 itextsharp 库的 iTextSharp.LGPLv2.Core 端口(参见:https://github.com/VahidN/iTextSharp.LGPLv2.Core)并且它有效。

它也可以在 Nuget PM 中使用。