A view component

This commit is contained in:
Paul Schneider 2016-11-09 14:03:57 +01:00
commit e74a81deca
11 changed files with 258 additions and 190 deletions

View file

@ -39,6 +39,11 @@
<a asp-action="Index">@SR["Back to List"]</a> |
@{ var filenametex = $"estimate-{Model.Id}.tex" ;
var filenamepdf = $"estimate-{Model.Id}.pdf" ;}
<a href="~/do/@filenametex" >Export au format LaTeX</a>
<a href="~/do/@filenamepdf" >Export au format Pdf</a>
<a href="~/api/pdfestimate/gettex/@Model.Id" >Export au format LaTeX</a> |
<form action="~/api/pdfestimate/gen/@Model.Id" method="GET">
<input type="submit" value="Générer le Pdf"/>
</form>
</p>

View file

@ -1,51 +0,0 @@
@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>

View file

@ -0,0 +1,50 @@
@using System.Reflection
@using System.IO
@using Microsoft.Extensions.WebEncoders
@using System.Diagnostics
@using System.Text
@using Yavsc.Formatters
@model Yavsc.ViewModels.Gen.PdfGenerationViewModel
@{
string errorMsg = null;
var billdir = Model.DestDir;
var tempdir = Startup.SiteSetup.TempDir;
string name = Model.BaseFileName;
string fullname = new FileInfo(
System.IO.Path.Combine(tempdir,name)).FullName;
string ofullname = new FileInfo(
System.IO.Path.Combine(billdir,name)).FullName;
FileInfo fi = new FileInfo(fullname + ".tex");
FileInfo fo = new FileInfo(ofullname + ".pdf");
using (StreamWriter sw = new StreamWriter (fi.FullName))
{
sw.Write (Model.TeXSource);
}
if (!fi.Exists)
{
errorMsg = "Source write failed";
}
else {
using (Process p = new Process ()) {
p.StartInfo.WorkingDirectory = 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) {
errorMsg = $"Pdf generation failed with exit code: {p.ExitCode}";
}
}
fi.Delete();
}
ViewBag.GenSuccess = fo.Exists;
}
@if (ViewBag.GenSuccess) {
@($"{name}.pdf")
} else {
@errorMsg
<text>Something went wrong ...</text>
}

View file

@ -1,6 +1,6 @@
@model Estimate
@using Yavsc.Helpers
@using System.Globalization
@model Estimate
@{
Layout = null;
var pro = Model.Query.PerformerProfile;
@ -10,8 +10,7 @@
var proaddr = Model.Query?.PerformerProfile.OrganizationAddress.Address;
var proaddrn = (proaddr!=null) ? proaddr.NewLinesWith("\\\\\n") : null ;
var proaddrm = (proaddr!=null) ? proaddr.NewLinesWith(" - ") : null ;
}
\documentclass[french,11pt]{article}
}\documentclass[french,11pt]{article}
\usepackage{eurosym}
\usepackage{babel}
\usepackage[T1]{fontenc}