2011-02-01 3 views
1

시나리오 :이미지를 저장하지 않고 SQL Reporting Services (.rdlc)에서 이미지 기반 PDF 보고서를 생성 할 수 있습니까?

는 여러 개의 차트와 웹 페이지를 가지고 있고 사용자가 클릭 할 수 있어야에 'PDF로 내보내기'버튼을 가지고는 사용자가 다음 저장할 수있는 PDF를 생성합니다.

을 감안할 때 :

Telerik RadChart 같은 메모리 스트림에 자신을 저장할 수 :

MemoryStream chartStream = new MemoryStream(); 
RadChart1.Save(chartStream, ImageFormat.Png); 

이 메모리 스트림을 사용하여, 그것은 SQL Reporting Services를 사용하여 PDF를 구축 할 수 있습니다 WITH OUT 먼저 파일에 저장 OR 먼저 MSSQL 테이블에 삽입해야합니까?

본인은이 문제에 대한 오픈 소스 또는 무료 솔루션 인 답변을 승낙하고 수락합니다.

감사합니다.

+0

당신은 그것을 즉시 PDF로 작가로하는 PrintDocument 클래스를 사용하여 코드에서 할 수 있습니까? –

+0

@ Paul, 좋은 생각이지만 다른 변수 데이터와 함께 차트가 여러 개 있기 때문에 효과가 없습니다. 보고서 서비스를 사용하여 PDF를 작성해야합니다 (모든 차트를 이미지로 저장하거나 데이터베이스 테이블에 저장하도록 강요합니다) 또는 다른 PDF 생성기를 사용해야합니다. – DJTripleThreat

답변

3

좋아, 그냥이 알아 냈어.

  1. 차트 이미지 (들)

    Step 1

  2. 후킹를 개최 형식화 된 데이터 집합을 만들기 :이 답변에 세 가지 요소, 두 개의 스크린 샷을 위해 따라야 할 다음 몇 가지 코드가있다

    Step 2

  3. BU RDLC 보고서까지 형식화 된 데이터 집합 tton PDF를 생성하고 브라우저로 스트리밍하기위한 코드를 클릭하십시오.

    protected void btnPDF_Click(object sender, EventArgs e) 
    { 
        //Put the image you want to display in a MemoryStream. You can read an image from the file system 
        //or generate an image, etc. This example renders an image to a memory stream using a custom charting control. 
        MemoryStream chtLoginsByMonthStream = new MemoryStream(); 
        this.chtLoginsByMonth.Save(chtLoginsByMonthStream, ImageFormat.Png); 
    
    
        //Setup the datatable you will pass into the RDLC 
        dsStudentUsage.dtUsageChartsDataTable dt = new dsStudentUsage.dtUsageChartsDataTable(); 
        dsStudentUsage.dtUsageChartsRow dr = dt.NewdtUsageChartsRow(); 
    
        //create new Byte Array, this will hold the image data from the memory stream 
        byte[] chtLoginsByMonthArray = new byte[chtLoginsByMonthStream.Length]; 
    
        //Set pointer to the beginning of the stream 
        chtLoginsByMonthStream.Position = 0; 
    
        //Read the entire stream 
        chtLoginsByMonthStream.Read(chtLoginsByMonthArray, 0, (int)chtLoginsByMonthStream.Length); 
    
        //Put the byte array into the new datarow in the appropriate column 
        dr["imgLoginsByMonth"] = chtLoginsByMonthArray; 
        dt.Rows.Add(dr); 
    
        //Add the data source to the report. 
        ReportViewer1.LocalReport.DataSources.Add(new ReportDataSource("dsStudentUsage_dtUsageCharts", dt)); 
    
        //Setup objects for streaming the PDF to the browser 
        Warning[] warnings; 
        string[] streamids; 
        string mimeType; 
        string encoding; 
        string extension; 
        byte[] bytes; 
    
    
        //Make a container for all of your report parameters 
        var rptList = new List<KeyValuePair<string, string>>(); 
        rptList.Add(new KeyValuePair<string, string>("rpTotalLogins", "2,000")); 
        //more parameters go here 
    
        //Feed the report parameters into the actual "ReportParameters" class 
        ReportParameter[] rptParams = new ReportParameter[rptList.Count]; 
        for (int i = 0; i < rptList.Count; i++) 
        { 
         rptParams[i] = new ReportParameter(rptList[i].Key, rptList[i].Value); 
        } 
    
        //Set parameters for the report. 
        ReportViewer1.LocalReport.SetParameters(rptParams); 
    
        //Render the report to a byte array in PDF format 
        bytes = ReportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings); 
    
        //Set the stream to either prompt user as file download or "inline" to stream 
        //PDF directly into the browser window. 
    
        HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=StudentUsageReport.pdf"); 
        //HttpContext.Current.Response.AddHeader("Content-disposition", "inline;"); 
        HttpContext.Current.Response.ContentType = "application/pdf"; 
        HttpContext.Current.Response.BinaryWrite(bytes); 
        HttpContext.Current.Response.End(); 
    
    } 
    
+0

EPIC! 이것은 내가 찾고 있었던 바로 그 것이다. 메모리에 데이터 세트를 구축하고 데이터베이스를 모두 전달할 수 있습니다. :) – DJTripleThreat

2

이전에 아무런 문제없이 this 방법을 사용해 봤는데 큰 도움이되었습니다.

+0

고마워요.하지만 데이터베이스를 사용하지 않고 이미지를 추가하는 방법이 아니라 PDF를 렌더링하는 방법은 무엇입니까? +1이 여전히 유용하기 때문에 어쨌든 +1하십시오. – DJTripleThreat

1

필자는 모든 PDF 생성을 위해 iTextSharp를 사용합니다. 약간의 학습 곡선이 있지만 꽤 쉽게 얻을 수 있습니다. 여기에 몇 가지 링크는 다음과 같습니다

관련 문제