简体   繁体   中英

Puppeteer sharp html to pdf with links enabled

Can anyone tell me how to enable the links in pdf which are present in html. For example an a tag link. I am using Puppeteer sharp for conversion of html to pdf.

You can use the page.pdf() function to enable links in a PDF that are present in the html. The method will allow you to specify the options like including the links in the pdf that will be generated. An example is here:

using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
using (var page = await browser.NewPageAsync())
{
    await page.SetContentAsync(html);

    // Enabling links in the pdf generated
    var pdfOptions = new PdfOptions { DisplayHeaderFooter = true, Landscape = true, PrintBackground = true, Format = PaperFormat.A4, MarginOptions = new MarginOptions { Top = "1cm", Bottom = "1cm", Left = "1cm", Right = "1cm" }, Scale = 1.5, };
    await page.PdfAsync(outputPath, pdfOptions);
}

Also, you can specify the other options like the margins, scale, and format using the PdfOptions object. I hope this one helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM