6

As colleagues of web development, I guess they all have a common trouble. Why can't the web end be able to print and preview directly like the CS end? Isn't it enough to just remove the print preview interface?

The reality is that the Web terminal is limited by the browser's authority and cannot directly access local resources such as printers. Therefore, the realization of no preview and printing on the Web is not a simple problem, but a problem of breaking through the authority and breaking through the platform. This causes the user to click at least two buttons to complete the printing when printing the report. If it is a scene that requires batch printing, the user needs to click the button multiple times, which is very troublesome.

Party A who doesn't know the details of the situation will be very puzzled. For such a simple request, it only saves a click of an operation step. Why can't you do it?

This kind of torture of our humble soul to Party B also gives us a headache. It is not that we can't do it, but objective conditions don't allow us to do it.

As a Wyn Enterprise (pronounced: One) embedded BI and ActiveReports report controls, the customers who have contacted in the past two years basically no longer use Winform or WPF, and most of the projects are migrated to On the B/S side, the front-end and back-end separation architecture, or MVC, is adopted.

In this case, customers will be faced with the ability to print but cannot directly connect to the printer for default printing. If it is an enterprise in the logistics industry or a manufacturing enterprise, web-side printing is a batch operation, such as a logistics order, which prints thousands of copies each time. If the print preview interface pops up every time you print a sheet, it may not take long. We The lovely party A will blow up our phone.

In such a severe situation, and many users are chasing this question, the realization of the function of silent printing on the Web is also very urgent.

Now most of the printing process is basically exporting the content that needs to be printed as a PDF file, and then calling the browser's print preview to print. Now that we have the first step, there must be a way to directly realize silent printing. The following is the content that I have searched the entire network and found to be more appropriate. Now I will share the results with you.

There are two realization ideas:

  1. Set up the browser
  2. Install plug-ins, use proxy software

Not much to say, just dry the goods, look at the test results.

Set up the browser

Where there are restrictions, there will be needs. Since the browser is restricted when printing, there must be other users who have the same needs as ours. The method is always more difficult than the difficulty. After searching, we found that there is a solution in the settings of Chrome and Firefox browsers. This method looks decent and has a lot of likes:

The general content is:

  1. Enter Chrome's advanced settings and set the default homepage of the browser as the page that our web application needs to print silently. Following the introduction, we also got into operation.
  • Click Settings
  • Appearance-Set to display the home button and enter the web page we want to visit.

  • Open the desktop shortcut, modify and enter --kiosk --kiosk-printing in the suffix.

But at this point, the road is nowhere to go. The system will keep popping up the following error and cannot be modified.

Let's try another browser.

  1. Modification-Firefox
  2. Enter in the browser: about:config
  3. Enter print.always\_print\_silent in the input box

And set to Boolean type, dot + sign

  1. Restart-test print

At this time, the system will still pop up the print preview dialog box, and this method is still invalid.

Use proxy software

JSPrint

The usage method is simple. Firstly, use the Demo website to call the JSPrintManager printing excuse interface, and then use the client to act as a proxy to realize printing. The supported platforms are: windows 10, IE, Edge, Firefox, Chrome, Safari.

Here are some commonly used features.

Use the user to select the printer code example:

const cpj = new JSPM.ClientPrintJob();

cpj.clientPrinter =  new JSPM.UserSelectedPrinter();

const printFile = new JSPM.PrintFilePDF(fileUrl, JSPM.FileSourceType.URL, fileName, copiesNumber);

cpj.files.push(printFile);

await cpj.sendToClient();

The print dialog will appear

Use default printer

const cpj = new JSPM.ClientPrintJob();

cpj.clientPrinter =  new JSPM.DefaultPrinter();

const printFile = new JSPM.PrintFilePDF(fileUrl, JSPM.FileSourceType.URL, fileName, copiesNumber);

cpj.files.push(printFile);

await cpj.sendToClient();

Use installed printer

const cpj = new JSPM.ClientPrintJob();

cpj.clientPrinter =  new JSPM.InstalledPrinter('Microsoft Print to PDF);

const printFile = new JSPM.PrintFilePDF(fileUrl, JSPM.FileSourceType.URL, fileName, copiesNumber);

cpj.files.push(printFile);

await cpj.sendToClient();

Get a list of printers:

const printersInfo = await JSPM.JSPrintManager.getPrintersInfo();

console.log(printersInfo);

JSPrintManager is not difficult to use, but there are some obvious technical limitations in its use. During the test, we found that due to unknown factors, the old example will not work, and we need to create a new example to complete the silent printing call of JSPrint; in addition, this method cannot be executed normally in the Firefox browser. . Note that if you are a Mac OS, once you try JSPrintManager, you cannot uninstall it.

Wyn Enterpriese

realizes the integrated functions of online report design, preview and silent printing. Wyn Enterprise V5.0 Update1 brings a breakthrough report printing-silent printing.

sample download:

https://gcdn.grapecity.com.cn/forum.php?mod=attachment&aid=MTgzNDk3fDg2YWRhNTgyfDE2NDA1OTEzMzd8MjkzODJ8MTM2MTU1

operation steps:

1. Start the print proxy service (the print proxy is done using PowerShell scripts)

The included files are as follows:


Environmental resources necessary to start the agent:
1. [Visual Studio 2022] ( https://visualstudio.microsoft.com/vs/ ) 17.0 and above (required to compile the example)
2、【.NET 6.0 SDK】 (https://www.microsoft.com/net/download)
3. [.NET Core Hosting Bundle] ( https://docs.microsoft.com/en-us ... view=aspnetcore-5.0 ) (IIS deployment environment)

Resource file introduction:

./switchPrinter.ps1   切换默认的打印机资源

`./start.ps1`  启动和安装打印服务

./stop.ps1  停止服务
 ./build.ps1  如果修改了 src文件夹下的文件,需要启动该脚本,进行重新编译,编译的项目会放置在./PrintAgent 文件夹中
 ./debug.ps1 如果修改了src文件夹,可以使用该脚本进行调试。

starts the proxy server:
After downloading the example, open the powershell tool with administrator privileges and execute the following command first:

  • Execute the command Set-ExecutionPolicy Bypass -Scope CurrentUser -Force to enable the script execution function

  • Call the ./switchPrinter.ps1 command to select the default printer

  • Use the ./start.ps1 command to start the service, if it is over, call ./stop.ps1 to close the case

2. After the startup is successful, let's run the WynReportDesigner or WynViewer example:
Executing these examples will automatically load into the default printer options. Realize printing needs.

This sample agent uses the internal PDF printing library of Grape City for silent printing. You can start the Agent through the attached powerhellpowershell script and register it to run automatically on Windows 10.

After the configuration is successful, you can see that after clicking the print button, the background will connect to the default printer for printing.

Summarize

There is no shortcut to solve silent printing. This method of setting the browser is very convenient, but the result is no effect. At this time, the use of an agent is an effective method, but the ready-made wisdom often requires some knowledge to pay. Anyway, the method is always more difficult than the difficulty. If you encounter similar needs, you don't have to worry, now there are effective methods to solve the problem on the market. To solve this problem.


葡萄城技术团队
2.7k 声望29.3k 粉丝

葡萄城是专业的软件开发技术和低代码平台提供商,聚焦软件开发技术,以“赋能开发者”为使命,致力于通过表格控件、低代码和BI等各类软件开发工具和服务,一站式满足开发者需求,帮助企业提升开发效率并创新开发模式。