Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of #39 (Pt 3) #128

Merged
merged 40 commits into from
Jul 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a5faae3
Added DocumentController
Jun 13, 2021
53b7b4e
sending post request to server with axios
gandompm Jun 15, 2021
2f7b691
Merge pull request #121 from amosproj/dev
gandompm Jun 16, 2021
d2646a6
Replacing images in docx and return docx via request response
Jun 19, 2021
21eb0f4
Receiving image data from the frontend
Jun 19, 2021
aaeac34
sending multiple images with axios
gandompm Jun 20, 2021
1d3b13c
changed exportPDF url to CreateReport
Jun 21, 2021
e2472c3
Added Report request response including Report
Jun 21, 2021
ff61dd8
recieving the docx file from server
gandompm Jun 22, 2021
06827a5
renamed res response
Jun 22, 2021
8e72cd5
Resolved the CORS Error, recieving the calculation data from backend
SaiVarunVaranasi Jun 9, 2021
983602c
Resolved the CORS Error, recieving the calculation data from backend
SaiVarunVaranasi Jun 9, 2021
d9c403e
WIP
Jun 22, 2021
b3de555
WIP
Jun 22, 2021
a3b8425
Added response file download
Jun 22, 2021
cab6306
Trying to find the right headers for data transfer
Jun 23, 2021
a8a9c75
Merge Dev-#39 Frontend Backend
Jun 23, 2021
5d9f10b
gitignore
Jun 24, 2021
fd583a7
exporting in frontend side
gandompm Jun 27, 2021
2032409
Merge branch 'dev-#39' of https://github.com/amosproj/amos-ss2021-car…
gandompm Jun 27, 2021
3ac13c4
move export Pdf function to a separate module
gandompm Jun 27, 2021
b28d301
defining the backend request to a separate module
gandompm Jun 27, 2021
afcc25c
documenting and refactoring ui
gandompm Jun 27, 2021
7fd1199
fixing shape of diagrams in pdf in compare page
gandompm Jun 29, 2021
d7a274f
Merge branch 'dev' into dev-#39
Waldleufer Jun 30, 2021
f9bc697
Merge Preparation
Waldleufer Jun 30, 2021
3c7e00a
Frontend Pipeline fixed.
Waldleufer Jun 30, 2021
9367d50
Backend pipeline fix
SaiVarunVaranasi Jun 30, 2021
b87eaaf
Fix the backend build errors
SaiVarunVaranasi Jun 30, 2021
cfa8b84
Merge branch 'dev-#39' of
SaiVarunVaranasi Jun 30, 2021
e5aa6b4
Changed docx template handling
Jul 1, 2021
59d3d03
Fixed download of file from backend
Jul 1, 2021
094dc7e
manual DetailsComponent Merge from Main
Jul 1, 2021
dbbac37
adding loading bar for exporting pdf
gandompm Jul 2, 2021
5f780c8
resoliving CI Pipe line errors
gandompm Jul 2, 2021
946bcc2
deleting frontend pdf generater function
gandompm Jul 2, 2021
025bd16
fixing the exception
gandompm Jul 5, 2021
7b186d7
fixing ui
gandompm Jul 5, 2021
26cbe50
Merge branch 'dev' into dev-#39 - Merge Preparation
Waldleufer Jul 5, 2021
81f0227
Merge branch 'dev-#39' of https://github.com/amosproj/amos-ss2021-car…
Waldleufer Jul 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backend/Backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

secrets.json
Templates/template-docx
Templates/template-modified.docx
Templates/jpegsample.jpg

# User-specific files
*.rsuser
Expand Down
6 changes: 6 additions & 0 deletions backend/Backend/Backend.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

<ItemGroup>
<PackageReference Include="AspNetCore.Proxy" Version="4.2.0" />
<PackageReference Include="CoreCompat.libgdiplus" Version="6.0.4-ci-84" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0-preview.5.21301.5" />
</ItemGroup>

