Linux 上的 ASP.NET Core 问题与 itextsharp

问题描述

我们有一个小型的 ASP.NET Core 3.1 Web 应用程序。我们使用 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,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,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);
    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 中使用。