Sunday, February 12, 2012

Auto-Save to PDF

I want to be able to automatically create and save an SQL Report (rendered as
PDF) from a link on a web page.
I know how to setup the hyperlink to open the report directly in PDF
(without the user seeing 'Report Viewer') - however I actually want to just
save the PDF without user interaction!!!
A point in the right direction would be fantastic!
Regards,
Ryan.If you want to save the report (no matter in what format) on the server
side, as long as the web app's running user account has proper permission to
the saving location, then yes, it can be saved somewhere on server side. If
you want the report to be saved on user's computer silently, no, you cannot.
Every thing downloaded to browser (except for those used by browser, such as
cookies, that are saved in Internet temporary folder) can only be saved by
user's explicit permission. That is, server side code is not allowed to
silently save something on user's computer without user's acknowledge.
"Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
news:F729834A-4EA5-4DCF-BE31-0658DD37A92A@.microsoft.com...
>I want to be able to automatically create and save an SQL Report (rendered
>as
> PDF) from a link on a web page.
> I know how to setup the hyperlink to open the report directly in PDF
> (without the user seeing 'Report Viewer') - however I actually want to
> just
> save the PDF without user interaction!!!
> A point in the right direction would be fantastic!
> Regards,
> Ryan.|||Thank you for your reply!
I do want to save server-side - so that isn't going to be a problem... i'm
just struggling to find a way of doing this 'programatically' when the user
clicks a link on an ASP webpage.
As in, the user clicks a link (then maybe using URL syntax) and a PDF
version of the report is saved server side to a specific folder... I don't
want the user to see the report (as in don't want it to open in Report Viewer
or PDF) - just create it behind the scenes.
...hope i've made sense there!
Regards,
Ryan.
"Norman Yuan" wrote:
> If you want to save the report (no matter in what format) on the server
> side, as long as the web app's running user account has proper permission to
> the saving location, then yes, it can be saved somewhere on server side. If
> you want the report to be saved on user's computer silently, no, you cannot.
> Every thing downloaded to browser (except for those used by browser, such as
> cookies, that are saved in Internet temporary folder) can only be saved by
> user's explicit permission. That is, server side code is not allowed to
> silently save something on user's computer without user's acknowledge.
>
> "Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
> news:F729834A-4EA5-4DCF-BE31-0658DD37A92A@.microsoft.com...
> >I want to be able to automatically create and save an SQL Report (rendered
> >as
> > PDF) from a link on a web page.
> >
> > I know how to setup the hyperlink to open the report directly in PDF
> > (without the user seeing 'Report Viewer') - however I actually want to
> > just
> > save the PDF without user interaction!!!
> >
> > A point in the right direction would be fantastic!
> >
> > Regards,
> >
> > Ryan.
>
>|||OK, to save a report on the server side, you can call Reporting Services'
web services to generate the report, render it in desired format and save
it.
I just write a peice of code to allow user to request a report to be send to
a given email address as an attachment. Here is the simplified code (note,
you need to add web reference to the reporting services' web services):
private void GetReportByEmail()
{
//Validate txtEmail for inputting
if (txtEmail.Text.ToUpper().StartsWith("XX"))
{
lblPrompt.Text="You must enter a valid email address!";
lblPrompt.Visible=true;
return;
}
if (txtEmail.Text.IndexOf("@.")<3)
{
lblPrompt.Text="You must enter a valid email address!";
lblPrompt.Visible=true;
return;
}
//Get report
TimeSheet.wdsg_svr003.ReportingService rs=new
TimeSheet.wdsg_svr003.ReportingService();
rs.Url="http://localhost/ReportServer/ReportService.asmx";
rs.Credentials=System.Net.CredentialCache.DefaultCredentials;
string report="/WDTimeSheets/TimeSheetEmp/PayrollSummary";
string historyID=null;
string deviceInfo=null;
TimeSheet.wdsg_svr003.ParameterValue[] pmts=new
TimeSheet.wdsg_svr003.ParameterValue[3];
TimeSheet.wdsg_svr003.ParameterValue pmt;
pmt=new TimeSheet.wdsg_svr003.ParameterValue();
pmt.Name="EmployeeID";
pmt.Value=lblEmpID.Text;
pmts[0]=pmt;
pmt=new TimeSheet.wdsg_svr003.ParameterValue();
pmt.Name="FromDate";
pmt.Value=DateTime.Parse(ddlFrom.SelectedValue).ToString("yyyy-MM-dd");
pmts[1]=pmt;
pmt=new TimeSheet.wdsg_svr003.ParameterValue();
pmt.Name="ToDate";
pmt.Value=DateTime.Parse(txtTo.Text).ToString("yyyy-MM-dd");
pmts[2]=pmt;
TimeSheet.wdsg_svr003.DataSourceCredentials[] credentials=null;
string showHide=null;
string encoding=null;
string mimetype=null;
TimeSheet.wdsg_svr003.ParameterValue[] pmtUsed=null;
TimeSheet.wdsg_svr003.Warning[] warnings=null;
string[] streamIDs=null;
byte[] reportData;
//Get report in byte array
try
{
reportData=rs.Render(report,"PDF",historyID,deviceInfo,pmts,credentials,showHide,
out encoding,out mimetype,out pmtUsed,out warnings,out streamIDs);
}
catch(System.Web.Services.Protocols.SoapException ex)
{
lblPrompt.Text=ex.Message;
lblPrompt.Visible=true;
return;
}
//Save PDF report to a temp location
string
folder=System.Configuration.ConfigurationSettings.AppSettings["TempDataFolder"];
if (Directory.Exists(folder))
{
//Get a random file name
string f="PayRollSummary_" + DateTime.Today.ToString("yyyy-MM-dd") + "_"
+ lblEmpID.Text;
string fileName=folder + "\\" + f + ".pdf";
//Save the report (PDF) in a configured location
try
{
if (File.Exists(fileName)) File.Delete(fileName);
FileStream fs=File.OpenWrite(fileName);
fs.Write(reportData,0,reportData.Length);
fs.Close();
}
catch(System.IO.IOException ex)
{
lblPrompt.Text=ex.Message;
lblPrompt.Visible=true;
return;
}
//Send PDF file with email
EmailReport(fileName); //Method is omitted
}
else
{
lblPrompt.Text="Web server configuration error: emailing operation was
cancelled.";
lblPrompt.Visible=true;
}
}
HTH
"Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
news:449D6EC9-A442-426E-AE54-370376731FFF@.microsoft.com...
> Thank you for your reply!
> I do want to save server-side - so that isn't going to be a problem... i'm
> just struggling to find a way of doing this 'programatically' when the
> user
> clicks a link on an ASP webpage.
> As in, the user clicks a link (then maybe using URL syntax) and a PDF
> version of the report is saved server side to a specific folder... I don't
> want the user to see the report (as in don't want it to open in Report
> Viewer
> or PDF) - just create it behind the scenes.
> ...hope i've made sense there!
> Regards,
> Ryan.
> "Norman Yuan" wrote:
>> If you want to save the report (no matter in what format) on the server
>> side, as long as the web app's running user account has proper permission
>> to
>> the saving location, then yes, it can be saved somewhere on server side.
>> If
>> you want the report to be saved on user's computer silently, no, you
>> cannot.
>> Every thing downloaded to browser (except for those used by browser, such
>> as
>> cookies, that are saved in Internet temporary folder) can only be saved
>> by
>> user's explicit permission. That is, server side code is not allowed to
>> silently save something on user's computer without user's acknowledge.
>>
>> "Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
>> news:F729834A-4EA5-4DCF-BE31-0658DD37A92A@.microsoft.com...
>> >I want to be able to automatically create and save an SQL Report
>> >(rendered
>> >as
>> > PDF) from a link on a web page.
>> >
>> > I know how to setup the hyperlink to open the report directly in PDF
>> > (without the user seeing 'Report Viewer') - however I actually want to
>> > just
>> > save the PDF without user interaction!!!
>> >
>> > A point in the right direction would be fantastic!
>> >
>> > Regards,
>> >
>> > Ryan.
>>|||Hi,
Sorry to be a pain... but what language is your code? (it looks like
Javascript to me...however thought it would have to be ASP / ASPX or
something server side).
I am a novice - so sorry for stupid questions!!!
Ryan.
"Norman Yuan" wrote:
> OK, to save a report on the server side, you can call Reporting Services'
> web services to generate the report, render it in desired format and save
> it.
> I just write a peice of code to allow user to request a report to be send to
> a given email address as an attachment. Here is the simplified code (note,
> you need to add web reference to the reporting services' web services):
> private void GetReportByEmail()
> {
> //Validate txtEmail for inputting
> if (txtEmail.Text.ToUpper().StartsWith("XX"))
> {
> lblPrompt.Text="You must enter a valid email address!";
> lblPrompt.Visible=true;
> return;
> }
> if (txtEmail.Text.IndexOf("@.")<3)
> {
> lblPrompt.Text="You must enter a valid email address!";
> lblPrompt.Visible=true;
> return;
> }
> //Get report
> TimeSheet.wdsg_svr003.ReportingService rs=new
> TimeSheet.wdsg_svr003.ReportingService();
> rs.Url="http://localhost/ReportServer/ReportService.asmx";
> rs.Credentials=System.Net.CredentialCache.DefaultCredentials;
> string report="/WDTimeSheets/TimeSheetEmp/PayrollSummary";
> string historyID=null;
> string deviceInfo=null;
> TimeSheet.wdsg_svr003.ParameterValue[] pmts=new
> TimeSheet.wdsg_svr003.ParameterValue[3];
> TimeSheet.wdsg_svr003.ParameterValue pmt;
> pmt=new TimeSheet.wdsg_svr003.ParameterValue();
> pmt.Name="EmployeeID";
> pmt.Value=lblEmpID.Text;
> pmts[0]=pmt;
> pmt=new TimeSheet.wdsg_svr003.ParameterValue();
> pmt.Name="FromDate";
> pmt.Value=DateTime.Parse(ddlFrom.SelectedValue).ToString("yyyy-MM-dd");
> pmts[1]=pmt;
> pmt=new TimeSheet.wdsg_svr003.ParameterValue();
> pmt.Name="ToDate";
> pmt.Value=DateTime.Parse(txtTo.Text).ToString("yyyy-MM-dd");
> pmts[2]=pmt;
> TimeSheet.wdsg_svr003.DataSourceCredentials[] credentials=null;
> string showHide=null;
> string encoding=null;
> string mimetype=null;
> TimeSheet.wdsg_svr003.ParameterValue[] pmtUsed=null;
> TimeSheet.wdsg_svr003.Warning[] warnings=null;
> string[] streamIDs=null;
> byte[] reportData;
> //Get report in byte array
> try
> {
> reportData=rs.Render(report,"PDF",historyID,deviceInfo,pmts,credentials,showHide,
> out encoding,out mimetype,out pmtUsed,out warnings,out streamIDs);
> }
> catch(System.Web.Services.Protocols.SoapException ex)
> {
> lblPrompt.Text=ex.Message;
> lblPrompt.Visible=true;
> return;
> }
> //Save PDF report to a temp location
> string
> folder=System.Configuration.ConfigurationSettings.AppSettings["TempDataFolder"];
> if (Directory.Exists(folder))
> {
> //Get a random file name
> string f="PayRollSummary_" + DateTime.Today.ToString("yyyy-MM-dd") + "_"
> + lblEmpID.Text;
> string fileName=folder + "\\" + f + ".pdf";
> //Save the report (PDF) in a configured location
> try
> {
> if (File.Exists(fileName)) File.Delete(fileName);
> FileStream fs=File.OpenWrite(fileName);
> fs.Write(reportData,0,reportData.Length);
> fs.Close();
> }
> catch(System.IO.IOException ex)
> {
> lblPrompt.Text=ex.Message;
> lblPrompt.Visible=true;
> return;
> }
> //Send PDF file with email
> EmailReport(fileName); //Method is omitted
> }
> else
> {
> lblPrompt.Text="Web server configuration error: emailing operation was
> cancelled.";
> lblPrompt.Visible=true;
> }
> }
>
> HTH
> "Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
> news:449D6EC9-A442-426E-AE54-370376731FFF@.microsoft.com...
> > Thank you for your reply!
> >
> > I do want to save server-side - so that isn't going to be a problem... i'm
> > just struggling to find a way of doing this 'programatically' when the
> > user
> > clicks a link on an ASP webpage.
> >
> > As in, the user clicks a link (then maybe using URL syntax) and a PDF
> > version of the report is saved server side to a specific folder... I don't
> > want the user to see the report (as in don't want it to open in Report
> > Viewer
> > or PDF) - just create it behind the scenes.
> >
> > ...hope i've made sense there!
> >
> > Regards,
> >
> > Ryan.
> >
> > "Norman Yuan" wrote:
> >
> >> If you want to save the report (no matter in what format) on the server
> >> side, as long as the web app's running user account has proper permission
> >> to
> >> the saving location, then yes, it can be saved somewhere on server side.
> >> If
> >> you want the report to be saved on user's computer silently, no, you
> >> cannot.
> >> Every thing downloaded to browser (except for those used by browser, such
> >> as
> >> cookies, that are saved in Internet temporary folder) can only be saved
> >> by
> >> user's explicit permission. That is, server side code is not allowed to
> >> silently save something on user's computer without user's acknowledge.
> >>
> >>
> >> "Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
> >> news:F729834A-4EA5-4DCF-BE31-0658DD37A92A@.microsoft.com...
> >> >I want to be able to automatically create and save an SQL Report
> >> >(rendered
> >> >as
> >> > PDF) from a link on a web page.
> >> >
> >> > I know how to setup the hyperlink to open the report directly in PDF
> >> > (without the user seeing 'Report Viewer') - however I actually want to
> >> > just
> >> > save the PDF without user interaction!!!
> >> >
> >> > A point in the right direction would be fantastic!
> >> >
> >> > Regards,
> >> >
> >> > Ryan.
> >>
> >>
> >>
>
>|||It is ASP.NET code in C#. Since you need to get the report in binary on
server side (reporting server), hence the ASP.NET.
"Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
news:5A8EBADD-325B-4E54-BFA8-C2206073CB5E@.microsoft.com...
> Hi,
> Sorry to be a pain... but what language is your code? (it looks like
> Javascript to me...however thought it would have to be ASP / ASPX or
> something server side).
> I am a novice - so sorry for stupid questions!!!
> Ryan.
> "Norman Yuan" wrote:
>> OK, to save a report on the server side, you can call Reporting Services'
>> web services to generate the report, render it in desired format and save
>> it.
>> I just write a peice of code to allow user to request a report to be send
>> to
>> a given email address as an attachment. Here is the simplified code
>> (note,
>> you need to add web reference to the reporting services' web services):
>> private void GetReportByEmail()
>> {
>> //Validate txtEmail for inputting
>> if (txtEmail.Text.ToUpper().StartsWith("XX"))
>> {
>> lblPrompt.Text="You must enter a valid email address!";
>> lblPrompt.Visible=true;
>> return;
>> }
>> if (txtEmail.Text.IndexOf("@.")<3)
>> {
>> lblPrompt.Text="You must enter a valid email address!";
>> lblPrompt.Visible=true;
>> return;
>> }
>> //Get report
>> TimeSheet.wdsg_svr003.ReportingService rs=new
>> TimeSheet.wdsg_svr003.ReportingService();
>> rs.Url="http://localhost/ReportServer/ReportService.asmx";
>> rs.Credentials=System.Net.CredentialCache.DefaultCredentials;
>> string report="/WDTimeSheets/TimeSheetEmp/PayrollSummary";
>> string historyID=null;
>> string deviceInfo=null;
>> TimeSheet.wdsg_svr003.ParameterValue[] pmts=new
>> TimeSheet.wdsg_svr003.ParameterValue[3];
>> TimeSheet.wdsg_svr003.ParameterValue pmt;
>> pmt=new TimeSheet.wdsg_svr003.ParameterValue();
>> pmt.Name="EmployeeID";
>> pmt.Value=lblEmpID.Text;
>> pmts[0]=pmt;
>> pmt=new TimeSheet.wdsg_svr003.ParameterValue();
>> pmt.Name="FromDate";
>> pmt.Value=DateTime.Parse(ddlFrom.SelectedValue).ToString("yyyy-MM-dd");
>> pmts[1]=pmt;
>> pmt=new TimeSheet.wdsg_svr003.ParameterValue();
>> pmt.Name="ToDate";
>> pmt.Value=DateTime.Parse(txtTo.Text).ToString("yyyy-MM-dd");
>> pmts[2]=pmt;
>> TimeSheet.wdsg_svr003.DataSourceCredentials[] credentials=null;
>> string showHide=null;
>> string encoding=null;
>> string mimetype=null;
>> TimeSheet.wdsg_svr003.ParameterValue[] pmtUsed=null;
>> TimeSheet.wdsg_svr003.Warning[] warnings=null;
>> string[] streamIDs=null;
>> byte[] reportData;
>> //Get report in byte array
>> try
>> {
>> reportData=rs.Render(report,"PDF",historyID,deviceInfo,pmts,credentials,showHide,
>> out encoding,out mimetype,out pmtUsed,out warnings,out streamIDs);
>> }
>> catch(System.Web.Services.Protocols.SoapException ex)
>> {
>> lblPrompt.Text=ex.Message;
>> lblPrompt.Visible=true;
>> return;
>> }
>> //Save PDF report to a temp location
>> string
>> folder=System.Configuration.ConfigurationSettings.AppSettings["TempDataFolder"];
>> if (Directory.Exists(folder))
>> {
>> //Get a random file name
>> string f="PayRollSummary_" + DateTime.Today.ToString("yyyy-MM-dd") +
>> "_"
>> + lblEmpID.Text;
>> string fileName=folder + "\\" + f + ".pdf";
>> //Save the report (PDF) in a configured location
>> try
>> {
>> if (File.Exists(fileName)) File.Delete(fileName);
>> FileStream fs=File.OpenWrite(fileName);
>> fs.Write(reportData,0,reportData.Length);
>> fs.Close();
>> }
>> catch(System.IO.IOException ex)
>> {
>> lblPrompt.Text=ex.Message;
>> lblPrompt.Visible=true;
>> return;
>> }
>> //Send PDF file with email
>> EmailReport(fileName); //Method is omitted
>> }
>> else
>> {
>> lblPrompt.Text="Web server configuration error: emailing operation
>> was
>> cancelled.";
>> lblPrompt.Visible=true;
>> }
>> }
>>
>> HTH
>> "Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
>> news:449D6EC9-A442-426E-AE54-370376731FFF@.microsoft.com...
>> > Thank you for your reply!
>> >
>> > I do want to save server-side - so that isn't going to be a problem...
>> > i'm
>> > just struggling to find a way of doing this 'programatically' when the
>> > user
>> > clicks a link on an ASP webpage.
>> >
>> > As in, the user clicks a link (then maybe using URL syntax) and a PDF
>> > version of the report is saved server side to a specific folder... I
>> > don't
>> > want the user to see the report (as in don't want it to open in Report
>> > Viewer
>> > or PDF) - just create it behind the scenes.
>> >
>> > ...hope i've made sense there!
>> >
>> > Regards,
>> >
>> > Ryan.
>> >
>> > "Norman Yuan" wrote:
>> >
>> >> If you want to save the report (no matter in what format) on the
>> >> server
>> >> side, as long as the web app's running user account has proper
>> >> permission
>> >> to
>> >> the saving location, then yes, it can be saved somewhere on server
>> >> side.
>> >> If
>> >> you want the report to be saved on user's computer silently, no, you
>> >> cannot.
>> >> Every thing downloaded to browser (except for those used by browser,
>> >> such
>> >> as
>> >> cookies, that are saved in Internet temporary folder) can only be
>> >> saved
>> >> by
>> >> user's explicit permission. That is, server side code is not allowed
>> >> to
>> >> silently save something on user's computer without user's acknowledge.
>> >>
>> >>
>> >> "Ryan Aldred" <RyanAldred@.discussions.microsoft.com> wrote in message
>> >> news:F729834A-4EA5-4DCF-BE31-0658DD37A92A@.microsoft.com...
>> >> >I want to be able to automatically create and save an SQL Report
>> >> >(rendered
>> >> >as
>> >> > PDF) from a link on a web page.
>> >> >
>> >> > I know how to setup the hyperlink to open the report directly in PDF
>> >> > (without the user seeing 'Report Viewer') - however I actually want
>> >> > to
>> >> > just
>> >> > save the PDF without user interaction!!!
>> >> >
>> >> > A point in the right direction would be fantastic!
>> >> >
>> >> > Regards,
>> >> >
>> >> > Ryan.
>> >>
>> >>
>> >>
>>

No comments:

Post a Comment