您如何使用 getHtmlPrintDocumentSourceAsync 在 HTML/JavaScript Windows Store 应用程序中进行打印?

How do you use getHtmlPrintDocumentSourceAsync to print in HTML/JavaScript Windows Store apps?

我非常简单地尝试使用 HTML 和 JavaScript/WinJS 在 Windows 10 应用程序(通用)中打印一些内容。

文档的

ALLMSApp 上有一个名为 getHtmlPrintDocumentSource 的函数。

我没有这个,我似乎也找不到任何相关来源来查看它是否已被移动。相反,我有 getHtmlPrintDocumentSourceAsync。这似乎是前者的替代品,但我无法让它工作,据我所知,它的文档为零。

当我 运行 下面的代码(基于文档但更新为异步)时:

function onPrintTaskRequested(printEvent) {
    var printTask = printEvent.request.createPrintTask("Print Sample", function (args) {
        MSApp.getHtmlPrintDocumentSourceAsync(document)
            .then(function(result) {
                args.setSource(result);
            });

        printTask.oncompleted = onPrintTaskCompleted;
    });
}

result 填充了一些我期望的打印设置,但是 content 属性 设置为 0,我猜这是问题。我真的不能确定,因为没有关于此功能的文档。我什至无法 运行 文档中使用 `getHtmlPrintDocumentSource' 的数十个示例代码中的任何一个,因为它似乎不再存在了。

除了将 document 发送到 Async 方法之外,我还尝试了几种不同的创建文档片段的变体。相同的结果。

可能不是很有帮助,但执行上述代码时打开的 Windows 打印对话框中的消息是:"Nothing was sent to print. Open a document and print again."

有什么想法吗?

getHtmlPrintDocumentSource 是在 Windows 10 个应用中同步弃用的 API。我们将处理 Windows 8 和 8.1 留下的一些文档来澄清这一点。

查看 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing/js 以了解如何在 JavaScript 中使用 getHtmlPrintDocumentSourceAsync 的示例。

代码如下:

// Needs to be invoked before calling the print API    
function registerForPrintContract() {
        var printManager = Windows.Graphics.Printing.PrintManager.getForCurrentView();
        printManager.onprinttaskrequested = onPrintTaskRequested;
        console.log("Print Contract registered. Use the Print button to print.", "sample", "status");
}

// Variable to hold the document source to print
var gHtmlPrintDocumentSource = null;

// Print event handler for printing via the PrintManager API.
function onPrintTaskRequested(printEvent) {
    var printTask = printEvent.request.createPrintTask("Print Sample", function (args) {
        args.setSource(gHtmlPrintDocumentSource);

        // Register the handler for print task completion event
        printTask.oncompleted = onPrintTaskCompleted;
    });
}

// Print Task event handler is invoked when the print job is completed.
function onPrintTaskCompleted(printTaskCompletionEvent) {
    // Notify the user about the failure
    if (printTaskCompletionEvent.completion === Windows.Graphics.Printing.PrintTaskCompletion.failed) {
        console.log("Failed to print.", "sample", "error");
    }
}

// Executed just before printing.
var beforePrint = function () {
    // Replace with code to be executed just before printing the current document:
};

// Executed immediately after printing.
var afterPrint = function () {
    // Replace with code to be executed immediately after printing the current document:
};

function printButtonHandler() {
    // Optionally, functions to be executed immediately before and after printing can be configured as following:
    window.document.body.onbeforeprint = beforePrint;
    window.document.body.onafterprint = afterPrint;

    // Get document source to print
    MSApp.getHtmlPrintDocumentSourceAsync(document).then(function (htmlPrintDocumentSource) {
        gHtmlPrintDocumentSource = htmlPrintDocumentSource;

        // If the print contract is registered, the print experience is invoked.
        Windows.Graphics.Printing.PrintManager.showPrintUIAsync();
    });
}