Skip to content

Due To Some Reason Blank Line Is Added #193

@Shishkovskiy

Description

@Shishkovskiy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions