Add CV template preview and PDF export pipeline
This commit is contained in:
@@ -8,6 +8,7 @@ namespace JobTrackerApi.Services
|
||||
public string DataRoot { get; }
|
||||
public string AttachmentsRoot { get; }
|
||||
public string CvArtifactsRoot { get; }
|
||||
public string CvExportsRoot { get; }
|
||||
|
||||
public AppPaths(IConfiguration cfg, IHostEnvironment env)
|
||||
{
|
||||
@@ -31,6 +32,13 @@ namespace JobTrackerApi.Services
|
||||
|
||||
Directory.CreateDirectory(cvArtifactsRoot);
|
||||
CvArtifactsRoot = cvArtifactsRoot;
|
||||
|
||||
var cvExportsRoot = (cfg["Data:CvExportsRoot"] ?? "").Trim();
|
||||
if (string.IsNullOrWhiteSpace(cvExportsRoot)) cvExportsRoot = Path.Combine(DataRoot, "CvExports");
|
||||
if (!Path.IsPathRooted(cvExportsRoot)) cvExportsRoot = Path.Combine(env.ContentRootPath, cvExportsRoot);
|
||||
|
||||
Directory.CreateDirectory(cvExportsRoot);
|
||||
CvExportsRoot = cvExportsRoot;
|
||||
}
|
||||
|
||||
public string GetDbPath(string fileName = "jobtracker.db") => Path.Combine(DataRoot, fileName);
|
||||
|
||||
@@ -0,0 +1,341 @@
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using JobTrackerApi.Models;
|
||||
|
||||
namespace JobTrackerApi.Services;
|
||||
|
||||
public sealed record TailoredCvRenderResult(string TemplateId, string SuggestedFileName, string Html);
|
||||
|
||||
public interface ICvTemplateRenderer
|
||||
{
|
||||
TailoredCvRenderResult Render(TailoredCvDocument? document, string? templateId, string candidateName, string jobTitle, string? companyName, string? photoDataUrl = null);
|
||||
}
|
||||
|
||||
public sealed class CvTemplateRenderer : ICvTemplateRenderer
|
||||
{
|
||||
public TailoredCvRenderResult Render(TailoredCvDocument? document, string? templateId, string candidateName, string jobTitle, string? companyName, string? photoDataUrl = null)
|
||||
{
|
||||
var normalized = TailoredCvDraftJson.Normalize(document);
|
||||
var effectiveTemplateId = NormalizeTemplateId(templateId ?? normalized.TemplateId);
|
||||
normalized.TemplateId = effectiveTemplateId;
|
||||
var suggestedFileName = Slugify($"{candidateName}-{jobTitle}-{effectiveTemplateId}") + ".pdf";
|
||||
var html = effectiveTemplateId switch
|
||||
{
|
||||
"harvard" => RenderHarvard(normalized, candidateName, jobTitle, companyName),
|
||||
"auckland" => RenderSidebar(normalized, candidateName, jobTitle, companyName, photoDataUrl, "Auckland", roundedPhoto: false, curvedHeader: false),
|
||||
"edinburgh" => RenderSidebar(normalized, candidateName, jobTitle, companyName, photoDataUrl, "Edinburgh", roundedPhoto: true, curvedHeader: true),
|
||||
_ => RenderAtsMinimal(normalized, candidateName, jobTitle, companyName, photoDataUrl)
|
||||
};
|
||||
return new TailoredCvRenderResult(effectiveTemplateId, suggestedFileName, html);
|
||||
}
|
||||
|
||||
private static string NormalizeTemplateId(string? value)
|
||||
{
|
||||
var normalized = (value ?? string.Empty).Trim().ToLowerInvariant();
|
||||
return normalized switch
|
||||
{
|
||||
"base" => "ats-minimal",
|
||||
"legacy-text" => "ats-minimal",
|
||||
"harvard" => "harvard",
|
||||
"auckland" => "auckland",
|
||||
"edinburgh" => "edinburgh",
|
||||
_ => "ats-minimal"
|
||||
};
|
||||
}
|
||||
|
||||
private static string RenderAtsMinimal(TailoredCvDocument document, string candidateName, string jobTitle, string? companyName, string? photoDataUrl)
|
||||
{
|
||||
var accent = ResolveAccent(document.RenderOptions.AccentColor);
|
||||
var showPhoto = document.RenderOptions.ShowPhoto && !string.IsNullOrWhiteSpace(photoDataUrl);
|
||||
var body = RenderMainSections(document, accent, headingStyle: "caps-rule");
|
||||
var companyFocusMarkup = string.IsNullOrWhiteSpace(companyName) ? string.Empty : $"<span>Company focus: {Encode(companyName)}</span>";
|
||||
var photoMarkup = showPhoto ? $"<div class=\"photo\"><img src=\"{EncodeAttribute(photoDataUrl)}\" alt=\"Profile photo\" /></div>" : string.Empty;
|
||||
|
||||
return $@"<!DOCTYPE html>
|
||||
<html lang=""en"">
|
||||
<head>
|
||||
<meta charset=""utf-8"" />
|
||||
<title>{Encode(candidateName)} — ATS Minimal</title>
|
||||
<style>
|
||||
:root {{ --accent:{accent}; --ink:#111827; --muted:#4b5563; --line:#d1d5db; --paper:#fff; }}
|
||||
* {{ box-sizing:border-box; }}
|
||||
body {{ margin:0; background:#eef2f7; color:var(--ink); font-family:Georgia, 'Times New Roman', serif; -webkit-print-color-adjust:exact; print-color-adjust:exact; }}
|
||||
.page {{ width:210mm; min-height:297mm; margin:0 auto; background:var(--paper); padding:16mm; }}
|
||||
.header {{ display:grid; grid-template-columns:1fr auto; gap:6mm; border-bottom:2px solid var(--accent); padding-bottom:8mm; margin-bottom:7mm; }}
|
||||
.name {{ margin:0; font-size:25pt; letter-spacing:.02em; }}
|
||||
.headline {{ margin-top:2mm; color:var(--muted); font-size:11pt; }}
|
||||
.meta {{ margin-top:3mm; display:flex; flex-wrap:wrap; gap:3mm; color:var(--muted); font-size:9pt; }}
|
||||
.photo {{ width:28mm; height:36mm; border-radius:5mm; overflow:hidden; border:1px solid var(--line); }}
|
||||
.photo img {{ width:100%; height:100%; object-fit:cover; display:block; }}
|
||||
{BaseSectionCss(accent, "caps-rule")}
|
||||
@page {{ size:A4; margin:0; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class=""page"">
|
||||
<header class=""header"">
|
||||
<div>
|
||||
<h1 class=""name"">{Encode(candidateName)}</h1>
|
||||
<div class=""headline"">{Encode(document.Headline ?? jobTitle)}</div>
|
||||
<div class=""meta""><span>Target role: {Encode(jobTitle)}</span>{companyFocusMarkup}<span>Template: ATS Minimal</span></div>
|
||||
</div>
|
||||
{photoMarkup}
|
||||
</header>
|
||||
{body}
|
||||
</main>
|
||||
</body>
|
||||
</html>";
|
||||
}
|
||||
|
||||
private static string RenderHarvard(TailoredCvDocument document, string candidateName, string jobTitle, string? companyName)
|
||||
{
|
||||
var accent = ResolveAccent(document.RenderOptions.AccentColor);
|
||||
var body = RenderMainSections(document, accent, headingStyle: "harvard");
|
||||
var contactLine = string.Join(" • ", new[]
|
||||
{
|
||||
string.IsNullOrWhiteSpace(companyName) ? null : $"Targeting {Encode(companyName)}",
|
||||
Encode(jobTitle)
|
||||
}.Where(x => !string.IsNullOrWhiteSpace(x)));
|
||||
|
||||
return $@"<!DOCTYPE html>
|
||||
<html lang=""en"">
|
||||
<head>
|
||||
<meta charset=""utf-8"" />
|
||||
<title>{Encode(candidateName)} — Harvard</title>
|
||||
<style>
|
||||
:root {{ --accent:{accent}; --ink:#111; --muted:#333; --line:#111; --paper:#fff; }}
|
||||
* {{ box-sizing:border-box; }}
|
||||
body {{ margin:0; background:#f5f5f5; color:var(--ink); font-family:Georgia, 'Times New Roman', serif; -webkit-print-color-adjust:exact; print-color-adjust:exact; }}
|
||||
.page {{ width:210mm; min-height:297mm; margin:0 auto; background:#fff; padding:16mm 18mm; }}
|
||||
.header {{ text-align:center; margin-bottom:6mm; }}
|
||||
.name {{ margin:0; font-size:23pt; font-weight:700; }}
|
||||
.headline {{ margin-top:2mm; font-size:10pt; font-style:italic; }}
|
||||
.meta {{ margin-top:4mm; font-size:9pt; }}
|
||||
{BaseSectionCss(accent, "harvard")}
|
||||
.section-title {{ color:var(--ink); }}
|
||||
@page {{ size:A4; margin:0; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class=""page"">
|
||||
<header class=""header"">
|
||||
<h1 class=""name"">{Encode(candidateName)}</h1>
|
||||
<div class=""headline"">{Encode(document.Headline ?? jobTitle)}</div>
|
||||
<div class=""meta"">{contactLine}</div>
|
||||
</header>
|
||||
{body}
|
||||
</main>
|
||||
</body>
|
||||
</html>";
|
||||
}
|
||||
|
||||
private static string RenderSidebar(TailoredCvDocument document, string candidateName, string jobTitle, string? companyName, string? photoDataUrl, string templateLabel, bool roundedPhoto, bool curvedHeader)
|
||||
{
|
||||
var accent = ResolveAccent(document.RenderOptions.AccentColor);
|
||||
var showPhoto = document.RenderOptions.ShowPhoto && !string.IsNullOrWhiteSpace(photoDataUrl);
|
||||
var sidebarSections = new StringBuilder();
|
||||
sidebarSections.Append(RenderSidebarMetaSection("Personal Details", new[]
|
||||
{
|
||||
$"Name\n{Encode(candidateName)}",
|
||||
$"Target role\n{Encode(jobTitle)}",
|
||||
string.IsNullOrWhiteSpace(companyName) ? null : $"Company focus\n{Encode(companyName)}"
|
||||
}));
|
||||
if (document.CustomSections.Count > 0)
|
||||
{
|
||||
foreach (var section in document.CustomSections.Take(2))
|
||||
{
|
||||
sidebarSections.Append(RenderSidebarMetaSection(section.Title ?? "Additional", section.Items));
|
||||
}
|
||||
}
|
||||
if (document.SelectedSkills.Count > 0)
|
||||
{
|
||||
sidebarSections.Append(RenderSidebarMetaSection("Skills", document.SelectedSkills.Take(8)));
|
||||
}
|
||||
|
||||
var main = RenderMainSections(document, accent, headingStyle: "sidebar");
|
||||
var photoClass = roundedPhoto ? "photo round" : "photo";
|
||||
var photoMarkup = showPhoto ? $"<div class=\"{photoClass}\"><img src=\"{EncodeAttribute(photoDataUrl)}\" alt=\"Profile photo\" /></div>" : string.Empty;
|
||||
var heroClass = curvedHeader ? "hero curved" : "hero";
|
||||
|
||||
return $@"<!DOCTYPE html>
|
||||
<html lang=""en"">
|
||||
<head>
|
||||
<meta charset=""utf-8"" />
|
||||
<title>{Encode(candidateName)} — {Encode(templateLabel)}</title>
|
||||
<style>
|
||||
:root {{ --accent:{accent}; --ink:#1f2937; --muted:#4b5563; --line:#d1d5db; --sidebar:#f3f4f6; --paper:#fff; }}
|
||||
* {{ box-sizing:border-box; }}
|
||||
body {{ margin:0; background:#edf2f7; color:var(--ink); font-family:Arial, Helvetica, sans-serif; -webkit-print-color-adjust:exact; print-color-adjust:exact; }}
|
||||
.page {{ width:210mm; min-height:297mm; margin:0 auto; background:#fff; display:grid; grid-template-columns:34% 66%; }}
|
||||
.sidebar {{ background:linear-gradient(180deg, var(--accent), color-mix(in srgb, var(--accent) 8%, white)); color:#fff; padding:12mm 8mm 12mm 10mm; }}
|
||||
.hero {{ margin:-12mm -8mm 8mm -10mm; padding:10mm 10mm 8mm 10mm; background:var(--accent); }}
|
||||
.hero.curved {{ border-bottom-right-radius:28mm; }}
|
||||
.name {{ margin:0; font-size:18pt; letter-spacing:.08em; font-weight:700; }}
|
||||
.headline {{ margin-top:2mm; font-size:10pt; opacity:.95; }}
|
||||
.photo {{ width:34mm; height:34mm; margin-top:6mm; border:2px solid rgba(255,255,255,.85); overflow:hidden; }}
|
||||
.photo.round {{ border-radius:50%; }}
|
||||
.photo img {{ width:100%; height:100%; object-fit:cover; display:block; }}
|
||||
.sidebar-section {{ margin-top:7mm; }}
|
||||
.sidebar-title {{ margin:0 0 3mm 0; font-size:9pt; text-transform:uppercase; letter-spacing:.16em; }}
|
||||
.sidebar-item {{ margin:0 0 2.4mm 0; font-size:8.8pt; line-height:1.4; white-space:pre-line; }}
|
||||
.content {{ padding:14mm 14mm 14mm 10mm; }}
|
||||
{BaseSectionCss(accent, "sidebar")}
|
||||
@page {{ size:A4; margin:0; }}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main class=""page"">
|
||||
<aside class=""sidebar"">
|
||||
<div class=""{heroClass}"">
|
||||
<h1 class=""name"">{Encode(candidateName)}</h1>
|
||||
<div class=""headline"">{Encode(document.Headline ?? jobTitle)}</div>
|
||||
{photoMarkup}
|
||||
</div>
|
||||
{sidebarSections}
|
||||
</aside>
|
||||
<section class=""content"">
|
||||
{main}
|
||||
</section>
|
||||
</main>
|
||||
</body>
|
||||
</html>";
|
||||
}
|
||||
|
||||
private static string RenderMainSections(TailoredCvDocument document, string accent, string headingStyle)
|
||||
{
|
||||
var sectionOrder = document.RenderOptions.SectionOrder.Count == 0
|
||||
? new List<string> { "summary", "skills", "experience", "education", "custom" }
|
||||
: document.RenderOptions.SectionOrder;
|
||||
|
||||
var sections = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
|
||||
{
|
||||
["summary"] = RenderListSection("Profile", document.Summary, bulletList: true),
|
||||
["skills"] = RenderSkillSection(document.SelectedSkills),
|
||||
["experience"] = RenderExperienceSection(document.Experience),
|
||||
["education"] = RenderEducationSection(document.Education),
|
||||
["custom"] = string.Join(string.Empty, document.CustomSections.Select(RenderCustomSection)),
|
||||
};
|
||||
|
||||
return string.Join(string.Empty, sectionOrder
|
||||
.Select(key => sections.TryGetValue(key, out var section) ? section : string.Empty)
|
||||
.Where(section => !string.IsNullOrWhiteSpace(section)));
|
||||
}
|
||||
|
||||
private static string BaseSectionCss(string accent, string headingStyle)
|
||||
{
|
||||
var headingCss = headingStyle switch
|
||||
{
|
||||
"harvard" => ".section-title{font-size:17pt;font-weight:700;border-bottom:1.5px solid var(--line);padding-bottom:1.5mm;margin-bottom:3mm;}",
|
||||
"sidebar" => ".section-title{font-size:14pt;font-weight:700;letter-spacing:.02em;margin-bottom:3mm;}",
|
||||
_ => ".section-title{font-size:9pt;letter-spacing:.16em;text-transform:uppercase;color:var(--accent);border-bottom:1px solid var(--line);padding-bottom:1.5mm;margin-bottom:3mm;}"
|
||||
};
|
||||
|
||||
return $@"
|
||||
.section{{margin-top:6mm;}}
|
||||
{headingCss}
|
||||
.summary,.custom-list,.education-list,.experience-bullets{{margin:0;padding-left:4.5mm;}}
|
||||
.summary li,.custom-list li,.education-list li,.experience-bullets li{{margin:0 0 1.6mm 0;line-height:1.42;}}
|
||||
.skills{{list-style:none;padding-left:0;display:flex;flex-wrap:wrap;gap:2mm;}}
|
||||
.skill-pill{{border:1px solid var(--line);border-radius:999px;padding:1mm 2.4mm;font-size:9pt;}}
|
||||
.entry{{margin-bottom:4.8mm;}}
|
||||
.entry-header{{display:flex;justify-content:space-between;gap:4mm;align-items:baseline;margin-bottom:1.2mm;}}
|
||||
.entry-title{{font-weight:700;font-size:11pt;}}
|
||||
.entry-meta{{color:var(--muted);font-size:9pt;text-align:right;white-space:nowrap;}}
|
||||
.entry-subtitle{{color:var(--muted);font-size:9.5pt;margin-bottom:1.3mm;}}";
|
||||
}
|
||||
|
||||
private static string RenderSidebarMetaSection(string title, IEnumerable<string?> items)
|
||||
{
|
||||
var content = string.Join(string.Empty, items.Where(item => !string.IsNullOrWhiteSpace(item)).Select(item => $"<p class=\"sidebar-item\">{item}</p>"));
|
||||
if (string.IsNullOrWhiteSpace(content)) return string.Empty;
|
||||
return $"<section class=\"sidebar-section\"><h2 class=\"sidebar-title\">{Encode(title)}</h2>{content}</section>";
|
||||
}
|
||||
|
||||
private static string RenderListSection(string title, IReadOnlyCollection<string> items, bool bulletList)
|
||||
{
|
||||
if (items.Count == 0) return string.Empty;
|
||||
var tag = bulletList ? "summary" : "custom-list";
|
||||
return $"<section class=\"section\"><h2 class=\"section-title\">{Encode(title)}</h2><ul class=\"{tag}\">{string.Join(string.Empty, items.Select(item => $"<li>{Encode(item)}</li>"))}</ul></section>";
|
||||
}
|
||||
|
||||
private static string RenderSkillSection(IReadOnlyCollection<string> skills)
|
||||
{
|
||||
if (skills.Count == 0) return string.Empty;
|
||||
return $"<section class=\"section\"><h2 class=\"section-title\">Skills</h2><ul class=\"skills\">{string.Join(string.Empty, skills.Select(skill => $"<li class=\"skill-pill\">{Encode(skill)}</li>"))}</ul></section>";
|
||||
}
|
||||
|
||||
private static string RenderExperienceSection(IReadOnlyCollection<TailoredCvExperienceItem> experience)
|
||||
{
|
||||
if (experience.Count == 0) return string.Empty;
|
||||
var items = new StringBuilder();
|
||||
foreach (var entry in experience)
|
||||
{
|
||||
var subtitle = string.Join(" · ", new[] { entry.Company, entry.Location }.Where(x => !string.IsNullOrWhiteSpace(x)).Select(Encode));
|
||||
var dateRange = FormatDateRange(entry.Start, entry.End, entry.IsCurrent);
|
||||
items.Append("<article class=\"entry\">");
|
||||
items.Append($"<div class=\"entry-header\"><div class=\"entry-title\">{Encode(entry.Title)}</div><div class=\"entry-meta\">{Encode(dateRange)}</div></div>");
|
||||
if (!string.IsNullOrWhiteSpace(subtitle)) items.Append($"<div class=\"entry-subtitle\">{subtitle}</div>");
|
||||
if (entry.Bullets.Count > 0) items.Append($"<ul class=\"experience-bullets\">{string.Join(string.Empty, entry.Bullets.Select(bullet => $"<li>{Encode(bullet)}</li>"))}</ul>");
|
||||
items.Append("</article>");
|
||||
}
|
||||
return $"<section class=\"section\"><h2 class=\"section-title\">Professional Experience</h2>{items}</section>";
|
||||
}
|
||||
|
||||
private static string RenderEducationSection(IReadOnlyCollection<TailoredCvEducationItem> education)
|
||||
{
|
||||
if (education.Count == 0) return string.Empty;
|
||||
var items = new StringBuilder();
|
||||
foreach (var entry in education)
|
||||
{
|
||||
var subtitle = string.Join(" · ", new[] { entry.Institution, entry.Location, FormatDateRange(entry.Start, entry.End, false) }
|
||||
.Where(x => !string.IsNullOrWhiteSpace(x))
|
||||
.Select(Encode));
|
||||
items.Append("<article class=\"entry\">");
|
||||
items.Append($"<div class=\"entry-title\">{Encode(entry.Qualification)}</div>");
|
||||
if (!string.IsNullOrWhiteSpace(subtitle)) items.Append($"<div class=\"entry-subtitle\">{subtitle}</div>");
|
||||
if (entry.Details.Count > 0) items.Append($"<ul class=\"education-list\">{string.Join(string.Empty, entry.Details.Select(detail => $"<li>{Encode(detail)}</li>"))}</ul>");
|
||||
items.Append("</article>");
|
||||
}
|
||||
return $"<section class=\"section\"><h2 class=\"section-title\">Education</h2>{items}</section>";
|
||||
}
|
||||
|
||||
private static string RenderCustomSection(TailoredCvCustomSection section)
|
||||
{
|
||||
if (section.Items.Count == 0) return string.Empty;
|
||||
return $"<section class=\"section\"><h2 class=\"section-title\">{Encode(section.Title ?? "Additional Information")}</h2><ul class=\"custom-list\">{string.Join(string.Empty, section.Items.Select(item => $"<li>{Encode(item)}</li>"))}</ul></section>";
|
||||
}
|
||||
|
||||
private static string FormatDateRange(string? start, string? end, bool isCurrent)
|
||||
{
|
||||
var normalizedStart = (start ?? string.Empty).Trim();
|
||||
var normalizedEnd = (end ?? string.Empty).Trim();
|
||||
if (string.IsNullOrWhiteSpace(normalizedStart) && string.IsNullOrWhiteSpace(normalizedEnd)) return string.Empty;
|
||||
if (string.IsNullOrWhiteSpace(normalizedStart)) return normalizedEnd;
|
||||
return $"{normalizedStart} - {(isCurrent ? "Present" : string.IsNullOrWhiteSpace(normalizedEnd) ? "Present" : normalizedEnd)}";
|
||||
}
|
||||
|
||||
private static string ResolveAccent(string? accentColor)
|
||||
{
|
||||
var normalized = (accentColor ?? string.Empty).Trim().ToLowerInvariant();
|
||||
return normalized switch
|
||||
{
|
||||
"slate" => "#334155",
|
||||
"blue" => "#1d4ed8",
|
||||
"emerald" => "#047857",
|
||||
"plum" => "#7c3aed",
|
||||
"brick" => "#b45309",
|
||||
_ when normalized.StartsWith("#") => normalized,
|
||||
_ => "#334155"
|
||||
};
|
||||
}
|
||||
|
||||
private static string Encode(string? value) => WebUtility.HtmlEncode(value ?? string.Empty);
|
||||
private static string EncodeAttribute(string? value) => WebUtility.HtmlEncode(value ?? string.Empty).Replace("'", "'", StringComparison.Ordinal);
|
||||
|
||||
private static string Slugify(string value)
|
||||
{
|
||||
var cleaned = new string((value ?? string.Empty).ToLowerInvariant().Select(ch => char.IsLetterOrDigit(ch) ? ch : '-').ToArray());
|
||||
while (cleaned.Contains("--", StringComparison.Ordinal)) cleaned = cleaned.Replace("--", "-", StringComparison.Ordinal);
|
||||
return cleaned.Trim('-');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using Microsoft.Playwright;
|
||||
|
||||
namespace JobTrackerApi.Services;
|
||||
|
||||
public sealed record CvPdfArtifact(string FileName, string StoragePath, byte[] Bytes);
|
||||
|
||||
public interface ICvPdfExporter
|
||||
{
|
||||
Task<CvPdfArtifact> ExportAsync(TailoredCvRenderResult renderResult, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
public sealed class PlaywrightCvPdfExporter : ICvPdfExporter
|
||||
{
|
||||
private readonly AppPaths _paths;
|
||||
private readonly ILogger<PlaywrightCvPdfExporter> _logger;
|
||||
|
||||
public PlaywrightCvPdfExporter(AppPaths paths, ILogger<PlaywrightCvPdfExporter> logger)
|
||||
{
|
||||
_paths = paths;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<CvPdfArtifact> ExportAsync(TailoredCvRenderResult renderResult, CancellationToken cancellationToken)
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var folder = Path.Combine(_paths.CvExportsRoot, now.ToString("yyyyMMdd"));
|
||||
Directory.CreateDirectory(folder);
|
||||
var fileName = string.IsNullOrWhiteSpace(renderResult.SuggestedFileName)
|
||||
? $"tailored-cv-{now:yyyyMMddHHmmss}.pdf"
|
||||
: renderResult.SuggestedFileName;
|
||||
var storagePath = Path.Combine(folder, fileName);
|
||||
|
||||
try
|
||||
{
|
||||
using var playwright = await Playwright.CreateAsync();
|
||||
await using var browser = await playwright.Chromium.LaunchAsync(new BrowserTypeLaunchOptions
|
||||
{
|
||||
Headless = true,
|
||||
});
|
||||
var page = await browser.NewPageAsync();
|
||||
await page.SetContentAsync(renderResult.Html, new PageSetContentOptions
|
||||
{
|
||||
WaitUntil = WaitUntilState.Load,
|
||||
});
|
||||
var bytes = await page.PdfAsync(new PagePdfOptions
|
||||
{
|
||||
Format = "A4",
|
||||
PrintBackground = true,
|
||||
Margin = new()
|
||||
{
|
||||
Top = "0",
|
||||
Right = "0",
|
||||
Bottom = "0",
|
||||
Left = "0",
|
||||
}
|
||||
});
|
||||
await File.WriteAllBytesAsync(storagePath, bytes, cancellationToken);
|
||||
return new CvPdfArtifact(fileName, storagePath, bytes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Failed to export CV PDF to {Path}", storagePath);
|
||||
throw new InvalidOperationException("CV PDF export is unavailable. Ensure Chromium is installed for Playwright on this machine.", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user