0

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?

5
  • 1
    there must be something wrong with your code that you cannot (or don't want to) reproduce in your development environment. The error related to deployed environment (Linux) should be different, involving some lib missing ... You should post your related code to this issue and focus on it instead. (I've not downvoted you). Commented Dec 23, 2020 at 9:04
  • when it didn't work on linux and we couldn't find the error we tried a completly basic scenario and the error remains. I have updated my question with the code details Commented Dec 23, 2020 at 9:21
  • 1
    Stacktrace indicates that Methode GetPdf ist called. And Not GetPfdTest Commented Dec 23, 2020 at 9:27
  • 2
    @TheMixy are you sure that you posted the involved method? I see that the method called in the stacktrace is this one GetPdf(Dovolilnica dovolilnica, String imagePath, String fontPath) with a completely different signature (besides the method name). Commented Dec 23, 2020 at 9:29
  • I apologize. I made so many different tests that I messed up the code and stack trace. I have now posted the correct code/stack combination Commented Dec 23, 2020 at 13:19

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.