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

51 lines
1.7 KiB
Text
Raw Normal View History

2016-11-07 19:34:56 +01:00
@using System.Reflection;
@using System.IO;
@using Microsoft.Extensions.WebEncoders;
@using System.Diagnostics;
@using System.Text;
@using Yavsc.Formatters;
2016-05-17 18:22:18 +02:00
@model Estimate
@{
2016-11-07 19:34:56 +01:00
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 );
2016-05-17 18:22:18 +02:00
content.WriteTo(writer, new TexEncoder());
var contentStr = writer.ToString();
2016-11-07 19:34:56 +01:00
string name = $"estimate-{Model.Id}";
string fullname = new FileInfo(
2016-05-17 18:22:18 +02:00
System.IO.Path.Combine(ViewBag.TempDir,name)).FullName;
2016-11-07 19:34:56 +01:00
string ofullname = new FileInfo(
System.IO.Path.Combine(ViewBag.BillsDir,name)).FullName;
2016-05-17 18:22:18 +02:00
FileInfo fi = new FileInfo(fullname + ".tex");
2016-11-07 19:34:56 +01:00
FileInfo fo = new FileInfo(ofullname + ".pdf");
2016-05-17 18:22:18 +02:00
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);
}
}
2016-11-07 19:34:56 +01:00
ViewBag.Success = fo.Exists;
2016-05-17 18:22:18 +02:00
fi.Delete();
2016-11-07 19:34:56 +01:00
var uri = $"~/api/pdfestimate/{Model.Id}";
2016-05-17 18:22:18 +02:00
}
2016-11-07 19:34:56 +01:00
<a href="@uri" >@uri</a>