Skip to content

Commit 3835ac4

Browse files
committed
reverted back all the changes to PDF gen, will change the entire logic from puppeter to dink
1 parent 710d04f commit 3835ac4

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

Services/PdfGeneratorService.cs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using PuppeteerSharp;
2-
using PuppeteerSharp.Media;
1+
using PuppeteerSharp.Media;
2+
using PuppeteerSharp;
3+
using PuppeteerSharp.BrowserData;
34
using Scriban;
45

56
namespace PdfReportGeneratorAPI.Services
@@ -8,28 +9,29 @@ public class PdfGeneratorService : IPdfGeneratorService
89
{
910
public async Task<byte[]> GeneratePdfAsync<T>(T model, string template)
1011
{
12+
// we are passing the string of html(converted into text) into Render but first we are pasrsing it
13+
// so that it can fill in all the placeholders using the Render(Model) method.
1114
var parsedTemplate = Template.Parse(template);
1215
var htmlContent = parsedTemplate.Render(model, member => member.Name);
1316

14-
// Download Chromium browser if not already downloaded
15-
var browserFetcher = new BrowserFetcher();
16-
var revisionInfo = await browserFetcher.DownloadAsync();
17+
var browserDownloaded = false;
1718

18-
var launchOptions = new LaunchOptions
19+
if (!browserDownloaded)
1920
{
20-
Headless = true,
21-
ExecutablePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe", // Adjust path if needed
22-
Args = new[]
23-
{
24-
"--no-sandbox",
25-
"--disable-setuid-sandbox"
26-
}
27-
};
28-
21+
var browserFetcher = new BrowserFetcher();
22+
await browserFetcher.DownloadAsync();
23+
browserDownloaded = true;
24+
}
2925

30-
await using var browser = await Puppeteer.LaunchAsync(launchOptions);
31-
await using var page = await browser.NewPageAsync();
26+
// launching browser(after downloading it)
27+
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions
28+
{
29+
Headless = true
30+
}) ;
3231

32+
33+
await using var page = await browser.NewPageAsync(); // now we are setting the browser's page
34+
// content to the html content we made
3335
await page.SetContentAsync(htmlContent);
3436

3537
var pdfBytes = await page.PdfDataAsync(new PdfOptions
@@ -39,6 +41,10 @@ public async Task<byte[]> GeneratePdfAsync<T>(T model, string template)
3941
});
4042

4143
return pdfBytes;
44+
45+
46+
47+
4248
}
4349
}
4450
}

0 commit comments

Comments
 (0)