1717var destinationDirectory = args . Length > 0 ? args [ 0 ] : Path . Combine ( Directory . GetCurrentDirectory ( ) , "output" ) ;
1818Directory . CreateDirectory ( destinationDirectory ) ;
1919
20+ var sharpClient = CreateClient ( options ) ;
21+
2022// Screenshot from HTML
21- var htmlPath = await ScreenshotFromHtml ( destinationDirectory , options ) ;
23+ var htmlPath = await ScreenshotFromHtml ( destinationDirectory , sharpClient ) ;
2224Console . WriteLine ( $ "HTML screenshot: { htmlPath } ") ;
2325
2426// Screenshot from URL
25- var urlPath = await ScreenshotFromUrl ( destinationDirectory , options ) ;
27+ var urlPath = await ScreenshotFromUrl ( destinationDirectory , sharpClient ) ;
2628Console . WriteLine ( $ "URL screenshot: { urlPath } ") ;
2729
28- static async Task < string > ScreenshotFromHtml ( string destinationDirectory , GotenbergSharpClientOptions options )
30+ static async Task < string > ScreenshotFromHtml ( string destinationDirectory , GotenbergSharpClient sharpClient )
2931{
30- var sharpClient = CreateClient ( options ) ;
31-
3232 var builder = new ScreenshotHtmlRequestBuilder ( )
3333 . AddDocument ( doc => doc . SetBody ( @"
3434 <html>
@@ -41,18 +41,16 @@ static async Task<string> ScreenshotFromHtml(string destinationDirectory, Gotenb
4141 . SetSize ( 1280 , 720 )
4242 . SetFormat ( ScreenshotFormat . Png ) ) ;
4343
44- var response = await sharpClient . ScreenshotHtmlAsync ( builder ) ;
44+ await using var response = await sharpClient . ScreenshotHtmlAsync ( builder ) ;
4545
4646 var resultPath = Path . Combine ( destinationDirectory , $ "ScreenshotHtml-{ DateTime . Now : yyyyMMddHHmmss} .png") ;
4747 await using var file = File . Create ( resultPath ) ;
4848 await response . CopyToAsync ( file ) ;
4949 return resultPath ;
5050}
5151
52- static async Task < string > ScreenshotFromUrl ( string destinationDirectory , GotenbergSharpClientOptions options )
52+ static async Task < string > ScreenshotFromUrl ( string destinationDirectory , GotenbergSharpClient sharpClient )
5353{
54- var sharpClient = CreateClient ( options ) ;
55-
5654 var builder = new ScreenshotUrlRequestBuilder ( )
5755 . SetUrl ( "https://example.com" )
5856 . WithScreenshotProperties ( p => p
@@ -61,7 +59,7 @@ static async Task<string> ScreenshotFromUrl(string destinationDirectory, Gotenbe
6159 . SetQuality ( 90 )
6260 . SetClip ( ) ) ;
6361
64- var response = await sharpClient . ScreenshotUrlAsync ( builder ) ;
62+ await using var response = await sharpClient . ScreenshotUrlAsync ( builder ) ;
6563
6664 var resultPath = Path . Combine ( destinationDirectory , $ "ScreenshotUrl-{ DateTime . Now : yyyyMMddHHmmss} .jpg") ;
6765 await using var file = File . Create ( resultPath ) ;
0 commit comments