Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generating PDF produces blank document when using async/await #882

Open
marcmognol opened this issue May 6, 2024 · 2 comments
Open

Generating PDF produces blank document when using async/await #882

marcmognol opened this issue May 6, 2024 · 2 comments

Comments

@marcmognol
Copy link
Contributor

Describe the bug
I'm trying to generate a PDF, the produced document is empty/blank.

To Reproduce

using QuestPDF.Fluent;
using QuestPDF.Infrastructure;

QuestPDF.Settings.License = LicenseType.Community;

for (int i = 0; i < 10; i++)
{
    var document = Document.Create(container =>
    {
        container.Page(async page =>
        {
            await Task.Delay(10);
            page.Content().Text(DateTime.Now.ToString());
        });
    });

    document.GeneratePdf("c:\\temp\\pdf_" + i + "doc.pdf");
}

Expected behavior
10 generated PDF with the current datetime as content.
But only one is correctly generated as shown below in the screenshot. Sometimes zero document is correctly generated.

Screenshots

image

Environment
What version of the library do you use? 2024.3.4, same behavior with old versions.
What operating system do you use? (OS type, x64 vs x86 vs arm64) x64

Additional context
If I remove async/await from the PageDescriptor, I've no issue but I need to be able to call some external API/Database to get contextual data that I can't operate outside.

Thanks for your support.

@MarcinZiabek
Copy link
Member

MarcinZiabek commented May 7, 2024

You are likely looking for this type of code:

class MyData
{
    public required int Index { get; init; }
    public required DateTime DateTime { get; init; }
    public required byte[] Image { get; init; }
}

public async Task Example()
{
    void GenerateDocumentImagePath(MyData data)
    {
        var document = Document.Create(container =>
        {
            container.Page(page =>
            {
                page.Content().Column(column =>
                {
                    column.Item().Text(data.DateTime.ToString());
                    column.Item().Image(data.Image);
                });
            });
        });

        document.GeneratePdf("c:\\temp\\pdf_" + data.Index + "doc.pdf");
    }
    
    for (int i = 0; i < 10; i++)
    {
        var data = new MyData
        {
            Index = i,
            DateTime = DateTime.Now,
            Image = await MyImageService.GetImageAsync(i)
        };
        
        GenerateDocumentImagePath(i);
    }
}

The PDF generation process is synchronous. If you need to call any external async resources, please do it before the generation and provide all necessary data through a DTO model.

@marcmognol
Copy link
Contributor Author

Hi @MarcinZiabek

Thank you for your answer. I'll do it that way.

Marc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants