首页 > 解决方案 > 在 Azure WebApp 上将 ReportViewer 报告打印为 PDF。获取一般 GDI 错误

问题描述

我的代码将获取 ReportViewer 报告并通过 PrintDialog 打印它。但是,当我将它移动到 Azure WebApp 时,我会收到一般 GDI 错误。我一直在寻找解决方案,但我发现 Azure 正在阻止大多数 GDI 调用。它在下面的 Export 方法上搞砸了。任何建议的工作?

    private Stream CreateStream(string name, string fileNameExtension, Encoding encoding, string mimeType, bool willSeek)
    {
        try
        {
            Stream stream = new MemoryStream();
            m_streams.Add(stream);
            return stream;
        }
        catch (Exception ex)
        {
            throw new Exception("CreateStream: " + ex.Message, ex.InnerException);
        }
    }

    public void Export(LocalReport report)
    {
        try
        {
            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;
        }
        catch (Exception ex)
        {
            throw new Exception("Export: " + ex.Message, ex.InnerException);
        }

    }

标签: azurereportviewergdiazure-web-app-service

解决方案


实际上,我认为问题在于 Azure 的 GDI 不支持 EMF,Telerik Reporting 的桌面查看器和 ReportProcessor 在打印时使用 EMF。但是,在R2 2018 SP1中,该问题得到了解决,并且可以调整打印格式,以便引擎将使用位图而不是 Azure 支持的元文件进行打印。有问题的渲染扩展名为ImagePrint,可以通过应用程序的配置文件进行设置,如下所示:

<configuration> 
  <!-- The configSectins element should be the first child element of configuration --> 
  <configSections>
  <section
    name="Telerik.Reporting"
    type="Telerik.Reporting.Configuration.ReportingConfigurationSection, Telerik.Reporting, Version=x.x.x.x, Culture=neutral, PublicKeyToken=a9d7983dfcc261be"
    allowLocation="true"
    allowDefinition="Everywhere"/>
  </configSections>
       
  <Telerik.Reporting> 
    <extensions> 
      <render> 
        <extension name="IMAGEPrint"> 
          <parameters> 
            <parameter name="OutputFormat" value="PNG"/> 
            <parameter name="DpiX" value="300"/> 
            <parameter name="DpiY" value="300"/> 
          </parameters> 
        </extension> 
      </render> 
    </extensions> 
  </Telerik.Reporting> 
</configuration> 

DpiX和DpiY参数不是强制性的,但由于默认设置为 96,这可能会导致图像像素化,尤其是在打印到 DPI 较高的打印机时,因此最好通过 app.config 配置它们


推荐阅读