UWInfo Blog
發表新文章
[Join] | [忘記密碼] | [Login]
搜尋

搜尋意見
文章分類-#Author#
[所有文章分類]
所有文章分類
  • ASP.NET (48)
  • ASP.NET2.0 (15)
  • ASP.NET4.0 (34)
  • JavaScript (49)
  • jQuery (26)
  • FireFox (4)
  • UW系統設定 (3)
  • SQL (39)
  • SQL 2008 (25)
  • mirror (4)
  • SVN (4)
  • IE (9)
  • IIS (20)
  • IIS6 (1)
  • 閒聊 (7)
  • W3C (6)
  • 作業系統 (9)
  • C# (24)
  • CSS (12)
  • FileServer (1)
  • HTML 5 (11)
  • CKEditor (3)
  • UW.dll (13)
  • Visual Studio (16)
  • Browser (8)
  • SEO (1)
  • Google Apps (3)
  • 網站輔助系統 (4)
  • DNS (5)
  • SMTP (4)
  • 網管 (11)
  • 社群API (3)
  • SSL (4)
  • App_Inventor (1)
  • URLRewrite (2)
  • 開發工具 (6)
  • JSON (1)
  • Excel2007 (1)
  • 試題 (3)
  • LINQ (1)
  • bootstrap (0)
  • Vue (3)
  • IIS7 (3)
  • foodpanda (2)
  • 編碼 (2)
  • 資安 (3)
  • Sourcetree (1)
  • MAUI (1)
  • CMD (1)
  • my sql (1)
最新回應
  • Newtonsoft.Json.JsonConvert.DeserializeObject 失敗的情況
    test...more
  • dotnet ef dbcontext scaffold
    ...more
  • [ASP.NET] 利用 aspnet_regiis 加密 web.config
    ...more
  • IIS ARR (reverse proxy) 服務安裝
    ...more
  • [錯誤訊息] 請加入 ScriptResourceMapping 命名的 jquery (區分大小寫)
    ...more
  • 用 Javascript 跨網頁讀取 cookie (Cookie cross page, path of cookie)
    ...more
  • 線上客服 - MSN
    本人信箱被盜用以致資料外洩,是否可以請貴平台予以協助刪除該信箱之使用謝謝囉...more
  • 插入文字到游標或選取處
    aaaaa...more
  • IIS 配合 AD (Active Directory) 認証, 使用 .Net 6.0
    太感謝你了~~~你救了我被windows 認證卡了好幾天QQ...more
  • PostgreSQL 的 monitor trigger
    FOR EACH ROW 可能要改為 FOR EACH STATEMENT ...more
標籤
  • 起始不標示為安全的a
  • sql
  • 光
  • -5083
  • Su
  • 590
  • dotnet
  • 1137
  • 9247
  • Message[t]
  • 10
  • ie
  • windows
  • cBbJFYKo
  • 403.18
  • 網站名稱
  • [U2]
  • sqlbuilder
  • 54
  • nelson
  • 824
  • List
  • 8CRLF-Head
  • fortigate
  • FireFox
  • 3644
  • sing
  • ctrl
  • 620
  • Config
  • josn
  • 問題
  • vb
  • -2481 sceo
  • Rf
  • 220
  • Lucene .Ne
  • 定位
  • machinekey
  • 4027
  • 600
  • 346
  • XmlBuilder
  • 悟
  • 612
  • sa
  • ti
  • https
  • inertsql
  • net
搜尋 print[t] 結果:
PrintDocument 另存成 PDF
如果列印主機是 windows server 主機,通常會有 "Microsoft Print to PDF" 名稱的印表機
那使用 Print Document 物件另存成 PDF code 如下
pd.PrinterSettings.PrinterName = "Microsoft Print to PDF";
pd.PrinterSettings.PrintToFile = true;
pd.PrinterSettings.PrintFileName = @"C:\TEMP\XPS\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
More...
darren, 2025/5/12 下午 04:36:12
Chrome 連接藍芽 BLE 印表機(出單機),使用 ESC / POS 列印繁體中文和 Qrcode
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>BLE Printer Test</title>
    <script src="https://cdn.jsdelivr.net/npm/iconv-lite-umd@0.6.10/lib/iconv-lite-umd.js"></script>
