puppeteer-sharp icon indicating copy to clipboard operation
puppeteer-sharp copied to clipboard

Spacing or adjacency issue among Characters

Open mertgundoganx opened this issue 1 year ago • 3 comments

I convert the value I write in HTML to a base64 PDF in the following method. While there is no issue with spacing among characters locally, there seems to be a problem with some characters being either adjacent or having gaps in the server environment.

Example

Screenshot 2024-01-24 at 09 43 39

Style

        html.AppendHtml("<!DOCTYPE html>");
        html.AppendHtml("<html lang='en'>");
        html.AppendHtml("<head>");
        html.AppendHtml("<meta charset=\"UTF-8\">");
        html.AppendHtml("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">");
        html.AppendHtml("<link rel=\"preconnect\" href=\"http://fonts.googleapis.com\">");
        html.AppendHtml("<link rel=\"preconnect\" href=\"http://fonts.gstatic.com\" crossorigin>");
        html.AppendHtml("<link href=\"http://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700;900&subset=latin-ext&display=swap\" rel=\"stylesheet\">");
        html.AppendHtml("</head>");
        html.AppendHtml("<body style='font-family: Roboto, sans-serif !important; -webkit-font-smoothing: antialiased !important; font-variant: tabular-nums !important; letter-spacing: normal !important; font-feature-settings: \"tnum\" !important;'>");


Converter Method


try
        {
            using var browserFetcher = new BrowserFetcher();
            await browserFetcher.DownloadAsync();

            await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
            {
                Headless = false,
                Args = new[] { "--no-sandbox" },
                Timeout = 10000,
                IgnoreHTTPSErrors = true
            });

            await using var page = await browser.NewPageAsync();
            var html = ToHtmlString(htmlText);
            await page.SetContentAsync(html);

            var advertisementVehiclePdf = await page.PdfDataAsync(new PdfOptions
            {
                Scale = 1.7m,
                PrintBackground = true,
                Format = PaperFormat.A4,
                MarginOptions = new MarginOptions
                {
                    Top = "20px",
                    Bottom = "20px",
                    Left = "30px",
                    Right = "30px"
                }
            });

            return Convert.ToBase64String(advertisementVehiclePdf);
        }
        catch (Exception e)
        {
            Console.WriteLine("Error generating pdf: " + e.Message);
            throw;
        }

private static string ToHtmlString(IHtmlContent builder)
    {
        using var writer = new StringWriter();
        builder.WriteTo(writer, HtmlEncoder.Default);
        return writer.ToString();
    }


Versions

  • PuppeteerSharp 13.0.2
  • net6.0

mertgundoganx avatar Jan 24 '24 06:01 mertgundoganx

Do you have remove access to the server to run chrome there?

kblok avatar Jan 24 '24 13:01 kblok

Do you have remove access to the server to run chrome there?

yes, i have access permission

mertgundoganx avatar Jan 24 '24 15:01 mertgundoganx

@mertgundoganx I bet there is some kind of font issue in the server or in the browser. Can you reproduce that printing to PDF in the server's browser?

kblok avatar Jan 24 '24 15:01 kblok