<ItemGroup>
Expand All @@ -17,5 +19,9 @@
</Content>
</ItemGroup>

<ItemGroup>
<Folder Include="Templates\" />
</ItemGroup>

</Project>

61 changes: 61 additions & 0 deletions backend/Backend/Controllers/DocumentController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Backend.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Web;
namespace Backend.Controllers
{
[Route("[controller]")]
[ApiController]
public class DocumentController : ControllerBase
{
private readonly DocumentService _documentService;

public DocumentController(DocumentService documentService)
{
_documentService = documentService;
}

//[HttpPost("test")]
//public IActionResult test()
//{
// var files = DocumentService.GetFileModelsFromRequest(this.Request);

// return new OkResult();
//}

[HttpPost("CreateReport")]
public FileContentResult CreateReport()
{
string filepath = _documentService.createReport(this.Request);

string filename = "template-modified.docx";
byte[] filedata = System.IO.File.ReadAllBytes(filepath);
string contentType;
new FileExtensionContentTypeProvider().TryGetContentType(filepath, out contentType);

var cd = new System.Net.Mime.ContentDisposition {
FileName = filename,
Inline = false
};

System.IO.File.Delete(filepath);
Response.Headers.Add("Content-Disposition", cd.ToString());
Response.Headers.Add("Access-Control-Expose-Headers", "*");
//Response.Headers.Add("Content-Transfer-Encoding", "Binary");
//Response.Headers.Add("Content-Encoding", "gzip");
return File(filedata, contentType);
}

[HttpOptions("CreateReport")]
public IActionResult PreflightRoute()
{
return NoContent();
}

}
}
6 changes: 6 additions & 0 deletions backend/Backend/Controllers/SimaProController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,11 @@ public Task ProxyCatchAll(string rest)
return this.HttpProxyAsync($"{rest}{queryString}", httpProxyOptions);
}

[HttpOptions]
public IActionResult PreflightRoute()
{
return NoContent();
}

}
}
56 changes: 56 additions & 0 deletions backend/Backend/Helper/XmlToDocxHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.IO;
using System.IO.Compression;
using System.Xml.Linq;

namespace Backend.Helper
{
public static class XmlToDocxHelper
{

public static void ZipDOCX(this string unzipPath, string fileName)
{

if (File.Exists(fileName))
{
File.Delete(fileName);
}

ZipFile.CreateFromDirectory(unzipPath, fileName);

}

public static string UnzipDOCX(this string zipPath)
{
string[] subs = zipPath.Split('.', StringSplitOptions.RemoveEmptyEntries);

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
if (Directory.Exists(subs[0]))
{
Directory.Delete(subs[0], true);
}

archive.ExtractToDirectory(subs[0]);

}

return subs[0];
}

//public static string XmlToString(this string xmlDirectory, string xmlPath)
//{
// XDocument document = XDocument.Load(xmlDirectory + xmlPath);
// string docString = document.ToString();

// return docString;
//}

//public static void StringToXml(this string xmlDirectory, string xmlPath, string documentString)
//{
// XDocument xDocument = XDocument.Parse(documentString, LoadOptions.PreserveWhitespace);
// xDocument.Save(xmlDirectory + xmlPath);
//}

}
}
116 changes: 116 additions & 0 deletions backend/Backend/Services/DocumentService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Drawing;
using System.IO;
using System.ComponentModel;
using Backend.Helper;
using Microsoft.AspNetCore.Http;

