@@ -33,8 +33,7 @@ class Outline
3333
3434 public bool pdf { get ; init ; }
3535 public string ? pdfFileName { get ; init ; }
36- public string ? pdfMargin { get ; init ; }
37- public bool pdfPrintBackground { get ; init ; }
36+ public string ? pdfCoverPage { get ; init ; }
3837 }
3938
4039 public static Task Run ( BuildJsonConfig config , string configDirectory , string ? outputDirectory = null )
@@ -111,24 +110,22 @@ static async Task CreatePdf(IBrowser browser, Uri outlineUrl, Outline outline, s
111110 var pageNumbers = new Dictionary < Outline , int > ( ) ;
112111 var nextPageNumbers = new Dictionary < Outline , int > ( ) ;
113112 var nextPageNumber = 1 ;
114- var margin = outline . pdfMargin ?? "0.4in" ;
115113
116114 await AnsiConsole . Progress ( ) . Columns ( new SpinnerColumn ( ) , new TaskDescriptionColumn { Alignment = Justify . Left } ) . StartAsync ( async c =>
117115 {
118116 await Parallel . ForEachAsync ( pages , async ( item , CancellationToken ) =>
119117 {
120118 var task = c . AddTask ( item . url . PathAndQuery ) ;
121119 var page = await browser . NewPageAsync ( ) ;
122- await page . GotoAsync ( item . url . ToString ( ) ) ;
120+ var response = await page . GotoAsync ( item . url . ToString ( ) ) ;
121+ if ( response is null || ! response . Ok )
122+ throw new InvalidOperationException ( $ "Failed to build PDF page [{ response ? . Status } ]: { item . url } ") ;
123+
123124 await page . AddScriptTagAsync ( new ( ) { Content = EnsureHeadingAnchorScript } ) ;
124125 await page . WaitForLoadStateAsync ( LoadState . NetworkIdle ) ;
125- var bytes = await page . PdfAsync ( new ( )
126- {
127- PrintBackground = outline . pdfPrintBackground ,
128- Margin = new ( ) { Bottom = margin , Top = margin , Left = margin , Right = margin } ,
129- } ) ;
126+ var bytes = await page . PdfAsync ( ) ;
130127 File . WriteAllBytes ( item . path , bytes ) ;
131- task . Value = task . MaxValue ;
128+ task . StopTask ( ) ;
132129 } ) ;
133130 } ) ;
134131
@@ -137,21 +134,33 @@ await Parallel.ForEachAsync(pages, async (item, CancellationToken) =>
137134
138135 IEnumerable < ( string path , Uri url , Outline node ) > GetPages ( Outline outline )
139136 {
137+ if ( ! string . IsNullOrEmpty ( outline . pdfCoverPage ) )
138+ {
139+ var url = new Uri ( outlineUrl , outline . pdfCoverPage ) ;
140+ if ( url . Host == outlineUrl . Host )
141+ yield return ( GetFilePath ( url ) , url , new ( ) { href = outline . pdfCoverPage } ) ;
142+ }
143+
140144 if ( ! string . IsNullOrEmpty ( outline . href ) )
141145 {
142146 var url = new Uri ( outlineUrl , outline . href ) ;
143147 if ( url . Host == outlineUrl . Host )
144- {
145- var id = Convert . ToHexString ( SHA256 . Create ( ) . ComputeHash ( Encoding . UTF8 . GetBytes ( url . ToString ( ) ) ) ) . Substring ( 0 , 6 ) . ToLower ( ) ;
146- var name = Regex . Replace ( url . PathAndQuery , "\\ W" , "-" ) . Trim ( '-' ) ;
147- yield return ( Path . Combine ( tempDirectory , $ "{ name } -{ id } .pdf") , url , outline ) ;
148- }
148+ yield return ( GetFilePath ( url ) , url , outline ) ;
149149 }
150150
151151 if ( outline . items != null )
152+ {
152153 foreach ( var item in outline . items )
153154 foreach ( var url in GetPages ( item ) )
154155 yield return url ;
156+ }
157+ }
158+
159+ string GetFilePath ( Uri url )
160+ {
161+ var id = Convert . ToHexString ( SHA256 . Create ( ) . ComputeHash ( Encoding . UTF8 . GetBytes ( url . ToString ( ) ) ) ) . Substring ( 0 , 6 ) . ToLower ( ) ;
162+ var name = Regex . Replace ( url . PathAndQuery , "\\ W" , "-" ) . Trim ( '-' ) ;
163+ return Path . Combine ( tempDirectory , $ "{ name } -{ id } .pdf") ;
155164 }
156165
157166 void MergePdf ( )
@@ -254,11 +263,14 @@ IEnumerable<BookmarkNode> CreateBookmarks(Outline[]? items, int level = 0)
254263 continue ;
255264 }
256265
257- nextPageNumber = nextPageNumbers [ item ] ;
258- yield return new DocumentBookmarkNode (
259- item . name , level ,
260- new ( pageNumbers [ item ] , ExplicitDestinationType . XyzCoordinates , ExplicitDestinationCoordinates . Empty ) ,
261- CreateBookmarks ( item . items , level + 1 ) . ToArray ( ) ) ;
266+ if ( ! string . IsNullOrEmpty ( item . name ) )
267+ {
268+ nextPageNumber = nextPageNumbers [ item ] ;
269+ yield return new DocumentBookmarkNode (
270+ item . name , level ,
271+ new ( pageNumbers [ item ] , ExplicitDestinationType . XyzCoordinates , ExplicitDestinationCoordinates . Empty ) ,
272+ CreateBookmarks ( item . items , level + 1 ) . ToArray ( ) ) ;
273+ }
262274 }
263275 }
264276 }
0 commit comments