繁体   English   中英

从网站/网络应用程序打印到标签打印机? [关闭]

[英]Print to a label printer from a web site / web application? [closed]

是否有任何已知的标签打印机可以接受来自网站或 Web 应用程序的打印命令?

具体来说,Dymo、Brother、Zebra 等一次性标签打印机。

有没有人成功打印到这些打印机(不使用 Internet Explorer-Only hack 或 ActiveX 控件)

我认为,随着我们进入一个更加以浏览器为中心的世界,最终必须有一个解决方案。

哇。 我知道这是 4 年前提出的问题,但是为了寻找一种从网络应用程序打印标签的强大方法,我花了一周的大部分时间,我不得不在这里发声。

这是我发现的:

  • DYMO 似乎是最有可能追求卓越的人选。 但不,事实证明它只能从(a)它自己的应用程序,或(b)使用它的 SDK 的东西打印。 DYMO SDK 使用的 XML 绘图模型既过于复杂,又在布局和样式方面受到限制。 文档分散且难以理解(例如,常见标签大小的<Bounds>值是多少?在任何地方都没有标签参数的描述!)令人沮丧,令人失望。

  • QZ(是jzebra) ,从而使浏览器打印所有说话EPL,ZPL,FGL,ESC / POS,EPCL和CPCL ......其中包括斑马系列设备。 它需要大量集成(在标签打印机所连接的设备上运行 Web 服务器),但它可以工作。

  • 半岛有一个精心设计的3rd 方应用程序,它适用于 DYMO(以及其他),但需要从浏览器打印到 PDF 的中间步骤。 他们还假设您永远不会将想要打印的内容缩小到 70% 以下,这是不正确的。

OP 说:“我认为,随着我们进入一个更加以浏览器为中心的世界,最终必须有一个解决方案。” 四年后,我会更进一步,并建议任何不能从浏览器打印(或者只是像普通打印机一样使用小纸)的标签打印机都远远没有发挥其潜力。 HTML+CSS 中的布局非常简单。 浏览器可以完美地解析它,并以任何分辨率将其渲染到任何输出设备。 看起来太明显了。

如果有人知道从浏览器打印的热敏标签打印机,而不是将您囚禁在过时的集成方法中,我非常想知道!

Dymo 打印机有一个浏览器插件,可让您从javascript打印。 非常容易使用。

几年后重温这个问题。

联网的 Zebra 打印机易于打印。 在一个项目中,我让 web 应用程序打开一个连接到打印机的套接字并在 ZPL 中提供指令。

您还可以尝试寻找提供云打印解决方案的PrintNode ,这意味着您可以通过互联网直接打印到任何打印机。

跨浏览器/平台兼容的网页/网页脚本没有将打印机语言命令(例如 Zebra 打印机语言 [ZPL])传输到打印机所需的低级访问权限。 为此,需要一个 ActiveX 控件/浏览器插件/小程序/类似位的可执行代码。 此限制不是特定于打印机的,来自浏览器而不是打印机制造商。

但是,许多标签打印机允许您像普通打印机一样使用它们进行打印——只是打印到非常小的纸张上的打印机。 您可以使用 HTML/CSS 布局标签。 显示结果时,请指导用户在单击打印之前转到“文件 > 打印”并选择他们的标签打印机。

希望这有帮助,

Zebra 现在有一个名为“BrowserPrint”的工具,它的工作方式类似于 PrintNode,专门用于 Zebra 打印机。 (与 Printnode 不同,它可以免费与 Zebra 打印机一起使用)

一些打印机也有 ppd 文件可用和/或可以在 linux 平台系统上的 cups 中配置,或者也可以通过 linux 连接和说话。 (这意味着通过构建一个简单的服务器守护进程或在 linux 机器上设置类似 apache 或 lighttpd 之类的东西,甚至是连接到打印机)

您可以从签名的 Java 小程序或从Java Web Start应用程序打印:

http://java.sun.com/javase/technologies/desktop/printing/

如果您格式化打印输出以使其适合标签,那应该可行。

