带PuppeteerSharp的AspNet核心剃刀视图

时间:2019-04-05 11:42:00

标签: asp.net-core razor-pages puppeteer-sharp

我们可以在Razor View中使用PuppeteerSharp吗?我想从AspNetCore应用程序内的Razor View发送HTML,并以PDF格式获取输出。任何参考或代码示例都会有所帮助

2 个答案:

答案 0 :(得分:0)

您可以使用SetContentAsync注入HTML,然后调用PdfAsync

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = false,
}))
using (var page = await browser.NewPageAsync())
{
    await page.SetContentAsync("Hello World");
    await page.PdfAsync("test.pdf");
}

答案 1 :(得分:0)

根据github,您可以生成这样的PDF文件

await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
    Headless = false
});
var page = await browser.NewPageAsync();
await page.GoToAsync("http://localhost/your/page");
Stream pdfStream = await page.PdfStreamAsync();