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

Some configurations of MarginOption lead to -> startIndex ('-1') must be a non-negative value. (Parameter 'startIndex') #2691

Closed
JoaoVictorVP opened this issue Jul 16, 2024 · 2 comments · Fixed by #2741

Comments

@JoaoVictorVP
Copy link

Description

All .PdfAsync variants throw an:

Message: startIndex ('-1') must be a non-negative value. (Parameter 'startIndex')
      Actual value was -1., Extra: ConvertedException { Message = startIndex ('-1') must be a non-negative value. (Parameter 'startIndex')
      Actual value was -1., Source = System.Private.CoreLib, StackTrace =    at System.ArgumentOutOfRangeException.ThrowNegative[T](T value, String paramName)
         at System.String.ThrowSubstringArgumentOutOfRange(Int32 startIndex, Int32 length)
         at System.String.Substring(Int32 startIndex)
         at PuppeteerSharp.Cdp.CdpPage.ConvertPrintParameterToInches(Object parameter) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Cdp/CdpPage.cs:line 1231
         at PuppeteerSharp.Cdp.CdpPage.PdfInternalAsync(String file, PdfOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Cdp/CdpPage.cs:line 812
         at PuppeteerSharp.Page.PdfStreamAsync(PdfOptions options) in /home/runner/work/puppeteer-sharp/puppeteer-sharp/lib/PuppeteerSharp/Page.cs:line 390

When you use 0 in the measures.

Complete minimal example reproducing the issue

using PuppeteerSharp;
using PuppeteerSharp.Media;

await new BrowserFetcher(SupportedBrowser.Chrome).DownloadAsync();
var browser = await Puppeteer.LaunchAsync(new LaunchOptions {Headless = false});
var page = await browser.NewPageAsync();
await page.SetContentAsync("""
    <div>this is a very simple test</div>
""");
var pdf = await page.PdfStreamAsync(new PdfOptions()
{
    Format = PaperFormat.Legal,
    Landscape = true,
    MarginOptions = new MarginOptions()
    {
        Top = "0",
        Bottom = "0",
        Left = "0",
        Right = "0"
    }
});
var file = File.OpenWrite("test.pdf");
await pdf.CopyToAsync(file);
file.Close();

await browser.CloseAsync();

Expected behavior:

The PDF is generated.

Actual behavior:

It crashes.

Versions

  • Which version of PuppeteerSharp are you using? I tried radomly some of them, since 9.0.0 to the most recent.
  • Which .NET runtime and version are you targeting? .NET 8+

Additional Information

This only happens if you don't specify a unit in the margin options, like with the "0" thing here. By using something like "0cm" it works, but the behavior is non-intuitive and error-prone as it is not described anywhere (also, you would expect it to work as it expects a string and in CSS this is perfectly valid).

@kblok
Copy link
Member

kblok commented Jul 23, 2024

@JoaoVictorVP sorry I skipped this issue. I'll take a look at it.

@kblok
Copy link
Member

kblok commented Aug 16, 2024

Fix is on the way. As a workaround, you can pass "0px" there.

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

Successfully merging a pull request may close this issue.

2 participants