-
-
Notifications
You must be signed in to change notification settings - Fork 487
Closed
Description
Hi,
I am encountering an issue while converting HTML to PDF using PuppeteerSharp. Specifically, the images are not displaying in the generated PDF. Despite following various solutions suggested on StackOverflow, the problem persists. Below is the method I am using to perform the HTML to PDF conversion. I would appreciate any guidance or solutions you could provide.
public async Task HtmlToPdf(string htmlContent, string fileName)
{
var startTime = DateTimeOffset.UtcNow;
_logger.LogInformation("Executing GeneratePdf: {fileName}...", fileName);
var launchOptions = new LaunchOptions { Headless = true };
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
using (var browser = await Puppeteer.LaunchAsync(launchOptions))
using (var page = await browser.NewPageAsync())
{
await page.SetContentAsync(htmlContent);
await page.AddStyleTagAsync(new AddTagOptions { Content = "body { font-size: 20px; margin: 50px; }" });
await page.WaitForSelectorAsync("img");
var pdfBytes = await page.PdfDataAsync(new PdfOptions { Format = PaperFormat.A4 });
var filePath = Path.Combine(Path.GetTempPath(), fileName);
File.WriteAllBytes(filePath, pdfBytes);
var formFile = await CreateFormFile(filePath, fileName);
File.Delete(filePath);
var endTime = DateTimeOffset.UtcNow;
var executionTime = endTime - startTime;
_logger.LogInformation("Time taken to execute GeneratePdf: {fileName}. Time: {ExecutionTime} ms",
fileName, executionTime.TotalMilliseconds);
return formFile;
}
}
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels