ASP.NET MVC - How to use custom Culture in EvoHtmlToPdf - asp.net

my program (ASP.NET MVC with VB.NET) is to convert local HTML(with dynamic conrtoller action) to PDF file using third-party tools (EvoHtmlToPdf)
However, my local HTML contains a lot of CSS and I can only use EvoHtmlToPdf's API to convert PDF (pass the link to the function)
outPdfBuffer = htmlToPdfConverter.ConvertUrl(Me.Url.Action("TestDownloadPDFText", "ReportA", New With {.Culture = Session("Culture")}, Me.Request.Url.Scheme))
Whatever I set the culture in different place, the PDF will be showed in English only. If I just try http://localhost/ReportA/TestDownloadPDFText and it can show Chinese when I hard code Chinese culture in code behind.
So, I guess the problem is coming from the third-party tools (EvoHtmlToPdf). Any suggestion or solution for this case? Thanks a lot.

I have finally solved the problem. The solution is to add culture configuration in that controller.
Dim ci As CultureInfo
ci = New CultureInfo(Culture)
System.Threading.Thread.CurrentThread.CurrentUICulture = ci
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(ci.Name)
Thanks all.

Related

How to add parameters to SSRS URL in report viewer?

In reference to this article:
http://msdn.microsoft.com/en-us/library/ms252072(VS.80).aspx
I can access my SSRS report from my Web Application but the problem is it does not accept the parameters in the URL.
here is the format that it accepts
Report Server URL:
http://ABCD.com/reportserver
Report Path:
/Company works/Sales report/ABC
Can anyone help me?
I want to pass parameters in the SSRS url through report viewer.
You can not pass parameters via the URL since the control is expecting on the URL to the actual reportserver and nothing more.
Example:
serverReport.ReportServerUrl = new Uri("http://ABCS.com/reportserver");
serverReport.ReportPath = "/AdventureWorks Sample Reports/My Report";
You can pass these parameters in your code behind though by using the ReportViewer.ServerReport.SetParameters() function. Here is a snippet of code I used for this purpose:
var parametersCollection = new List<ReportParameter>();
//add generic parameters that every report needs
parametersCollection.Add(new ReportParameter("RandomizerString", new Random().Next().ToString(), false));
parametersCollection.Add(new ReportParameter("MachineName", Request.UserHostAddress.ToString(), false));
ERDashboard.ServerReport.SetParameters(parametersCollection);
can't seem to add a comment to Sam's answer so I'll post another:
The ReportViewerControl ReportServerURL property is a lot different than formatting a general URL to access a report. You could use the logic that sam has provided to navigate a web browser control to that URL which would also render the report within your web app. It's another to try if you want.
As a note there are various reasons that you may choose a webbrowser control vs a reportviewer control. The reportviewer control is a lot easier to manage in the code-behind, but there is a memory issue when refreshing a report for extended periods of time (lots of information through google about memory leaks). This is the scenario where I have instead used the webbrowser and manually formatted URLs to pass parameters instead.

Possible to use one resource file with multiple languages?

I've got a .net 4.0 website that we have 2 copies of it running. One for US based users and another for AU based users. Code is basically the same, the only differences being some text and wording here or there where it references the US versus Australia. Right now I have two copies of the site which is pretty silly. So I want to maintain just one copy and put all these regional text changes in a resource file.
Is it possible to have just a single resource file contain multiple 'languages' or do i need to create a Resource.US.resx and a Resource.AU.resx file? Also, if i do create two files, how do I tell .net which file to use in each site? I assume in the web.config globalization uiCulture & culture='en-AU' or en-US would tell .net which of the two files to use?
Yes, you are right, you can use Resource.US.resx and a Resource.AU.resx files and tell in web.config globalization uiCulture & culture='en-AU' or en-US
in code:
string culturePref = [Your setting from web-config]; // "en-US" or "en-EU"
try
{
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(culturePref);
}
catch
{
Thread.CurrentThread.CurrentCulture =
new CultureInfo("en-US");
}
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;

SQL Server Reporting Services: how to print reports automatically without preview from a web application

