Coder Social home page Coder Social logo

Comments (13)

HakanL avatar HakanL commented on July 19, 2024 1

If I check the fixes in 0.12.6 I can see that TOC is listed, perhaps 0.12.5 isn't properly working with TOC? It's always best to test your HTML with the native wkhtmltopdf command line tool to see if the issue is with wkhtmltopdf, or with this wrapper. That being said, I've tested to upgrade to 0.12.6, but I haven't had a chance to test it, feel free to try it out if you can, it's in this branch: test-0.12.6. You'll find the NuGet package here: https://www.nuget.org/packages/Haukcode.WkHtmlToPdfDotNet/1.3.0-test-0.12.6
I'm not sure why you load the managed library directly in your code, have you looked at the WebServer sample in this repo?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024 1

Hmm, thanks for the details. Unfortunately I don't know much about wkhtmltopdf itself, I just forked an existing wrapper and updated it and made it so it includes all binaries. I'm not sure I'm able to figure out a solution for this, we may need to depend on the community to see if someone can figure out what's going on. Maybe you can add a test method to the samples in this repo in a PR so it's easy for someone to test the scenario?

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024 1

Ah, good catch. I've changed the unit test to check the first generated file as well. Obviously that doesn't solve the actual problem, but at least we have a way to confirm.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

Does it work with other types of pages?

from wkhtmltopdf-dotnet.

hungdn2703 avatar hungdn2703 commented on July 19, 2024

Each of pages is worked as well, but table of content is exceptioned.
I ran my code on console app, it's good. But with ASP.NET (RESTful API), the issue is happened.
My config code for pdf configuration:

public static void ConfigPdfLibrary(this IServiceCollection services)
{
    string fileName = GetPdfLibraryFileName();
    CustomAssemblyLoadContext context = new CustomAssemblyLoadContext();
    context.LoadUnmanagedLibrary(fileName);

    ITools pdfTools = new PdfTools();
    IConverter synchronizedConverter = new SynchronizedConverter(pdfTools);
    services.AddSingleton<IConverter>(synchronizedConverter);
}

from wkhtmltopdf-dotnet.

hungdn2703 avatar hungdn2703 commented on July 19, 2024

Thank you for your updated.
I've updated my code and upgraded to new version. But the issue is still not resolved.

public void ConfigureServices(IServiceCollection services)
{
    services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}

ConvertController.cs

[HttpGet]
public IActionResult Get()
{
    var tableOfContents = new TableOfContentsSettings() { };

    var doc = new HtmlToPdfDocument()
    {
        GlobalSettings = {
            PaperSize = PaperKind.A4,
            Orientation = Orientation.Landscape,
        },

        Objects = {
            tableOfContents,
                new ObjectSettings()
            {
                Page = "template.html",

            }
        }
    };

    byte[] pdf = _converter.Convert(doc);

    return new FileContentResult(pdf, "application/pdf");
}
[Serializable]
public class TableOfContentsSettings : ObjectSettings
{
    public TableOfContentsSettings()
    {
        ProduceTableOfContents = true;
        UseDottedLines = true;
    }

    [WkHtml("isTableOfContent")]
    internal bool ProduceTableOfContents { get; set; }

    [WkHtml("toc.useDottedLines")]
    public bool UseDottedLines { get; set; }
}
  1. First time:
    first_time
  2. Next time
    next_time
    first_time.pdf
    next_time.pdf

from wkhtmltopdf-dotnet.

hungdn2703 avatar hungdn2703 commented on July 19, 2024

Hi HakanL,
Thank you for your updated. I've created unit test project and test method to convert pdf file.
It's included table of contents settings. Pull request at: https://github.com/HakanL/WkHtmlToPdf-DotNet/pull/23
Test method:

[TestMethod]
public void ConvertToPdfTest()
{
    for (int i = 1; i < 3; i++)
    {
        var pdf = GetPdf();
        File.WriteAllBytes($"Test {i}.pdf", pdf);

        Assert.IsNotNull(pdf);
    }
}
private byte[] GetPdf(string templatePage = "")
{
    if (string.IsNullOrEmpty(templatePage))
    {
        templatePage = "template.html";
    }
    var globalSettings = new GlobalSettings()
    {
        PaperSize = PaperKind.A4,
        Orientation = Orientation.Portrait
    };
    var templateSettings = new ObjectSettings()
    {
        Page = templatePage,
    };

    var tableOfContentSettings = new TableOfContentsSettings()
    {
        IsTableOfContent = true
    };

    var doc = new HtmlToPdfDocument()
    {
        GlobalSettings = globalSettings,

        Objects = { tableOfContentSettings, templateSettings }
    };

    byte[] pdf = _converter.Convert(doc);

    return pdf;
}

You can run this test method and compare pdf file results.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

I merged in the PR and also did some refactoring in the code. The issue is that the unit test runs fine here. Can you test with the latest code in master to confirm if it works for you as well?

from wkhtmltopdf-dotnet.

hungdn2703 avatar hungdn2703 commented on July 19, 2024

Thank HakanL, I can run your refactor code as well. But the issue with table of contents is not resolved.
I'm investigating it and try to find out the solution.

[TestMethod]
public void RepeatTableOfContents()
{
    for (int i = 0; i < 2; i++)
    {
        byte[] pdf = GetPdfWithTableOfContents();

        Assert.IsNotNull(pdf);
        Assert.IsTrue(pdf.Length > 10000);

        WriteToPdfFile(pdf);
    }
}

private void WriteToPdfFile(byte[] pdf, string fileName = "")
{
    if (!Directory.Exists("Files"))
    {
        Directory.CreateDirectory("Files");
    }
    if (string.IsNullOrEmpty(fileName))
    {
        fileName = $"{DateTime.UtcNow:yyyyMMddHHmmss}.pdf";
    }
    using (var stream = new FileStream(Path.Combine("Files", fileName), FileMode.Create))
    {
        stream.Write(pdf, 0, pdf.Length);
    }
}

The table of contents page in file 2 still is blank
File1: 20201008023350.pdf
File2: 20201008023351.pdf

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

from wkhtmltopdf-dotnet.

hungdn2703 avatar hungdn2703 commented on July 19, 2024

I'm using VS on Windows too. The unit test is not failed but the pdf file was not corrected.

from wkhtmltopdf-dotnet.

HakanL avatar HakanL commented on July 19, 2024

I attempted a refactoring of the converter to see if it would make a difference, but it doesn't. So if I run all unit tests then it's successful, but if I run just the repeat unit test then it fails. I tried to add a sleep just to see if something weird was going on, but it didn't help. I'm not sure what's going on here, very odd.

from wkhtmltopdf-dotnet.

hungdn2703 avatar hungdn2703 commented on July 19, 2024

I've checked the code and find out the root-cause. You can see the result in below capture
Green is result for all unit test in ConvertTest class
Red is result when i run RepeatTableOfContents test method only
image
When you run all of unit tests, the ConvertToPdf test method will be run in the first time. After that, it can't generate TOC in the next time (file size 13kb).
If we just run RepeatTableOfContents method only, it can generate TOC in the first time. The pdf file size is 23KB, after that it can't generate TOC again => the file size will be 13KB

from wkhtmltopdf-dotnet.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.