TheMixy
TheMixy

Reputation: 1316

ASP.NET Core on Linux problem with itextsharp

We have a small ASP.NET Core 3.1 web app. We use itextsharp library (5.5.13) for generating a simple PDF and exporting it.

All works well in debug (and on Windows server), but when published on a Linux server (Debian 10) there is an error generating the 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)

The code from the controller that is called to generate the 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);
}

and the code that generates the 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;
}

Has anyone used itextsharp (lower than version 7) successfully when publishing on Linux?

Upvotes: 0

Views: 2630

Answers (1)

TheMixy
TheMixy

Reputation: 1316

We couldn't get this working on Linux so we tried using iTextSharp.LGPLv2.Core port of the itextsharp library (see: https://github.com/VahidN/iTextSharp.LGPLv2.Core) and it works.

It's also available in Nuget PM.

Upvotes: 2

Related Questions