namespace Backend.Services
{
public class DocumentService
{
public string createReport(HttpRequest request)
{
var imageNumbers = new List<int>() { 7, 14, 11 };

GetFileModelsFromRequest(request, imageNumbers);

var unzipPath = @"Templates\template-docx.docx".UnzipDOCX();
var docxImagePath = unzipPath + @"\word\media\";


foreach (var number in imageNumbers)
{
File.Delete(docxImagePath + "image" + number.ToString() + ".png");
var image = Image.FromFile(@"Templates\image" + number.ToString() + ".png");
image.Save(docxImagePath + "image" + number.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);
image.Dispose();
File.Delete(@"Templates\image" + number.ToString() + ".png");
}

unzipPath.ZipDOCX(@"Templates\template-modified.docx");
Directory.Delete(@"Templates\template-docx",true);
return @"Templates\template-modified.docx";

//List<Image> images = new List<Image>() {
// Image.FromFile(@"Templates\image7.png"),
// Image.FromFile(@"Templates\image11.png"),
// Image.FromFile(@"Templates\image14.png")
//};

//File.Delete(imagePath + "image7.png");
//File.Delete(imagePath + "image11.png");
//File.Delete(imagePath + "image14.png");

//images[0].Save(imagePath + "image" + "7.png", System.Drawing.Imaging.ImageFormat.Png);
//images[1].Save(imagePath + "image" + "11.png", System.Drawing.Imaging.ImageFormat.Png);
//images[2].Save(imagePath + "image" + "14.png", System.Drawing.Imaging.ImageFormat.Png);


//File.Delete(@"Templates\image7.png");
//File.Delete(@"Templates\image11.png");
//File.Delete(@"Templates\image14.png");




}

public static void GetFileModelsFromRequest(HttpRequest request, List<int> imageNumbers)
{
foreach (var formField in request.Form)
{
// Form data
var stringValues = formField.Value.Select(x => x.ToString()).Select(y => y.Replace("data:image/jpeg;base64,",""));
var stringValueTuples = stringValues.Zip(imageNumbers);

foreach (var tuple in stringValueTuples)
{
//... process and add to the FileModel list
byte[] bytes = Convert.FromBase64String(tuple.First);

Image image;
using (MemoryStream ms = new MemoryStream(bytes))
{
image = Image.FromStream(ms);
image.Save(@"Templates\" + "image" + tuple.Second.ToString() +".png", System.Drawing.Imaging.ImageFormat.Png);
image.Dispose();
ms.Dispose();
}
}
}
}

//XDocument template = XDocument.Load(@"Templates\template-xml.xml");
//string document = template.ToString();

//var documentModified = ReplaceXmlInlineImageDate(document, @"Templates\jpegsample.jpg", "7");
//documentModified = ReplaceXmlInlineImageDate(documentModified, @"Templates\jpegsample.jpg", "11");
//documentModified = ReplaceXmlInlineImageDate(documentModified, @"Templates\jpegsample.jpg", "14");

//XDocument xDocument = XDocument.Parse(documentModified, LoadOptions.PreserveWhitespace);
//xDocument.Save(@"Templates\template-xml-edited.docx");

//private static string ReplaceXmlInlineImageDate(string document, string imagePath, string imageToBeReplacedNumber)
//{
// //load image from filesystem
// Image image = Image.FromFile(imagePath);
// Bitmap bitmap = new Bitmap(image);
// TypeConverter converter = TypeDescriptor.GetConverter(typeof(Bitmap));
// string imgContent = Convert.ToBase64String((byte[])converter.ConvertTo(bitmap, typeof(byte[])));

// //build document string
// string delimiterOpeningTag = "<pkg:part pkg:name=\"/word/media/image"+imageToBeReplacedNumber+".png\" pkg:contentType=\"image/png\" pkg:compression=\"store\">\r\n <pkg:binaryData>";
// string delimiterClosingTag = "</pkg:binaryData>\r\n </pkg:part>\r\n";
// var documentArray = document.Split(delimiterOpeningTag);
// var documentArray2 = documentArray[1].Split(delimiterClosingTag);
// var restString = String.Join(delimiterClosingTag, documentArray2, 1, documentArray2.Length - 1);
// var documentModified = documentArray[0] + delimiterOpeningTag + imgContent + delimiterClosingTag + restString;
// return documentModified;
//}
}
}
4 changes: 4 additions & 0 deletions backend/Backend/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using AspNetCore.Proxy;
using Backend.Security;
using Backend.Middleware;
using Backend.Services;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
Expand Down Expand Up @@ -49,6 +50,9 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddProxies();

services.AddTransient<DocumentService>();

//Used to get the authenticate/process the Http requests.
services.AddTransient<SimaProLoginDelegatingHandler>();

Expand Down
Binary file added backend/Backend/Templates/jpegsample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added backend/Backend/Templates/template-docx.docx
Binary file not shown.
41 changes: 18 additions & 23 deletions frontend/src/components/details/DetailsComponent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import PropTypes from 'prop-types';
import React, { Component } from 'react';
import ScenarioComponent from './ScenarioComponent';
import { jsPDF } from 'jspdf';
import html2canvas from 'html2canvas';
import { Col, Container, Row } from 'react-grid-system';
import './navbar.css';
import { postCalculationRequest } from 'interface/BackendConnect';
import LoadingComponent from 'components/loading';
import { exportPdf } from '../../interface/PdfExporter.js';

/**
* the main component for detail page which includes
* canvas page and variable drop down list
Expand All @@ -29,7 +29,8 @@ class DetailsComponent extends Component {
baselineScenario: true,
modifiedScenario: false,
loadComparePage: false,
stillLoading: true
stillLoading: true,
onExportClicked: false
};

/**
Expand Down Expand Up @@ -93,26 +94,18 @@ class DetailsComponent extends Component {
};

let handleExportPdfButton = () => {
// getting the element that should be exported
var div = document.getElementById('capture');
this.setState({ onExportClicked: true });

// converting html to an image and then exporting it by pdf
html2canvas(div).then((canvas) => {
var imgData = canvas.toDataURL('image/jpeg', 1);
// pdf configuration
var pdf = new jsPDF('p', 'mm', 'a4');
var pageWidth = pdf.internal.pageSize.getWidth();
var pageHeight = pdf.internal.pageSize.getHeight();
var imageWidth = canvas.width;
var imageHeight = canvas.height;
// geting the element that should be exported
var div1 = document.getElementById('capturePieChart');
var div2 = document.getElementById('captureColumnDiagram');
var div3 = document.getElementById('captureTable');

var ratio =
imageWidth / imageHeight >= pageWidth / pageHeight
? pageWidth / imageWidth
: pageHeight / imageHeight;
pdf.addImage(imgData, 'JPEG', 0, 0, imageWidth * ratio, imageHeight * ratio);
pdf.save('invoice.pdf');
});
exportPdf(div1, div2, div3, pdfExportDoneCallback);
};

let pdfExportDoneCallback = () => {
this.setState({ onExportClicked: false });
};
/*
* Important function that is given as the callback parameter to the postCalculationRequest in order to be called
Expand Down Expand Up @@ -144,10 +137,11 @@ class DetailsComponent extends Component {
<ScenarioComponent
loadComparePage={this.state.loadComparePage}
onCompareClick={handleCompareButton}
onExportClick={handleExportPdfButton}
exportHandler={handleExportPdfButton}
scenarioName={scenarioNames.baseline}
selectedScenarioType={this.state.selectedScenarioType}
selectedProduct={selectedProduct}
onExportClicked={this.state.onExportClicked}
newScenarioHandler={handleNewScenarioSelection}
/>
</Col>
Expand All @@ -163,10 +157,11 @@ class DetailsComponent extends Component {
<ScenarioComponent
loadComparePage={this.state.loadComparePage}
onCompareClick={handleCompareButton}
onExportClick={handleExportPdfButton}
exportHandler={handleExportPdfButton}
scenarioName={scenarioNames.modified}
selectedScenarioType={this.state.selectedScenarioType}
selectedProduct={selectedProduct}
onExportClicked={this.state.onExportClicked}
newScenarioHandler={handleNewScenarioSelection}
/>
</Col>
Expand Down
Loading