WIP Estimate to pdf
This commit is contained in:
55
Yavsc/Views/FrontOffice/Estimate.pdf.cshtml
Normal file
55
Yavsc/Views/FrontOffice/Estimate.pdf.cshtml
Normal file
@ -0,0 +1,55 @@
|
||||
|
||||
@using System.Reflection
|
||||
@using System.IO
|
||||
@using Microsoft.Extensions.WebEncoders
|
||||
@using System.Diagnostics
|
||||
@using System.Text
|
||||
@using Yavsc.Formatters
|
||||
|
||||
@model Estimate
|
||||
@{
|
||||
Layout = null;
|
||||
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 = $"tmpestimtex-{Model.Id}";
|
||||
string fullname = new FileInfo(
|
||||
System.IO.Path.Combine(ViewBag.TempDir,name)).FullName;
|
||||
|
||||
FileInfo fi = new FileInfo(fullname + ".tex");
|
||||
FileInfo fo = new FileInfo(fullname + ".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);
|
||||
}
|
||||
}
|
||||
if (fo.Exists) {
|
||||
UTF8Encoding utf8 = new UTF8Encoding();
|
||||
|
||||
using (StreamReader sr = new StreamReader (fo.FullName)) {
|
||||
byte[] buffer = File.ReadAllBytes (fo.FullName);
|
||||
ViewBag.Pdf = utf8.GetString(buffer,0,buffer.Length);
|
||||
}
|
||||
fo.Delete();
|
||||
}
|
||||
fi.Delete();
|
||||
}
|
||||
@ViewBag.Pdf
|
Reference in New Issue
Block a user