2012-04-24 14 views
6

क्या किसी भी एचटीएमएल फाइल से wkhtmltopdf द्वारा बनाई गई पीडीएफ स्ट्रीम प्राप्त करना संभव है और आईई/फ़ायरफ़ॉक्स/क्रोम इत्यादि में एक डाउनलोड संवाद पॉप अप करना संभव है?wkhtmltopdf आउटपुटस्ट्रीम और डाउनलोड - डायग्लॉग

फिलहाल मैं इस कोड के आधार पर अपना outputstream मिलती है:

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); 
     } 
    } 
} 

तब मैं संवाद प्रदर्शित करना चाहते हैं:

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(); 
एक PDF फ़ाइल इस ठीक काम करता है से बनाए गए MemoryStreams साथ

, लेकिन यहाँ मुझे केवल एक खाली पृष्ठ मिलता है। बाइटियर में 1270 बाइट्स हैं।

+0

आप इस समस्या को हल किया? क्या मेरा समाधान मदद करता है? – Nenotlep

उत्तर

3

क्या यह अभी भी एक समस्या है?

मैंने अभी अपने कंप्यूटर पर wkhtmltopdf 0.11.0 आरसी 2 स्थापित करने के बाद एक नई एएसपीनेट वेबसाइट बनाई है और यह पीडीएफ बनाने में ठीक काम करता है। मेरा संस्करण केवल थोड़ा अलग था;

मेरी CSHTML मैं में था:

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(); 

मेरे 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

इसलिए यदि आपको आउटपुट में समस्याएं हैं, तो हो सकता है कि आप यह जांचना चाहें कि कनवर्टर को स्रोत HTML कैसे प्राप्त होता है यदि इसमें कोई समस्या है। – Nenotlep

+0

+ "- -" क्या है; आपके तर्क के अंत में? –

+1

@Mvision मैंने इसे बेवकूफ़ बना दिया है, लेकिन इसका मतलब है कि इनपुट और आउटपुट के लिए फ़ाइलों की बजाय, मैं इनपुट और आउटपुट के रूप में स्ट्रीम का उपयोग करूंगा। उदाहरण के लिए, फ़ाइल या यूआरएल के बजाय एसटीडीआईएन से इनपुट प्राप्त करने के लिए wkhtmltopdf प्राप्त करने के लिए और एक फ़ाइल में आउटपुट प्राप्त करने के लिए आप 'wkhtmltopdf.exe - output.pdf' का उपयोग करेंगे। इसी तरह एक इनपुट फ़ाइल और आउटपुट का उपयोग STDOUT (या शायद stderr, याद नहीं कर सकते) का उपयोग करने के लिए आप 'wkhtmltopdf.exe input.html -' का उपयोग करेंगे – Nenotlep

संबंधित मुद्दे