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

Due To Some Reason Blank Line Is Added #193

Open
Shishkovskiy opened this issue Nov 6, 2024 · 0 comments
Open

Due To Some Reason Blank Line Is Added #193

Shishkovskiy opened this issue Nov 6, 2024 · 0 comments

Comments

@Shishkovskiy
Copy link

Shishkovskiy commented Nov 6, 2024

The environment where the application is running:

  • Operating System: Windows
  • .NET Version: .NET 6

Package version:

  • DinkToPdf 1.0.8

Occasionally we faced a blank line while building PDF files with this usage:

private void ConfigureUnmanagedAssemblies()
{
        const string HtmlToPdfLibraryName = "libwkhtmltox.dll";

#if DEBUG
        const string HtmlToPdfLibraryDirectoryName = "HtmlToPdfConverter_x64";
#else
        const string HtmlToPdfLibraryDirectoryName = "HtmlToPdfConverter_x86";
#endif

        var customAssemblyLoadContext = new CustomAssemblyLoadContext();
        var customAssemblyDirectoryInfo = new DirectoryInfo(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + $"\\{HtmlToPdfLibraryDirectoryName}");
        var customAssemblyFile = customAssemblyDirectoryInfo.GetFiles(HtmlToPdfLibraryName, SearchOption.AllDirectories).FirstOrDefault();
        if (!string.IsNullOrWhiteSpace(customAssemblyFile?.DirectoryName))
        {
            customAssemblyLoadContext.LoadUnmanagedLibrary(Path.Combine(customAssemblyFile.DirectoryName, customAssemblyFile.Name));
        }
}
public async Task<FileModel> GenerateAsync(Invoice invoice, string fileName)
{
    FileModel fileModel = null;

    var invoiceExportPdfModel = await this.GetInvoiceExportPdfModelAsync(invoice.Id, invoice.IssuedDate).ConfigureAwait(false);

    var htmlToPdfDocument = await this.GetHtmlToPdfDocument(invoiceExportPdfModel).ConfigureAwait(false);
    if (htmlToPdfDocument != null)
    {
        fileModel = new FileModel { Content = this.htmlToPdfConverter.Convert(htmlToPdfDocument), Name = fileName, ContentType = ContentTypeConstants.ContentTypePdf };
    }

    return fileModel;
}
private async Task<HtmlToPdfDocument> GetHtmlToPdfDocument(InvoiceExportPdfModel invoiceExportPdfModel)
{
    HtmlToPdfDocument htmlToPdfDocument = null;

    if (invoiceExportPdfModel != null)
    {
        htmlToPdfDocument = new HtmlToPdfDocument
        {
            GlobalSettings = new GlobalSettings
            {
                ColorMode = ColorMode.Color,
                Orientation = Orientation.Portrait,
                PaperSize = PaperKind.A4,
                Margins = new MarginSettings(0, 0, 0, 0),
                DocumentTitle = invoiceExportPdfModel.InvoiceNumber
            }
        };

        var htmlPages = await this.invoiceHtmlPageBuilder.BuildAsync(invoiceExportPdfModel).ConfigureAwait(false);

        htmlToPdfDocument.Objects.AddRange(
            htmlPages.Select(
                htmlPage => new ObjectSettings
                {
                    PagesCount = true,
                    HtmlContent = htmlPage,
                    WebSettings = { DefaultEncoding = "utf-8", UserStyleSheet = this.filePathProvider.GetFilePath(StyleSheetConstants.FolderName, StyleSheetConstants.FileName, FileType.Css) }
                }));
    }

    return htmlToPdfDocument;
}

NOTE:

When I start generating it locally on my local machine which uses Windows as well it generates files without the blank page so, it looks like there's something related to the environment where the app is running but at the same time, it looks like there's no difference. Does anyone have any idea of the reason or probably someone has already faced a similar issue and figured out how it can be solved? Thanks in advance for any help @gldraphael @rdvojmoc

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

1 participant