0
I have an Asp.Net MVC project where there is a method that returns a PartialView
:
[HttpPost]
public ActionResult MinhaAction(int param1, int param2)
{
// ...
// Código
// ...
MigraDoc.DocumentObjectModel.Document doc = new MigraDoc.DocumentObjectModel.Document();
DocumentRenderer renderer = new DocumentRenderer(doc);
PdfDocumentRenderer pdfRenderer = new PdfDocumentRenderer();
pdfRenderer.PdfDocument = outPdf;
pdfRenderer.DocumentRenderer = renderer;
MemoryStream ms = new MemoryStream();
pdfRenderer.Save(ms, false);
byte[] buffer = new byte[ms.Length];
ms.Seek(0, SeekOrigin.Begin);
ms.Flush();
ms.Read(buffer, 0, (int)ms.Length);
PdfFormatOptions pfo = ExportOptions.CreatePdfFormatOptions();
pfo.CreateBookmarksFromGroupTree = true; //habilita tree
return PartialView("PrintPdf", ms);
}
My Partialview PrintPdf
is like this:
@using myProject.Enumerator
@model Stream
@{
var file = Model.ToArray();
Response.Clear();
Response.ClearContent();
Response.AddHeader("Content-Length", file.Length.ToString());
Response.AddHeader("Content-Disopsition", "filename=" + ViewBag.FileName);
Response.ContentType = "application/pdf";
Response.BinaryWrite(file.ToArray());
}
As you can see, I am wanting to return a PDF to the View where I have the following Ajax:
<div id="conteudo-pdf" style="display:none;width:790px;height:590px;"></div>
// ...
$.ajax({
beforeSend: function () {
LoadStart();
},
complete: function () {
LoadStop();
},
contentType: 'application/json, charset=utf-8',
dataType: 'json',
type: 'POST',
url: '@Url.Action("MinhaAction", "MeuController")',
data: JSON.stringify(
{
param1: 1,
param2: 2
}),
success: function (data) {
$("#conteudo-pdf").empty();
ShowDialog("Title", "#conteudo-pdf", "1024", "660");
$("#conteudo-pdf").html(data);
},
timeout: 420000,
fail: function (jqXHR, textStatus) {
if (textStatus === 'timeout') {
alert('Failed from timeout');
}
alert("Error 007")
}
});
// ...
function ShowDialog(title, idDiv, largura, altura) {
$(idDiv).dialog({
title: title,
resizable: false,
width: largura,
height: altura,
modal: true,
position: 'center',
buttons: [{
text: Resources.Fechar,
click: function () {
$(idDiv).attr("title", "");
$(this).dialog("close");
}
}]
}).css("font-size", "10px").fadeIn("slow");
ValidaForm();
}
Unfortunately it has not worked, because the variable data
always comes null
. I’ve tried to send the pdf
as base64
, but by having many graphic details, ends up crashing the browser. I need something light and simple but that keeps my Action
as POST
.
You want to return the PDF to a
div
or want to open it on a page?– Randrade
The goal was to display him in
div
, but just downloading would save my skin! The problem is that I have a methodPOST
and I don’t have a physical file, but aMemoryStream
...– Jedaias Rodrigues
But why does it have to be
POST
? You cannot open this PDF with a normal link?– Randrade
It has to be POST because I’m generating a report that can send many parameters, and besides a very large URL does not work, it gets visually ugly...
– Jedaias Rodrigues
None of the reasons make sense, but come on. Have you already put a breakpoint in Action to see if Ajax is passing the parameters correctly? She comes back to
PartialView
? Remove thePOST
and open the URL normally to test if it is working.– Randrade