Home > front end >  Puppeteer sharp html to pdf with links enabled
Puppeteer sharp html to pdf with links enabled

Time:01-26

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.

CodePudding user response:

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.

  • Related