I have a webpage that activates a print job on a printer. This works in the localhost environment but does not work when the application is deployed to the webserver. I'm using the PrintDocument class from the .net System.Drawing.Print namespace. I'm now assuming the printer has to be available to the application on the remote server? Any suggestions on how I would get this to work?
PrintDocument pd = new PrintDocument();
PaperSource ps = new PaperSource();
pd.DefaultPageSettings.PaperSize =
new System.Drawing.Printing.PaperSize("Custom", 1180, 850);
pd.PrintPage += new PrintPageEventHandler
(this.pd_PrintPage);
// Set your printer's name. Obtain from
// System's Printer Dialog Box.
pd.PrinterSettings.PrinterName =
"Okidata ML 321 Turbo/D (IBM)";
//PrintPreviewDialog dlgPrintPvw = new PrintPreviewDialog();
//dlgPrintPvw.Document = pd;
//dlgPrintPvw.Focus();
//dlgPrintPvw.ShowDialog();
pd.Print();
The printer is on a different computer. PrintDocument is for use in desktop applications, not web applications.
To print on the client, you would need to use JavaScript, and you would only be able to print documents already on the client machine. I don't know for sure that there is a way to print on the client. You may be able to display a "Print" dialog and have the user print the file himself.
I was having the same issues. I was told to put your code inside of this:
using (WindowsIdentity.GetCurrent().Impersonate())
{
// code here
}
It allows for specific user settings to be used instead of the ASP.NET settings for that particular printer.
This code got it to the printer, but now I'M having issues with multiple copies of one webform hitting the printer.
Related
I have a windows service that prints pdf files once it receives a request. It uses PrintDocument, but for some printers it seems to ignore printer and tray settings I give it in my code.
Currently every printer has a few trays which are all installed as seperate queues. For some printers I can just set the PrinterName property (of PrintDocument) to the name of the queue and it works fine. However a couple of printers seem to ignore this. I also tried setting the papersource, but this seems to alway be ignored.
Here's the code used to print:
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.PrinterName = printer; //printer is send to the service along with the request
pd.Print();
Again: this works for some printers, but not for all.
I also tried using Papersource like this:
pd.PrinterSettings.DefaultPageSettings.PaperSource.SourceName =
pd.PrinterSettings.PaperSources[tray - 1].SourceName; //Tray is also send with the request
and like this:
pd.PrinterSettings.DefaultPageSettings.PaperSource =
pd.PrinterSettings.PaperSources[tray - 1];
What am I doing wrong here?
EDIT: The pdf file always has content in it, so it can't be empty.
I changed my code to use PrintQueue instead (https://msdn.microsoft.com/en-us/library/system.printing.printqueue(v=vs.110).aspx). This seems to work well, since I can directly call the queues instead of the printer.
My Requirement is to print invoices in pdf direct to local printer from web application developed in .net mvc framework.
I need to do exact like shipstation is doing with SHIPSTATION CONNECT
SHIPSTATION CONNECT
Does it use process like
REMOTE PRINTER SHARING CODEPROJECT
or using WMI library to share printer remotely.
Any expert thought will help me and my programmer to build the solution.I am not expecting code or spoon feeding but like to know the process and way to start on this in right direction.
Thanks in advance for the help!
regards
check printnode.com might be of some help.Seems like doing same thing what you want.The service is not free chargeable or alternatively you can build same using google cloud print.
you can write javascript function that print from local printer,
w=window.open();
w.document.open();
w.document.write("<html><head></head><body>");
w.document.write("HI");
w.document.write("</body></html>");
w.document.close();
w.print();
w.close();
working example:
http://jsfiddle.net/xwgq5ap4/
if you want to print from the server you need to send a request for the server for example :
www.mysite.com/print.aspx?file=invoice.pdf
to print it by the server you have 2 solutions the first is calling to other process to accomplish it like you can see in this answer:
Print Pdf in C#
the second is write your own implementation using PrintDocument namespace for example:
namespace PrintPDF
{
class Program
{
static void Main(string[] args)
{
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("sample.pdf");
//Use the default printer to print all the pages
//doc.PrintDocument.Print();
//Set the printer and select the pages you want to print
PrintDialog dialogPrint = new PrintDialog();
dialogPrint.AllowPrintToFile = true;
dialogPrint.AllowSomePages = true;
dialogPrint.PrinterSettings.MinimumPage = 1;
dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count;
dialogPrint.PrinterSettings.FromPage = 1;
dialogPrint.PrinterSettings.ToPage = doc.Pages.Count;
if (dialogPrint.ShowDialog() == DialogResult.OK)
{
doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage;
doc.PrintToPage = dialogPrint.PrinterSettings.ToPage;
doc.PrinterName = dialogPrint.PrinterSettings.PrinterName;
PrintDocument printDoc = doc.PrintDocument;
dialogPrint.Document = printDoc;
printDoc.Print();
}
}
}
}
original taken from free 3rd party library
I have a report that I want to fire off based on a button in a GridView. This will generate a label and send it to an attached (local) Zebra printer. When I run this locally, it works fine. The printer doesn't even have to be the default. When I copy the files to the server, and click the Print button nothing happens.
CrystalReportSource CrystalReportSource1 = new CrystalReportSource();
CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();
CrystalReportViewer1.HasPrintButton = true;
// CrystalReportViewer1.PrintMode = "ActiveX";
CrystalReportViewer1.ReportSource = CrystalReportSource1;
CrystalReportViewer1.EnableParameterPrompt = false;
CrystalReportSource1.Report.FileName = "BinLocation2.rpt";
TableLogOnInfo logOnInfo = new TableLogOnInfo();
CrystalReportSource1.ReportDocument.SetParameterValue(0, Item);
CrystalReportSource1.ReportDocument.SetParameterValue(1, binlocation);
CrystalReportSource1.ReportDocument.SetParameterValue(2, Lot);
CrystalReportSource1.ReportDocument.SetParameterValue(3, expiredate);
CrystalReportSource1.ReportDocument.SetParameterValue(4, NDC);
logOnInfo.ConnectionInfo.ServerName = ConfigurationManager.AppSettings["SalesReportServerName"];
logOnInfo.ConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["SalesReportDatabaseName"];
logOnInfo.ConnectionInfo.UserID = ConfigurationManager.AppSettings["SalesReportUserID"];
logOnInfo.ConnectionInfo.Password = ConfigurationManager.AppSettings["SalesReportPassword"];
TableLogOnInfos infos = new TableLogOnInfos();
infos.Add(logOnInfo);
CrystalReportViewer1.LogOnInfo = infos;
try
{
CrystalReportSource1.ReportDocument.PrintToPrinter(1, false, 0, 0);
}
I installed the redist packages for Visual Studio. Compared the web.config files. The printer is not installed on the server, but I want this done on the client side (with no prompt to print). Is there a better way to go about doing this? What am I missing?
Firstly, this is how I've achieved this functionality (explanations will follow):
// get the user selected printer
var printerName = Settings.Instance[profile].DirectPrinter;
// enumerate the printers visible to the application
var allPrinters = PrinterSettings.InstalledPrinters.OfType<string>().OrderBy(p => p).ToList();
// Strip out PDF, XPS and any file based printers. These cause havoc on a server based print
var acceptablePrinters = allPrinters.Except(allPrinters
.Where(s => Settings.Instance[profile].PrintersToIgnore
.Any(u => s.ToLower().Contains(u)))).ToList();
if (acceptablePrinters.Contains(printerName))
{
report.PrintOptions.PrinterName = printerName;
report.PrintToPrinter(copies, collate, 0, 0);
}
else
{
// Log the reason why the printer was rejected
// Or you may choose to just print to default
}
Explanation
The PrintToPrinter method runs on the server side, so it will only work with printers that the server and the IIS application pool identity can 'see'.
Most Zebra printers will be networked, so with configuration can be made visible across the entire network. However, you need to allow configuration in your asp.net application to set a printer name at runtime (even if it's always the same), and then you can call PrintToPrinter that will print to a networked printer.
Printing to client without a prompt is practically impossible, and ill advised. For that I allow the user to choose to print to client and then export to PDF, and the standard PDF print dialog box prompts the user for his printer settings. This was added to allow printing to non-networked printers.
I'm creating a RDLC report and printing it from ASP.Net. When i try this in local machine it works fine. But when deploying in the server the printer settings are not valid.
//Code
if (m_streams == null || m_streams.Count == 0)
throw new Exception("Error: no stream to print.");
PrintDocument printDoc = new PrintDocument();
// printDoc.PrinterSettings.PrinterName = "HP LaserJet 3055 PCL5";
if (!printDoc.PrinterSettings.IsValid)
{
throw new Exception("Error: cannot find the default printer.");
}
else
{
printDoc.PrintPage += new PrintPageEventHandler(PrintPage);
m_currentPageIndex = 0;
printDoc.Print();
}
From the above code it throws me an exception "cannot find default printer". Tried to add the printer name which exists in the client machine but still didn't work.
I need to print the report in the client machine.
Don't know where to change. Any help?
I belive that you are doing something you arent' supposed to do.
If you have a ASP.NET Website, that has a RDLC report in the site. If I, access that report, I would like to print in my machine, not in the server. Are you sure, that you want to make the server to print the report? Shouldn't the client/browser to issue the print command?
If your server is in a datacenter, and I'm at my home, where should the printing be done? Unless you want to print in the server (Mail Merge stickers, for example).
As far as I know it is not possible to access the client's printer settings. What you are doing is access the servers printer, which, if I am right, is not installed.
You can print accesing browser/ javascript functionality by calling window.print() in javascript,because the browser is running client-side and has access to the printers. But accessing the clients system resources directly would be a huge security flaw.
Maybe there is an obscure workaround/hack, but it would be a bad solution
I am making one application where user will print invoices which I am displaying using Crystal Report.
The user showed me his current application made using ForPro. In that application, under Printer Options form, one can see all the printers currently installed and the user could select default printer. When the invoice is made, the user presses the print button, then there is one dialog asking for no. of copies. When it's entered, the invoice gets printed directly, without any Print Dialog Box. If the user wants to change the printer again he/she will change it in the Printer Option form.
I want to know if similar thing is possible in Crystal Report and need guidance on how to approach for it.
Take a look at the ReportDocument.PrintToPrinter SAP Docs or MSDN Docs for how to specify the PrinterName and then Print using the ReportDocument object.
If you can try and get away from how the FoxPro app UI for printer selection. Instead use the standard print dialog box to select the printer.
You should note that if you don't set the PrinterName before sending the report to the printer it will use the default on the crystal file. Not to be confused with the user's OS default printer.
Here's an example of showing the PrintDialog settings some parameters using the SetParameterValue method and then sending the report document to a printer
// Note: untested
var dialog = new PrintDialog();
Nullable<bool> print = dialog.ShowDialog();
if (print.HasValue && print.Value)
{
var rd = new ReportDocument();
rd.Load("ReportFile.rpt");
rd.SetParameter("Parameter1", "abc");
rd.SetParameter("Parameter2", "foo");
rd.PrintOptions.PrinterName = dialog.PrinterSettings.PrinterName;
rd.PrintToPrinter(1, false, 0, 0);
}
The code above no longer works as advertised which has been admitted by SAP
You need to set the report document to an ISCDReportClientDocument and then print it. This is a more robust way of making sure the print job doesn't go to the default printer. The last two lines can be replaced with this code.
CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions printReportOptions = new CrystalDecisions.ReportAppServer.Controllers.PrintReportOptions();
CrystalDecisions.ReportAppServer.Controllers.PrintOutputController printOutputController = new CrystalDecisions.ReportAppServer.Controllers.PrintOutputController();
CrystalDecisions.ReportAppServer.ClientDoc.ISCDReportClientDocument rptClientDoc;
rptClientDoc = cryRtp.ReportClientDocument;
printReportOptions.PrinterName = pDialog.PrinterSettings.PrinterName;
rptClientDoc.PrintOutputController.PrintReport(printReportOptions);
Here is another good link
http://mattruma.azurewebsites.net/?p=258