yavsc/Yavsc/Views/FrontOffice/Estimate.pdf.cshtml

51 lines
1.7 KiB
Plaintext

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

@using System.Reflection;
@using System.IO;
@using Microsoft.Extensions.WebEncoders;
@using System.Diagnostics;
@using System.Text;
@using Yavsc.Formatters;
@model Estimate
@{
if (ViewBag.TempDir==null) { throw new InvalidOperationException(); }
ViewBag.Pdf = "";
ViewBag.TeX = "";
var writer = new System.IO.StringWriter();
var content = await Html.PartialAsync("Estimate.tex", Model );
content.WriteTo(writer, new TexEncoder());
var contentStr = writer.ToString();
string name = $"estimate-{Model.Id}";
string fullname = new FileInfo(
System.IO.Path.Combine(ViewBag.TempDir,name)).FullName;
string ofullname = new FileInfo(
System.IO.Path.Combine(ViewBag.BillsDir,name)).FullName;
FileInfo fi = new FileInfo(fullname + ".tex");
FileInfo fo = new FileInfo(ofullname + ".pdf");
using (StreamWriter sw = new StreamWriter (fi.FullName))
{
sw.Write (contentStr);
}
if (!fi.Exists)
{
throw new Exception ("Source write failed");
}
using (Process p = new Process ()) {
p.StartInfo.WorkingDirectory = ViewBag.TempDir;
p.StartInfo = new ProcessStartInfo ();
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = "/usr/bin/texi2pdf";
p.StartInfo.Arguments = $"--batch --build-dir=. -o {fo.FullName} {fi.FullName}";
p.Start ();
p.WaitForExit ();
if (p.ExitCode != 0) {
throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode);
}
}
ViewBag.Success = fo.Exists;
fi.Delete();
var uri = $"~/api/pdfestimate/{Model.Id}";
}
<a href="@uri" >@uri</a>