请注意,大多数现代浏览器都限制对 Java 小程序的支持,因此您可能会遇到小程序的问题,具体取决于所使用的浏览器。 特别是,谷歌浏览器将在 2015 年 9 月停止支持 Java 小程序 这些限制并不适用于Java Web Start的,应继续工作。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Management;
using System.Reflection;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        GetAllPrinterList();
        this.textBox1.Attributes.Add(
    "onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");
        this.lstPrinterList.Attributes.Add(
    "onkeypress", "button_click(this,'" + this.Button1.ClientID + "')");

    }

    private void GetAllPrinterList()
    {
        //ManagementScope objScope = new ManagementScope(ManagementPath.DefaultPath); //For the local Access
        //objScope.Connect();

        //SelectQuery selectQuery = new SelectQuery();
        //selectQuery.QueryString = "Select * from win32_Printer";
        //ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
        //ManagementObjectCollection MOC = MOS.Get();
        //foreach (ManagementObject mo in MOC)
        //{
        //    lstPrinterList.Items.Add(mo["Name"].ToString());
        //    //lstPrinterList.Items.Add(new ListItem(mo["Name"].ToString()));

        //}

        lstPrinterList.Items.Add("\\\\10.32.65.6\\Parcel-Zebra-FDX ZM400");
        lstPrinterList.Items.Add("\\\\10.32.65.4\\Singles_Station_Z_Printer");
        lstPrinterList.Items.Add("\\\\10.32.65.12\\ZebraZ4M-Packaging");
        if (lstPrinterList.Items.Count > 3)
        {
        lstPrinterList.Items.RemoveAt(5);
        lstPrinterList.Items.RemoveAt(4);
        lstPrinterList.Items.RemoveAt(3);

        }

        //LocalPrintServer printServer = new LocalPrintServer();
        //string printer;
        //printer = LocalPrintServer.DefaultPrintQueue;
        //System.Management.ObjectQuery oquery =
        //        new System.Management.ObjectQuery("SELECT * FROM Win32_Printer");

        //    System.Management.ManagementObjectSearcher mosearcher =
        //        new System.Management.ManagementObjectSearcher(oquery);

        //    System.Management.ManagementObjectCollection moc = mosearcher.Get();

        //    foreach (ManagementObject mo in moc)
        //    {
        //        System.Management.PropertyDataCollection pdc = mo.Properties;
        //        foreach (System.Management.PropertyData pd in pdc)
        //        {
        //           // if ((bool)mo["Network"])
        //           // {
        //                lstPrinterList.Items.Add(mo["Name"].ToString());
        //           // }
        //        }
        //    }

        //}
        //        using (var printServer = new PrintServer(string.Format(@"\\{0}", PrinterServerName)))
        //{
        //    foreach (var queue in printServer.GetPrintQueues())
        //    {
        //        if (!queue.IsShared)
        //        {
        //            continue;
        //        }
        //        Debug.WriteLine(queue.Name);
        //     }
        // }
        //foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
        //{
        //MessageBox.Show(printer);
        //System.Web.UI.WebControls.ListBox lstPrinterList = new System.Web.UI.WebControls.ListBox();
        //System.Web.UI.WebControls.ListBox lstPrinterList = (System.Web.UI.WebControls.ListBox)Page.FindControl("lstPrinterList");
        //lstPrinterList.Text = "Zebra 110PAX4 (203 ;dpi)";

        //lstPrinterList.Items.Add(printer.ToString());
        //lstPrinterList.Items.Add(new ListItem(printer));
        //lstPrinterList.Items.Add(printer);
        //System.Web.UI.WebControls.ListBox lstPrinterList = (System.Web.UI.WebControls.ListBox)Page.FindControl("ListBox1");
        //Zebra 110PAX4 (203 dpi)
        //lstPrinterList.Items.Insert(printer);
        //}
    }


    //private void lstPrinterList_OnClick(object sender, System.EventArgs e)
    //{
    //    // Get the currently selected item in the ListBox.
    //    string curItem = lstPrinterList.SelectedItem.ToString();

    //    // Find the string in ListBox2.
    //    int index = lstPrinterList.DataTextField(curItem);
    //    //int index = lstPrinterList.FindString(curItem);
    //    //lstPrinterList.DataTextField(curItem);
    //    // If the item was not found in ListBox 2 display a message box, otherwise select it in ListBox2.
    //    if (index == -1)
    //        MessageBox.Show("Item is not available in ListBox2");
    //    else
    //        lstPrinterList.SetSelected(index, true);
    //}

    //private void button1_Click(object sender, System.EventArgs e)
    //{

    //    string str = File.ReadAllText("lpn.prn");
    //    str = str.Replace("BOX_TYPE", "boom");
    //    str = str.Replace("11111111", textBox1.Text);
    //    File.WriteAllText("lpn.prn", str);
    //            // Print the file to the printer.
    // RawPrinterHelper.SendFileToPrinter("\\\\Zebra-FDX ZM400 200 dpi (ZPL)", "C:\\Users\\Administrator\\Documents\\Visual Studio 2015\\Projects\\WindowsFormsApplication4\\Prn Print\\bin\\Debug\\lpn.prn");  
    //}

    public void button1_Click(object sender, System.EventArgs e)
    {
        String prnFile = "lpn2.prn";
        string s = File.ReadAllText(prnFile);
        string printer;
        String Printer = lstPrinterList.SelectedItem.ToString();
        s = s.Replace("*description*", "snuffles");
        s = s.Replace("*barcode*", textBox1.Text);
        //File.WriteAllText("PPlpn.prn", s);
        //s = "^XA^LH30,30\n^FO20,10^ADN,90,50^AD^FDHello World^FS\n^XZ";
        printer = lstPrinterList.SelectedItem.Value;
        PrintDialog pd = new PrintDialog();
        pd.PrinterSettings = new PrinterSettings();
        RawPrinterHelper.SendStringToPrinter(printer, s); 
        //RawPrinterHelper.SendStringToPrinter(Printer, s);
        //Response.Redirect(Request.RawUrl.Replace(Request.Url.Query, "?__VIEWSTATE=%2%3D&ctl00%24MainContent%24textBox1=bp300&ctl00%24MainContent%24lstPrinterList=Shipping+Printer"));




    }
    protected void textBox1_TextChanged(object sender, EventArgs e)
    {

    }
}




