diff --git a/web/Controllers/FrontOfficeApiController.cs b/web/Controllers/FrontOfficeApiController.cs index a5d93406..72b78246 100644 --- a/web/Controllers/FrontOfficeApiController.cs +++ b/web/Controllers/FrontOfficeApiController.cs @@ -89,11 +89,11 @@ namespace Yavsc.ApiControllers /// The estim tex. /// Estimid. [AcceptVerbs ("GET")] - public HttpResponseMessage GetEstimTex (long estimid) + public HttpResponseMessage EstimateToTex (long estimid) { string texest = null; try { - texest = getEstimTex (estimid); + texest = estimateToTex (estimid); } catch (TemplateException ex) { return new HttpResponseMessage (HttpStatusCode.OK) { Content = new ObjectContent (typeof(string), @@ -102,16 +102,17 @@ namespace Yavsc.ApiControllers )) }; } catch (Exception ex) { - return new HttpResponseMessage (HttpStatusCode.OK) { Content = new ObjectContent (typeof(string), - ex.Message, new SimpleFormatter ("text/text")) + ex.Message, new ErrorHtmlFormatter (HttpStatusCode.InternalServerError, + LocalizedText.DocTemplateException)) }; } if (texest == null) return new HttpResponseMessage (HttpStatusCode.OK) { Content = - new ObjectContent (typeof(string), - "Not an estimation id:" + estimid, new SimpleFormatter ("text/text")) + new ObjectContent (typeof(string), "Not an estimation id:" + estimid, + new ErrorHtmlFormatter (HttpStatusCode.NotFound, + LocalizedText.Estimate_not_found)) }; return new HttpResponseMessage () { @@ -121,7 +122,7 @@ namespace Yavsc.ApiControllers }; } - private string getEstimTex (long estimid) + private string estimateToTex (long estimid) { Yavsc.templates.Estim tmpe = new Yavsc.templates.Estim (); Estimate e = wfmgr.GetEstimate (estimid); @@ -141,22 +142,42 @@ namespace Yavsc.ApiControllers return tmpe.TransformText (); } - /// /// Gets the estimate in pdf format from tex generation. /// - /// The estim pdf. + /// The to pdf. /// Estimid. - public HttpResponseMessage GetEstimPdf (long estimid) + [AcceptVerbs("GET")] + public HttpResponseMessage EstimateToPdf (long estimid) { - Estimate estim = wfmgr.GetEstimate (estimid); - //TODO better with pro.IsBankable && cli.IsBillable + string texest = null; + try { + texest = estimateToTex (estimid); + } catch (TemplateException ex) { + return new HttpResponseMessage (HttpStatusCode.OK) { Content = + new ObjectContent (typeof(string), + ex.Message, new ErrorHtmlFormatter (HttpStatusCode.NotAcceptable, + LocalizedText.DocTemplateException + )) + }; + } catch (Exception ex) { + return new HttpResponseMessage (HttpStatusCode.OK) { Content = + new ObjectContent (typeof(string), + ex.Message, new ErrorHtmlFormatter (HttpStatusCode.InternalServerError, + LocalizedText.DocTemplateException)) + }; + } + if (texest == null) + return new HttpResponseMessage (HttpStatusCode.OK) { Content = + new ObjectContent (typeof(string), "Not an estimation id:" + estimid, + new ErrorHtmlFormatter (HttpStatusCode.NotFound, + LocalizedText.Estimate_not_found)) + }; return new HttpResponseMessage () { - Content = new ObjectContent ( - typeof(Estimate), - estim, - new EstimToPdfFormatter ()) + Content = new ObjectContent (typeof(string), + texest, + new TexToPdfFormatter ()) }; } } diff --git a/web/Formatters/TexToPdfFormatter.cs b/web/Formatters/TexToPdfFormatter.cs new file mode 100644 index 00000000..71c18733 --- /dev/null +++ b/web/Formatters/TexToPdfFormatter.cs @@ -0,0 +1,110 @@ +// +// TexToPdfFormatter.cs +// +// Author: +// Paul Schneider +// +// Copyright (c) 2015 Paul Schneider +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this program. If not, see . +using System; +using System.Net.Http.Formatting; +using System.Net.Http.Headers; +using System.Collections.Generic; +using System.IO; +using System.Web; +using System.Diagnostics; + +namespace Yavsc.Formatters +{ + public class TexToPdfFormatter: BufferedMediaTypeFormatter + { + /// + /// Initializes a new instance of the class. + /// + public TexToPdfFormatter () + { + SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/pdf")); + } + /// + /// Determines whether this instance can read type the specified type. + /// + /// true if this instance can read type the specified type; otherwise, false. + /// Type. + public override bool CanReadType(Type type) + { + return false; + } + /// + /// Determines whether this instance can write type the specified type. + /// + /// true if this instance can write type the specified type; otherwise, false. + /// Type. + public override bool CanWriteType(System.Type type) + { + if (type == typeof(string)) + { + return true; + } + else + { + Type enumerableType = typeof(IEnumerable); + return enumerableType.IsAssignableFrom(type); + } + } + /// + /// Writes to stream. + /// + /// Type. + /// Value. + /// Stream. + /// Content headers. + public override void WriteToStream (Type type, object value, Stream stream, HttpContentHeaders contentHeaders) + { + + string content = value as string; + string name = "tmpdoc-"+Guid.NewGuid().ToString(); + string fullname = Path.Combine ( + HttpRuntime.CodegenDir, name); + FileInfo fi = new FileInfo(fullname + ".tex"); + FileInfo fo = new FileInfo(fullname + ".pdf"); + using (StreamWriter sw = new StreamWriter (fi.FullName)) + { + sw.Write (content); + } + using (Process p = new Process ()) { + p.StartInfo.WorkingDirectory = HttpRuntime.CodegenDir; + p.StartInfo = new ProcessStartInfo (); + p.StartInfo.UseShellExecute = false; + p.StartInfo.FileName = "/usr/bin/texi2pdf"; + p.StartInfo.Arguments = + string.Format ("--batch --build-dir={2} -o {0} {1}", + fo.FullName, + fi.FullName,HttpRuntime.CodegenDir); + p.Start (); + p.WaitForExit (); + if (p.ExitCode != 0) + throw new Exception ("Pdf generation failed with exit code:" + p.ExitCode); + } + + using (StreamReader sr = new StreamReader (fo.FullName)) { + byte[] buffer = File.ReadAllBytes (fo.FullName); + stream.Write(buffer,0,buffer.Length); + } + fi.Delete(); + fo.Delete(); + } + + } +} diff --git a/web/Views/FrontOffice/Estimate.aspx b/web/Views/FrontOffice/Estimate.aspx index c75ea64a..4f36b737 100644 --- a/web/Views/FrontOffice/Estimate.aspx +++ b/web/Views/FrontOffice/Estimate.aspx @@ -54,6 +54,11 @@ <% } %> <% } %> + + @@ -241,10 +246,6 @@ function addRow(){ -
- /FrontOffice/GetEstimTex?estimid=<%=Model.Id%>"><%= LocalizedText.Tex_version %> - /FrontOffice/GetEstimPdf?estimid=<%=Model.Id%>"><%= LocalizedText.Pdf_version %> -
diff --git a/web/Web.csproj b/web/Web.csproj index e94258a3..f5fa30b7 100644 --- a/web/Web.csproj +++ b/web/Web.csproj @@ -184,6 +184,7 @@ + diff --git a/yavscModel/LocalizedText.Designer.cs b/yavscModel/LocalizedText.Designer.cs index 5b1cebf9..95e1904a 100644 --- a/yavscModel/LocalizedText.Designer.cs +++ b/yavscModel/LocalizedText.Designer.cs @@ -160,6 +160,12 @@ namespace Yavsc.Model { } } + public static string Estimate_not_found { + get { + return ResourceManager.GetString("Estimate_not_found", resourceCulture); + } + } + public static string Online { get { return ResourceManager.GetString("Online", resourceCulture); diff --git a/yavscModel/LocalizedText.fr.resx b/yavscModel/LocalizedText.fr.resx index 70b98371..e96a510e 100644 --- a/yavscModel/LocalizedText.fr.resx +++ b/yavscModel/LocalizedText.fr.resx @@ -46,4 +46,5 @@ Vous n'êtes pas administrateur Rôle créé Article ajouté au panier + Devis non trouvé diff --git a/yavscModel/LocalizedText.resx b/yavscModel/LocalizedText.resx index 6a3537a1..52cd3306 100644 --- a/yavscModel/LocalizedText.resx +++ b/yavscModel/LocalizedText.resx @@ -46,4 +46,5 @@ You're not administrator role created Item added to basket + Estimate not found