refactoring Pdf generation
This commit is contained in:
parent
7a41fa55ac
commit
94ddffd5b2
3 changed files with 102 additions and 80 deletions
|
|
@ -79,13 +79,13 @@ namespace Yavsc.ApiControllers
|
||||||
public IActionResult GetTex(long id)
|
public IActionResult GetTex(long id)
|
||||||
{
|
{
|
||||||
Response.ContentType = "text/x-tex";
|
Response.ContentType = "text/x-tex";
|
||||||
return ViewComponent("Estimate",new object[] { id, false });
|
return ViewComponent("Estimate",new object[] { id, "LaTeX" });
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("gen/{id}")]
|
[HttpPost("gen/{id}")]
|
||||||
public IActionResult GeneratePdf(long id)
|
public IActionResult GeneratePdf(long id)
|
||||||
{
|
{
|
||||||
return ViewComponent("Estimate",new object[] { id, true } );
|
return ViewComponent("Estimate",new object[] { id, "Pdf" } );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,19 +1,24 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Diagnostics;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.AspNet.Http;
|
using Microsoft.AspNet.Http;
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
using Microsoft.AspNet.Mvc.ViewEngines;
|
using Microsoft.AspNet.Mvc.ViewEngines;
|
||||||
|
using Yavsc.ViewModels.Gen;
|
||||||
|
|
||||||
namespace Yavsc.Helpers
|
namespace Yavsc.Helpers
|
||||||
{
|
{
|
||||||
public class TeXString {
|
public class TeXString
|
||||||
|
{
|
||||||
|
|
||||||
public class Replacement {
|
public class Replacement
|
||||||
|
{
|
||||||
string target;
|
string target;
|
||||||
string replacement;
|
string replacement;
|
||||||
public Replacement(string target, string replacement){
|
public Replacement(string target, string replacement)
|
||||||
|
{
|
||||||
this.target = target;
|
this.target = target;
|
||||||
this.replacement = replacement;
|
this.replacement = replacement;
|
||||||
}
|
}
|
||||||
|
|
@ -49,9 +54,11 @@ namespace Yavsc.Helpers
|
||||||
new Replacement("–","\\textendash")
|
new Replacement("–","\\textendash")
|
||||||
};
|
};
|
||||||
string data;
|
string data;
|
||||||
public TeXString(string str) {
|
public TeXString(string str)
|
||||||
|
{
|
||||||
data = str;
|
data = str;
|
||||||
foreach (var r in SpecialCharsRendering) {
|
foreach (var r in SpecialCharsRendering)
|
||||||
|
{
|
||||||
data = r.Execute(data);
|
data = r.Execute(data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -76,6 +83,49 @@ namespace Yavsc.Helpers
|
||||||
if (target == null) return null;
|
if (target == null) return null;
|
||||||
return new TeXString(target.NewLinesWith(lineSeparator));
|
return new TeXString(target.NewLinesWith(lineSeparator));
|
||||||
}
|
}
|
||||||
|
public static bool GenerateEstimatePdf(this PdfGenerationViewModel Model)
|
||||||
|
{
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
Model.Generated = fo.Exists;
|
||||||
|
Model.GenerationErrorMessage = new HtmlString(errorMsg);
|
||||||
|
return fo.Exists;
|
||||||
|
}
|
||||||
|
|
||||||
public static string RenderViewToString(
|
public static string RenderViewToString(
|
||||||
this Controller controller, IViewEngine engine,
|
this Controller controller, IViewEngine engine,
|
||||||
|
|
|
||||||
|
|
@ -4,47 +4,19 @@
|
||||||
@using System.Diagnostics
|
@using System.Diagnostics
|
||||||
@using System.Text
|
@using System.Text
|
||||||
@using Yavsc.Formatters
|
@using Yavsc.Formatters
|
||||||
|
@using Yavsc.Helpers
|
||||||
@model Yavsc.ViewModels.Gen.PdfGenerationViewModel
|
@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");
|
Model.GenerateEstimatePdf();
|
||||||
using (StreamWriter sw = new StreamWriter (fi.FullName))
|
|
||||||
{
|
|
||||||
sw.Write (Model.TeXSource);
|
|
||||||
}
|
}
|
||||||
if (!fi.Exists)
|
@if (Model.Generated) {
|
||||||
{
|
<div>
|
||||||
errorMsg = "Source write failed";
|
@(Model.BaseFileName).pdf was generated
|
||||||
}
|
</div>
|
||||||
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 {
|
} else {
|
||||||
@errorMsg
|
<div class="error">
|
||||||
<text>Something went wrong ...</text>
|
@Model.GenerationErrorMessage
|
||||||
|
<p>Something went wrong ...</p>
|
||||||
|
</div>
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue