The below code is initiated from my "export" li tag, which changes a FusionChart to an SVG string and then passes it to my controller via an ajax call.
The problem I have is that it hits the controller, runs through the code right till the end and "Success" is displayed by the alert, but no popup is displayed to save the pdf ?
I have grabbed the entire HTML string and put into notepad, saved as html and everything displays as it should, but simply will not work when running from the controller and popping up the save message...
Am I missing something ? or doing something wrong, or anything that is not best practice ?
I have the following that is calling a javascript function:
<li id="export" style="font-size:13px"><a onclick="svgString()">Rotativa Export</a></li>
Here is my javascript that is making the AJAX call:
function svgString() {
var ChartSVG = new Array(10);
var text = document.createTextNode(savingsChart.getSVGString());
document.getElementById("msg").appendChild(text);
ChartSVG = chunkSubstr(document.getElementById("msg").innerHTML, 10);
var Details =
{
"fkiProjectID" : @Model.fkiProjectID,
"ChartSVG": ChartSVG
};
$.ajax({
url: '/Profile/ExportCombinedPDF',
data: JSON.stringify(Details),
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert("Success : " + data);
},
error: function(data){
alert("Error: " + data);
}
});
}
document.getElementById("export").addEventListener("click", svgString);
function chunkSubstr(str, size) {
var numChunks = Math.ceil(str.length / size),
chunks = new Array(size);
//alert(str.length);
for(var i = 0, o = 0; i < size; ++i, o += numChunks) {
chunks[i] = str.substring(o, o + numChunks);
//alert(o + " " + numChunks);
}
return chunks;
}
Controller:
public FileStreamResult ExportCombinedPDF(CalculatedOutputViewModel CVM)
{
List<PipelineDetails> PipeList = new List<PipelineDetails>();
ProjectManager PM = new ProjectManager();
PipeList = PM.GetPipelineList(CVM.fkiProjectID);
string webgridstyle = PM.Pipeline_HtmlForExport(CVM.fkiProjectID);
string ProjectHtml = PM.Project_HtmlForExport(CVM.fkiProjectID);
string chartcontent = ExportRotativaPDF2(CVM);
WebGrid grid = new WebGrid(source: PipeList, canPage: false, canSort: false);
string gridHtml = grid.GetHtml(tableStyle: "webGrid",
headerStyle: "webGridHeader",
alternatingRowStyle: "webGridAlt",
columns: grid.Columns(
grid.Column("NodeNumber", "Node Nr."),
grid.Column("Accumulated_Length", "Accumulated Length"),
grid.Column("Elevation", "Elevation"),
grid.Column("Pipe_Outside_Diameter", "Pipe Outside Diameter"),
grid.Column("Wall_Thickness", "Wall Thickness"),
grid.Column("Control_Point_Description", "Control Point Description"),
grid.Column("Control_Point_Size", "Control Point Size"))).ToString();
string exportData = String.Format("<html><body>{0}{1} <p style='page-break-after:always;'></p> {2} {3}</body></html>", "<style>" + webgridstyle + "</style>", ProjectHtml, gridHtml, chartcontent);
var bytes = System.Text.Encoding.UTF8.GetBytes(exportData);
using (var input = new MemoryStream(bytes))
{
var output = new MemoryStream();
var document = new iTextSharp.text.Document(PageSize.A4, 50, 50, 50, 50);
var writer = PdfWriter.GetInstance(document, output);
PdfPTable table = new PdfPTable(1);
table.HeaderRows = 1;
Font headerFont = FontFactory.GetFont("Verdana", 10);
Font rowfont = FontFactory.GetFont("Verdana", 10);
writer.CloseStream = false;
document.Open();
var xmlWorker = iTextSharp.tool.xml.XMLWorkerHelper.GetInstance();
xmlWorker.ParseXHtml(writer, document, input, System.Text.Encoding.UTF8);
document.Add(table);
document.Close();
output.Position = 0;
return File(output, "application/pdf", "Pipeline_Report.pdf");
//return new FileStreamResult(output, "application/pdf");
}
}
If anyone is able to help, Thanks!