public class RawPrinterHelper
{
    // Structure and API declarions:
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
    public class DOCINFOA
    {
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDocName;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pOutputFile;
        [MarshalAs(UnmanagedType.LPStr)]
        public string pDataType;
    }
    [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

    [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool ClosePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

    [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndDocPrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool StartPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool EndPagePrinter(IntPtr hPrinter);

    [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
    public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

    // SendBytesToPrinter()
    // When the function is given a printer name and an unmanaged array
    // of bytes, the function sends those bytes to the print queue.
    // Returns true on success, false on failure.
    public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
    {
        Int32 dwError = 0, dwWritten = 0;
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool bSuccess = false; // Assume failure unless you specifically succeed.
        di.pDocName = "My C#.NET RAW Document";
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (bSuccess == false)
        {
            dwError = Marshal.GetLastWin32Error();
        }
        return bSuccess;
    }

    public static bool SendFileToPrinter(string szPrinterName, string szFileName)
    {
        // Open the file.
        FileStream fs = new FileStream(szFileName, FileMode.Open);
        // Create a BinaryReader on the file.
        BinaryReader br = new BinaryReader(fs);
        // Dim an array of bytes big enough to hold the file's contents.
        Byte[] bytes = new Byte[fs.Length];
        bool bSuccess = false;
        // Your unmanaged pointer.
        IntPtr pUnmanagedBytes = new IntPtr(0);
        int nLength;

        nLength = Convert.ToInt32(fs.Length);
        // Read the contents of the file into the array.
        bytes = br.ReadBytes(nLength);
        // Allocate some unmanaged memory for those bytes.
        pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
        // Copy the managed byte array into the unmanaged array.
        Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
        // Send the unmanaged bytes to the printer.
        bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
        // Free the unmanaged memory that you allocated earlier.
        Marshal.FreeCoTaskMem(pUnmanagedBytes);
        return bSuccess;
    }

    public static bool SendStringToPrinter(string szPrinterName, string szString)
    {
        IntPtr pBytes;
        Int32 dwCount;
        // How many characters are in the string?
        dwCount = szString.Length;
        // Assume that the printer is expecting ANSI text, and then convert
        // the string to ANSI text.
        pBytes = Marshal.StringToCoTaskMemAnsi(szString);
        // Send the converted ANSI string to the printer.
        SendBytesToPrinter(szPrinterName, pBytes, dwCount);
        Marshal.FreeCoTaskMem(pBytes);
        return true;
    }
}

这是我在 C# 中创建的,效果很好。 这是一个网络应用程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM