2017-03-16 183 views
1

我在MVC中使用rdlc报告,在Visual Studio中运行时,打印操作完美工作,但发布到设置在同一台机器上的iis时,打印操作未发生。但是当我将报告作为pdf返回时,它就在那里,并且我可以使用javascript打印文件。但实际上我并不需要显示报告,但想从服务器上打印。感谢提前给予帮助托管到iis时无法访问打印机

public ActionResult GenerateOrder() 
    { 


     try 
     { 

      LocalReport report = new LocalReport(); 
      report.ReportPath = (Server.MapPath("~/Reports/Report1.rdlc")); 
      Export(report); 
      Print(); 
     } 
     catch (Exception Ex) 
     { 

     } 

     return View(); 
    } 

    private Stream CreateStream(string name, 
    string fileNameExtension, Encoding encoding, 
    string mimeType, bool willSeek) 
    { 
     Stream stream = new MemoryStream(); 
     m_streams.Add(stream); 
     return stream; 
    } 
    private void Export(LocalReport report) 
    { 
     string deviceInfo = 
      @"<DeviceInfo> 
      <OutputFormat>EMF</OutputFormat> 
      <PageWidth>8.5in</PageWidth> 
      <PageHeight>11in</PageHeight> 
      <MarginTop>0.25in</MarginTop> 
      <MarginLeft>0.25in</MarginLeft> 
      <MarginRight>0.25in</MarginRight> 
      <MarginBottom>0.25in</MarginBottom> 
     </DeviceInfo>"; 
     Warning[] warnings; 
     m_streams = new List<Stream>(); 
     report.Render("Image", deviceInfo, CreateStream, 
      out warnings); 
     foreach (Stream stream in m_streams) 
      stream.Position = 0; 
    } 
    private int m_currentPageIndex; 
    private IList<Stream> m_streams; 

    private void PrintPage(object sender, PrintPageEventArgs ev) 
    { 
     try 
     { 
      Metafile pageImage = new 
         Metafile(m_streams[m_currentPageIndex]); 

      // Adjust rectangular area with printer margins. 
      Rectangle adjustedRect = new Rectangle(
       ev.PageBounds.Left - (int)ev.PageSettings.HardMarginX, 
       ev.PageBounds.Top - (int)ev.PageSettings.HardMarginY, 
       ev.PageBounds.Width, 
       ev.PageBounds.Height); 

      // Draw a white background for the report 
      ev.Graphics.FillRectangle(Brushes.White, adjustedRect); 

      // Draw the report content 
      ev.Graphics.DrawImage(pageImage, adjustedRect); 

      // Prepare for the next page. Make sure we haven't hit the end. 
      m_currentPageIndex++; 
      ev.HasMorePages = (m_currentPageIndex < m_streams.Count); 
     } 
     catch (Exception ex) 
     { 

     } 

    } 
    private void Print() 
    { 
     if (m_streams == null || m_streams.Count == 0) 
      throw new Exception("Error: no stream to print."); 
     PrintDocument printDoc = new PrintDocument(); 
     if (!printDoc.PrinterSettings.IsValid) 
     { 
      throw new Exception("Error: cannot find the default printer."); 
     } 
     else 
     { 

      PrinterSettings pset = new PrinterSettings(); 
      printDoc.PrintPage += new PrintPageEventHandler(PrintPage); 
      m_currentPageIndex = 0; 
      printDoc.PrinterSettings.PrinterName = pset.PrinterName; 
      printDoc.Print(); 
     } 
    } 
+0

这可能会帮助你https://msdn.microsoft.com/en-us/library/aa290045(v=vs.71).aspx –

+1

感谢您的帮助@ pranav,我都试过,但没有解决我的问题.. :( – sarath

+0

可以请你分享你的打印代码吗? –

回答

2

我想你想从服务器本身打印页面。检查您的应用程序正在运行的标识。如果它在默认帐户下运行,请将其更改为您的帐户其他帐户有打印机访问权限。

转到IIS, (一)首先找到应用程序池您的应用使用 (B),然后去应用程序池细节,并找到身份它正在使用。 (c)将此标识更改为您的/某些具有打印机访问权限的其他帐户。

+0

谢谢你的帮助,我试过了为管理员帐户,但不工作.. :( – sarath

+0

你正在禁止所有的异常(空catch块)。你可以在你的异常块中添加一些日志并找出它正在抛出的确切错误? –

+0

它改变了我改变管理员用户正常用户..谢谢你.. :) – sarath