</head>
<body>
    <h1>BLE Printer Test 123</h1>
    <button id="printTestButton">Test Print</button>
    <button id="printAgain">Print Again</button>
    <pre id="log"></pre>
    <script>
        // Function to log messages on the page
        function logMessage(message) {
            const logElement = document.getElementById('log');
            logElement.textContent += message + '\n';
        }
        var device;
        var server;
        var service;
        var characteristic;
        var name = 'T58_6752'; // 藍芽設備的名稱
        var uuid = 0x1800; //service UUID (用 BLE Scanner 找到的)
        var characteristicUuid = 0x2A00; // characteristic UUID (用 BLE Scanner 找到的)
        // Function to connect to BLE printer and send test data

        async function connectAndTestPrint() {

            try {
                logMessage("Requesting Bluetooth device...");

                // Request the BLE device
                device = await navigator.bluetooth.requestDevice({
                    //acceptAllDevices: true,
                    filters: [
                        { name: name }
                    ],
                    optionalServices: [uuid] // Replace with the correct service UUID
                });

                logMessage(`Device selected: ${device.name}`);
                logMessage(`Device uuid: ${device.id}`);

                await printAgain();


            } catch (error) {
                logMessage(`Error: ${error.message}`);
            }
        }

        // 转码方法
        function stringToGbk(str) {
            const ranges = [
                [0xA1, 0xA9, 0xA1, 0xFE],
                [0xB0, 0xF7, 0xA1, 0xFE],
                [0x81, 0xA0, 0x40, 0xFE],
                [0xAA, 0xFE, 0x40, 0xA0],
                [0xA8, 0xA9, 0x40, 0xA0],
                [0xAA, 0xAF, 0xA1, 0xFE],
                [0xF8, 0xFE, 0xA1, 0xFE],
                [0xA1, 0xA7, 0x40, 0xA0],
            ]
            const codes = new Uint16Array(23940)
            let i = 0

            for (const [b1Begin, b1End, b2Begin, b2End] of ranges) {
                for (let b2 = b2Begin; b2 <= b2End; b2++) {
                    if (b2 !== 0x7F) {
                        for (let b1 = b1Begin; b1 <= b1End; b1++) {
                            codes[i++] = b2 << 8 | b1
                        }
                    }
                }
            }
            const cstr = new TextDecoder('gbk').decode(codes)

            // 编码表
            const table = new Uint16Array(65536)
            for (let i = 0; i < cstr.length; i++) {
                table[cstr.charCodeAt(i)] = codes[i]
            }
            const buf = new Uint8Array(str.length * 2)
            let n = 0

            for (let i = 0; i < str.length; i++) {
                const code = str.charCodeAt(i)
                if (code < 0x80) {
                    buf[n++] = code
                } else {
                    const gbk = table[code]
                    buf[n++] = gbk & 0xFF
                    buf[n++] = gbk >> 8
                }
            }
            u8buf = buf.subarray(0, n)
            // console.log(u8buf);
            return u8buf
        }

        async function printAgain() {
            // Connect to the GATT server
            server = await device.gatt.connect();
            logMessage("Connected to GATT server.");

            // Get the printer service
            service = await server.getPrimaryService(uuid); // Replace with your printer's service UUID
            logMessage("Printer service retrieved.");

            // Get the characteristic for writing data
            characteristic = await service.getCharacteristic(characteristicUuid); // Replace with the correct characteristic UUID
            logMessage("Printer characteristic retrieved.");

            // Prepare test print data
            const encoder = new TextEncoder();
            const testData = encoder.encode("TEST PRINT: Hello from Web Bluetooth!\n");
            const finalData = encoder.encode("--\n--\n \n \n");

            const setFontSize = new Uint8Array([0x1D, 0x21, 0x11]); // GS ! n
            const setFontSize2 = new Uint8Array([0x1D, 0x21, 0x22]); // GS ! n
            const setFontSize3 = new Uint8Array([0x1D, 0x21, 0x33]); // GS ! n

            // Write test data to the printer
            logMessage("Sending test data to printer...");

            await characteristic.writeValue(new Uint8Array([0x1D, 0x21, 0x00]));
            await characteristic.writeValue(encoder.encode("1x1!\n"));
            await characteristic.writeValue(setFontSize);
            await characteristic.writeValue(encoder.encode("2x2!\n"));
            await characteristic.writeValue(setFontSize2);
            await characteristic.writeValue(encoder.encode("3x3!\n"));
            await characteristic.writeValue(setFontSize3);
            await characteristic.writeValue(encoder.encode("4x4!\n"));


            const initPrinter = new Uint8Array([0x1B, 0x40]); // ESC @
            await characteristic.writeValue(initPrinter);

            // 3. 設置字符集為 GBK
            const setGBK = new Uint8Array([0x1B, 0x74, 0x11]); // ESC t 0x11 (GBK)
            await characteristic.writeValue(setGBK);

            const text = "繁體中文測試\n \n";
            const encodedText = stringToGbk(text);
            await characteristic.writeValue(encodedText);
            logMessage("Test data sent successfully!");

            // QrCode 列印
            const qrData = "https://example.com"; // Your QR code data
            const qrDataLength = qrData.length + 3;
            const pL = qrDataLength & 0xFF; // Low byte
            const pH = (qrDataLength >> 8) & 0xFF; // High byte

            const commands = [
                0x1B, 0x40, // Initialize printer
                0x1D, 0x28, 0x6B, pL, pH, 0x31, 0x50, 0x30, ...new TextEncoder().encode(qrData), // Store data
                0x1D, 0x28, 0x6B, 0x03, 0x00, 0x31, 0x51, 0x30 // Print QR code
            ];

            const buffer = new Uint8Array(commands);
            await characteristic.writeValue(buffer);
            logMessage("QrCode sent successfully!");

            await characteristic.writeValue(finalData);
            logMessage("finalData sent successfully!");

            // Disconnect the GATT server
            server.disconnect();
            logMessage("Disconnected from printer.");
        }

        // Bind the function to the button
        document.getElementById('printTestButton').addEventListener('click', connectAndTestPrint);
        document.getElementById('printAgain').addEventListener('click', printAgain);
    </script>
