ho potuto scrivere finalmente il pdf al sistema di directory modificando il codice di base del metodo render RazorPDF. Il metodo Rendor crea un oggetto PDFWriter associato al flusso di risposta:
// Associate output with response stream
var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
pdfWriter.CloseStream = false;
la soluzione era creare un altro oggetto PDFWriter che è stato associato ad un oggetto FileStream come illustrato di seguito:
// Create the pdf file in the directory system
var fileStream = new FileStream(myPdfFilePath, FileMode.Create);
var pdfWriter2 = PdfWriter.GetInstance(document, fileStream);
Poi chiusi gli oggetti:
fileStream.Close();
pdfWriter.Close();
pdfWriter2.Close();
ho dovuto incorporare essenzialmente le classi PdfResult e PdfView di RazorPDF nel mio progetto e significativamente alterare la codice. Il motivo è perché ho anche dovuto includere le chiamate in una classe email che ha inviato il pdf a un utente.
Il metodo completo di rendering viene visualizzato sotto:
public void Render(ViewContext viewContext, TextWriter writer)
{
// generate view into string
var sb = new System.Text.StringBuilder();
TextWriter tw = new System.IO.StringWriter(sb);
myResult.View.Render(viewContext, tw);
var resultCache = sb.ToString();
// detect itext (or html) format of response
XmlParser parser;
using (var reader = GetXmlReader(resultCache))
{
while (reader.Read() && reader.NodeType != XmlNodeType.Element)
{
// no-op
}
if (reader.NodeType == XmlNodeType.Element && reader.Name == "itext")
parser = new XmlParser();
else
parser = new HtmlParser();
}
// Create a document processing context
var document = new Document();
document.Open();
// Associate output with response stream
var pdfWriter = PdfWriter.GetInstance(document, viewContext.HttpContext.Response.OutputStream);
pdfWriter.CloseStream = false;
// Create the pdf file in the directory system
var fileStream = new FileStream(myPdfFilePath, FileMode.Create);
var pdfWriter2 = PdfWriter.GetInstance(document, fileStream);
// this is as close as we can get to being "success" before writing output
// so set the content type now
viewContext.HttpContext.Response.ContentType = "application/pdf";
// parse memory through document into output
using (var reader = GetXmlReader(resultCache))
{
parser.Go(document, reader);
}
fileStream.Close();
// Send an email to the claimant
Thread.Sleep(100);
if (File.Exists(myPdfFilePath))
{
var subject = "PDF Documents";
var body = Config.GetContent(ContentParams.CLAIM_DOCUMENT_EMAIL_BODY_TEXT);
bool success;
string errorMessage;
Email.Send(myEmailAddress, subject, body, out success, out errorMessage, myPdfFilePath);
}
pdfWriter.Close();
pdfWriter2.Close();
}
Sarebbe bello se questa capacità fosse in qualche modo incorporato nel progetto RazorPDF corrente.
fonte
2013-08-30 17:45:58
Hai risolto questo? Sto avendo lo stesso problema. – Gmorken
Ho ancora lo stesso problema. Nessuna soluzione ancora. –
Hai visto questo post http://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-mvc? Dovrebbe essere possibile aggiungere quell'intestazione 'Content-Disposition' prima di chiamare' PdfResult() '- se funziona o meno rimane da vedere. –