這裡推薦一個套件,真的滿厲害的
比起自己用itext刻快多了~
參考範例 :
特別說明,如果要讓pdf完整套用跟畫面一致,請記得設定參數唷
new PdfOptions() { PrintBackground = true, PreferCSSPageSize = true }
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions() { Headless = true })) { //參考頁面 https://www.puppeteersharp.com/api/PuppeteerSharp.ScreenshotOptions.html using (var page = await browser.NewPageAsync()) { var loginPath = $"https://google.com"; await page.GoToAsync(loginPath); await page.EmulateMediaTypeAsync(PuppeteerSharp.Media.MediaType.Screen); //透過SetViewport控制視窗大小決定抓圖尺寸 await page.SetViewportAsync(new ViewPortOptions { Width = 1024, Height = 768 }); var outputFile = $"test.pdf"; await page.PdfAsync(outputFile, new PdfOptions() { PrintBackground = true, PreferCSSPageSize = true }); } }
參考
黑暗blog :https://blog.darkthread.net/blog/headless-chrome/
Api說明 : https://www.puppeteersharp.com/api/PuppeteerSharp
沒有留言:
張貼留言