2015-05-04 69 views
0

我最近將jsreport設置爲ASP.NET應用程序中的嵌入式服務器。 經過很多的努力,我已經能夠生成html報告並使用.net jsreport.Client以最簡單的形式將下面的代碼返回給瀏覽器。jsreport .net客戶端API返回空PDF

正確或錯誤我已將jsreport嵌入到現有的VB應用程序中,並將所有示例代碼轉換爲C#(我不是C#人員)。

Imports System.Net 
Imports System.Web.Http 
Imports System.Net.Http 
Imports System.Data 
Imports System.Text.RegularExpressions 
Imports System.IO 
Imports System.Net.Http.Headers 
Imports jsreport.Client 
Imports jsreport.Client.Entities 

<ActionName("ProcessReport")> 
Public Async Function ProcessReport(ByVal reportID As Integer) As Threading.Tasks.Task(Of HttpResponseMessage) 

Dim _reportingService = EmbeddedServer.ReportingService 
Dim Report = Await _reportingService.RenderAsync(New RenderRequest() _ 
With {.template = New Template() _ 
    With { 
      .recipe = "html", 
      .content = "<h1>Some content</h1>" 
     } 
    } 
) 

Dim ReportContent As String = New StreamReader(Report.Content).ReadToEnd() 
Dim response = New HttpResponseMessage() 
response.StatusCode = HttpStatusCode.OK 
response.Content = New StringContent(ReportContent) 
response.Content.Headers.ContentType = New MediaTypeHeaderValue(Report.ContentType.MediaType) 
Return response 

End Function 

但我的問題是,我不能使用phantom-pdf配方生成PDF,如下所示。 ReportContent是一個PDF文檔,但它是空的並且沒有內容。

With { 
    .recipe = "phantom-pdf", 
    .content = "<h1>Some content</h1>" 
    } 

請注意,如果我期待在:

的App_Data >> jsreport淨嵌入式>>數據>>臨時

運行上面的代碼後有已呈現一個PDF內容,但不會在回覆中返回。

回答

1

而我不是一個VB傢伙。

但是下面的代碼保存正確的PDF文件到我

Dim fs As New FileStream("f:\\temp\\out.pdf", FileMode.Create) 
Report.Content.CopyTo(fs) 

下面的代碼在asp.net mvc的返回正確的PDF

Return New FileStreamResult(Report.Content, "application/pdf") 

當我們正在與啓用Visual Studio中顯示PDF問題瀏覽器鏈接,也許你可以嘗試禁用它 http://www.poconosystems.com/software-development/how-to-disable-browser-link-in-visual-studio-2013/

你也可以嘗試安裝完整在預先jsreport上或自己做渲染web請求。這是與.net 4兼容的明碼,用於提出渲染請求 https://gist.github.com/pofider/1897f8a1b6dab72a49b0