2012-04-24 19 views
6

è possibile ottenere un flusso pdf creato da wkhtmltopdf da qualsiasi file html e aprire una finestra di dialogo di download in IE/Firefox/Chrome, ecc.?wkhtmltopdf outputstream & download - diaglog

Al momento ho il mio OutputStream da questo codice:

public class Printer 
{ 
    public static MemoryStream GeneratePdf(StreamReader Html, MemoryStream pdf, Size pageSize) 
    { 
     Process p; 
     StreamWriter stdin; 
     ProcessStartInfo psi = new ProcessStartInfo(); 

     psi.FileName = @"C:\PROGRA~1\WKHTML~1\wkhtmltopdf.exe"; 

     // run the conversion utility 
     psi.UseShellExecute = false; 
     psi.CreateNoWindow = true; 
     psi.RedirectStandardInput = true; 
     psi.RedirectStandardOutput = true; 
     psi.RedirectStandardError = true; 

     // note that we tell wkhtmltopdf to be quiet and not run scripts 
     psi.Arguments = "-q -n --disable-smart-shrinking " + (pageSize.IsEmpty ? "" : "--page-width " + pageSize.Width + "mm --page-height " + pageSize.Height + "mm") + " - -"; 

     p = Process.Start(psi); 

     try 
     { 
      stdin = p.StandardInput; 
      stdin.AutoFlush = true; 
      stdin.Write(Html.ReadToEnd()); 
      stdin.Dispose(); 

      CopyStream(p.StandardOutput.BaseStream, pdf); 
      p.StandardOutput.Close(); 
      pdf.Position = 0; 

      p.WaitForExit(10000); 

      return pdf; 
     } 
     catch 
     { 
      return null; 
     } 
     finally 
     { 
      p.Dispose(); 
     } 
    } 

    public static void CopyStream(Stream input, Stream output) 
    { 
     byte[] buffer = new byte[32768]; 
     int read; 
     while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      output.Write(buffer, 0, read); 
     } 
    } 
} 

Poi voglio visualizzare la finestra:

MemoryStream PDF = Printer.GeneratePdf(Rd, PDFStream, Size); 

byte[] byteArray1 = PDF.ToArray(); 
PDF.Flush(); 
PDF.Close(); 
Response.BufferOutput = true; 

Response.Clear(); 
Response.ClearHeaders(); 
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf"); 
Response.ContentType = "application/octet-stream"; 
Response.BinaryWrite(byteArray1); 
Response.End(); 

Con MemoryStreams creati da un file PDF questo funziona bene, ma qui Ho solo una pagina vuota. Il bytearray ha 1270 byte.

+0

hai fatto a risolvere questo problema? La mia soluzione è stata d'aiuto? – Nenotlep

risposta

3

È ancora un problema?

Ho appena creato un nuovo sito Web ASP.net per testarlo sul mio computer dopo aver installato wkhtmltopdf 0.11.0 rc2 e ha funzionato bene creando il PDF. La mia versione era solo leggermente diversa;

Nel mio CSHTML ho avuto:

MemoryStream PDFStream = new MemoryStream(); 
MemoryStream PDF = Derp.GeneratePdf(PDFStream); 
byte[] byteArray1 = PDF.ToArray(); 
PDF.Flush(); 
PDF.Close(); 
Response.BufferOutput = true; 
Response.Clear(); 
Response.ClearHeaders(); 
Response.AddHeader("Content-Disposition", "attachment; filename=Test.pdf"); 
Response.ContentType = "application/octet-stream"; 
Response.BinaryWrite(byteArray1); 
Response.End(); 

La mia classe Derp

public class Derp 
{ 
    public static MemoryStream GeneratePdf(MemoryStream pdf) 
    { 
     using (StreamReader Html = new StreamReader(@"Z:\HTMLPage.htm")) 
     { 
      Process p; 
      StreamWriter stdin; 
      ProcessStartInfo psi = new ProcessStartInfo(); 
      psi.FileName = @"C:\wkhtmltopdf\wkhtmltopdf.exe"; 
      psi.UseShellExecute = false; 
      psi.CreateNoWindow = true; 
      psi.RedirectStandardInput = true; 
      psi.RedirectStandardOutput = true; 
      psi.RedirectStandardError = true; 
      psi.Arguments = "-q -n --disable-smart-shrinking " + " - -"; 
      p = Process.Start(psi); 
      try 
      { 
       stdin = p.StandardInput; 
       stdin.AutoFlush = true; 
       stdin.Write(Html.ReadToEnd()); 
       stdin.Dispose(); 
       CopyStream(p.StandardOutput.BaseStream, pdf); 
       p.StandardOutput.Close(); 
       pdf.Position = 0; 
       p.WaitForExit(10000); 
       return pdf; 
      } 
      catch 
      { 
       return null; 
      } 
      finally 
      { 
       p.Dispose(); 
      } 
     } 
    } 

    public static void CopyStream(Stream input, Stream output) 
    { 
     byte[] buffer = new byte[32768]; 
     int read; 
     while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      output.Write(buffer, 0, read); 
     } 
    } 
} 
+0

Quindi, se si verificano problemi con l'output, è possibile controllare come si ottiene l'HTML di origine sul convertitore, se questo ha qualche problema. – Nenotlep

+0

Cos'è il + "- -"; per alla fine dei tuoi argomenti? –

+1

@Mvision L'ho concatenato stupidamente, ma significa che invece di file per l'input e l'output, userò gli stream come input e output. Ad esempio, per ottenere wkhtmltopdf per ottenere il proprio input da STDIN invece che da un file o da un URL e inviarlo a un file, si utilizzi 'wkhtmltopdf.exe - output.pdf'. Analogamente, per usare un file di input e inviarlo a STDOUT (o forse a stderr, non ricordo) si userà 'wkhtmltopdf.exe input.html -' – Nenotlep

Problemi correlati