</body>
</html>
More...
Bike, 2025/1/2 下午 02:20:15
在列印前把列印的內容使用圖檔存備份
主要是使用 Printing.PreviewPrintController 的產生預覽圖片的功能來儲存列印內容, 再把圖片送到印表機, 以保証列印內容和儲存的圖檔是一致的. 值得一提的是, 在預覽時可以使用不同的印表機, 以便讓產生較小的圖檔, 否則有時會發生 GDI+ 的錯誤.

Sample 如下:

Imports System.Drawing.Printing

Namespace X
    Partial Public Class PrintSomething
        Dim PD As PrintDocument

        Dim ppc As New Printing.PreviewPrintController()

        Dim TotalPage As Int32 = 0

        ''' <summary>
        ''' 第幾次列印, 用來控制寫出的圖檔
        ''' </summary>
        ''' <remarks></remarks>
        Dim PrintCount As Int32 = 1

        Dim PageCount As Int32 = 1

            :
            :
            :
      
        Sub New(ByVal oPM As DB.PackingListMain, ByVal dtDetail As DataTable, dtUsedCoupon As DataTable)
            '初始化資料
            :
            :
            :
        End Sub

        Sub InitPrintDocument()
            '在這裡設定 PrintDocument 的紙張, 印表機, 邊界, 橫印或直印

            PD = New PrintDocument

            :
            :
            :

        End Sub

        Sub print()
            InitPrintDocument()

            PD.PrintController = ppc

           
            ' 第一次先把列印結果存在 Printing.PreviewPrintController 中
            AddHandler PD.PrintPage, AddressOf GeneratePreview

            '再來儲存圖片
            AddHandler PD.EndPrint, AddressOf SaveImage

            PD.Print()
        End Sub


        Dim ppi() As Printing.PreviewPageInfo
        Dim PrintPage As Int32 = 0

        Sub SaveImage(ByVal sender As Object, ByVal ev As PrintEventArgs)

            PrintDebug("Save Image Start, " & Now.ToString("yyyy-MM-dd HH:mm:ss.fff"))

            '把圖檔存起來
            Dim TF As String = Now.ToString("yyyyMMddHHmmss") ' DB.SysConfig.Path.LocalDataRoot
            Dim PathLocal As String = "C:\PrintLog\" & TF.Substring(0, 4) & "-" & TF.Substring(4, 2) & "\"
            Dim PathPackingListSheetImage As String = DB.SysConfig.PackingListSheetImage & TF.Substring(0, 4) & "-" & TF.Substring(4, 2) & "\"

            System.IO.Directory.CreateDirectory(PathLocal)
            System.IO.Directory.CreateDirectory(PathPackingListSheetImage)

            Dim LatestImages As String = ""
            ppi = ppc.GetPreviewPageInfo()

            For x As Integer = 0 To ppi.Length - 1
                Dim Filename As String = TF & "_" & Me.oPM.Id & "_" & x & ".png"

                ppi(x).Image.Save(PathLocal & Filename, System.Drawing.Imaging.ImageFormat.Png)
            Next


            '把圖檔印出來
            For x As Integer = 0 To ppi.Length - 1
  '建立新的 PrintDocument
                InitPrintDocument()

                If CST.WebConfig.Server_NAME = "測試主機" Then
                    UW.WU.DebugWriteLine("PrinterName: " & Me.PD.PrinterSettings.PrinterName)
                    UW.WU.DebugWriteLine("PaperSize: " & PD.DefaultPageSettings.PaperSize.PaperName)

                    PD.DocumentName = "PackingList"
                    PD.PrinterSettings.PrintFileName = "C:\PDF\PackingList" & Now.ToString("yyyyMMddHHmmss") & "_" & PrintPage & ".prn"
                    PD.PrinterSettings.PrintToFile = True

                    'PD.PrinterSettings.
                End If

                '很奇怪, 這裡不會分頁, 所以要一張一張印   
                AddHandler PD.PrintPage, AddressOf pd_PrintPage2
                PD.Print()


               PrintPage += 1

            Next
        End Sub

        Private Sub pd_PrintPage2(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
            '很奇怪, 這裡不會分頁, 所以要一張一張印           
            ev.Graphics.DrawImage(ppi(PrintPage).Image, 0, 0)
            ev.HasMorePages = False
        End Sub


        Private Sub GeneratePreview(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
            ' 列印內容
        End Sub

        End Class
    End Class

End Namespace

More...
Bike, 2012/5/3 下午 04:00:38
~ Uwinfo ~