I'm using SQL Server Reporting Services and viewing the reports in a web application in ASP.NET.
To display the reports, I'm using Report viewer Web control which brings funcionalities of exporting the report and/or printing it, but requires to display a preview of the report before printing it.
I need to print a report without doing a preview in the web page?
It seems like there's a way to do it in WinForms, but I didn't find a way to do it in WebForms.
Any ideas?
Thanks
David
I don't think this is possible. The SSRS webforms reportviewer is quite rigid.
What's even much worse: The printing function only exists in IE, not in other browsers. Firefox, Safari, Chrome and Opera users have to download the file as PDF or Word and print from there.
I've rendered SSRS reports without using the ReportViewer control, most recently using LocalReports. You can in code configure the ReportViewer, force it to generate a PDF, and grab the byte stream of the PDF. I stopped there and rendered the PDF to the screen b/c that was my set of requirements, but I'm sure its much easier to find a way to print a byte stream of a PDF file then it is to deal with anything with the ReportViewer.
Here is how to get the LocalReport to a byte array of a PDF file:
LocalReport lclRpt = new LocalReport();
//Do Stuff like bind DataSources, ReportParameters, SubReportProcessing Delegates, etc.
string strMIMEType = String.Empty;
string strEncoding = String.Empty;
string strFileNameExtension = string.Empty;
string[] strarrStreams;
Warning[] warnLocalReportWarnings;
byte[] bytarrPDF = lclRpt.Render("PDF", "<DeviceInfo><StartPage>0</StartPage></DeviceInfo>", out strMIMEType, out strEncoding, out strFileNameExtension, out strarrStreams, out warnLocalReportWarnings);
return bytarrPDF;
I'm not 100% on how to accomplish your last step, might need a .pdf utility or there might be a way to do it straight from the code.

Is it possible to create a .ascx virtually? i.e. not on disk, but as a string?

I was wondering if it was possible to load a asp.net control (.ascx) that doesn't reside on the file system?
Like say a string variable?
Not a string varible but you can load it from resources or zip file, but you have to have full trust. Google for VirtualPathProvider.
As of 4.0 you don't have to have full trust.
System.Reflection.Emit
Assembly.Load(byte[])
No designer for you if you do this though.
You could try this:
string controlString = //read or build it
Control control = this.Page.TemplateControl.ParseControl(controlString);
More information is available here:
http://msdn.microsoft.com/en-us/library/kz3ffe28.aspx

What's the best way to display an image from a sql server database in asp.net?

I have a sql server database that returns byte for the image. If I use the tableadapter wizard and set it to my stored procedure and preview data, it pulls back an image. It automatically turns it into an image in the preview data. I don't see it as a string of Ints or anything.
How can I display it on my asp.net webpage with a gridview and objectdatasource?
I have searched and foudn where the imagefield can point to a url on another page that does the byte transformation but I'm not sure it's the best. I found another way that creates a temp file.
Just trying to see the best way to do it.
edit - I am trying not to use a temp file. If I cannot use a gridview a regular image field is ok.
asp.net 2.0, c#.
Thank you for any help.
edit
ended up with:
protected void Page_Load(object sender, EventArgs e)
{
string id = Request["id"];
string connstr = "DSN=myserver";
OdbcConnection conn = new OdbcConnection(connstr);
OdbcCommand cmd = new OdbcCommand("{call mySP (?)}", conn);
cmd.CommandType = CommandType.StoredProcedure;
// Add the input parameter and set its properties.
OdbcParameter parameter = new OdbcParameter();
parameter.ParameterName = "#MyParam";
parameter.OdbcType = OdbcType.VarChar;
parameter.Direction = ParameterDirection.Input;
parameter.Value = id;
// Add the parameter to the Parameters collection.
cmd.Parameters.Add(parameter);
conn.Open();
OdbcDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
byte[] buffer = (byte[])dr[0];
Response.ContentType = "image/jpg";
Response.BinaryWrite(buffer);
Response.Flush();
}
}
and this on the calling page:
<asp:Image ID="Image1" ImageAlign="Middle" ImageUrl="show.aspx?id=123" Runat="server" />
Two options:
Create a temp file - The problem with this approach is that you have to create the file, which means your web must have write access to a directory which is not a great thing. You also need to have a way to clean up the images.
Serve it from another URL - This is my preferred method, as you have no disk access required. A simple http handler (ashx) is a great method to serve up the image.
Edit
If you need session state in the ashx, check out: Asp.net System.Web.HttpContext.Current.Session null in global.asax.
Edit
Couple more thoughts. There are some cases where using a temp file might be better. For example if your images are requested frequently by a lot of users. Then storing the images on the disk would make sense, since you could write the file once, this does increase the maintance complexity but depending on traffic it might be worth it since this would let you avoid calling back into the .net stack and leverage IIS caching of static content.
I wrote the SqlReader plugin for open-source ImageResizing.Net library to allow you to serve and display images from a SQL database in the most performance-optimal way.
Even if you don't need to do any image processing whatsoever, it's still (a) the easiest, and (b) the most efficient way to do it. You can combine it with disk caching (which provides automatic cleanup) to get the best performance that is possible.
Installation is easy - 2 nuget commands, or copy & paste into Web.Config, your pick.
If you need help, support is free and fast.
The sample code you added is good but you should move it to a .ashx file which is meant for such things.
Here is some example code on how to do this.

Resources