2013-08-09 5 views
5

Attualmente sto assemblando e visualizzando un PDF utilizzando RazorPDF in MVC4 e desidero salvare il file PDF nel file system mentre restituisco la vista.RazorPDF salva il file pdf nella directory del server in MVC4

La seguente riga di codice nella azione di controllo sta chiamando la vista:

return new PdfResult(claims, "PDF"); 
+1

Hai risolto questo? Sto avendo lo stesso problema. – Gmorken

+0

Ho ancora lo stesso problema. Nessuna soluzione ancora. –

+0

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. –

risposta

4

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.

+0

Funziona alla grande. Buon lavoro – Gmorken

+0

Puoi anche accettare la tua risposta come risposta. Sembra rispondere a me. –

1

perché non solo ottenere lo streaming tramite una richiesta web per l'url?

string razorPdfUrl="http://..."; 
var req = HttpWebRequest.Create(RazorPDFURL); 
using (Stream pdfStream = req.GetResponse().GetResponseStream()) 
{ 
    ... 
} 
+0

Questo non fornisce una risposta alla domanda. Per criticare o richiedere chiarimenti da un autore, lascia un commento sotto il loro post - puoi sempre commentare i tuoi post e, una volta che hai una reputazione sufficiente, potrai commentare qualsiasi post. – Bruce