如何使用getHtmlPrintDocumentSourceAsync在HTML/JavaScript Windows应用商店应用中打印?

ale*_*din 3 windows-runtime windows-store-apps microsoft-edge

我只是尝试使用HTML和JavaScript/WinJS在Windows 10应用程序(通用)中打印一些内容.

所有文档都说有一个MSApp被调用的函数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;
    });
}
Run Code Online (Sandbox Code Playgroud)

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

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

可能不是非常有用,但执行上述代码时打开的Windows打印对话框中的消息是:"没有发送任何内容进行打印.打开文档并再次打印."

有任何想法吗?

Kir*_*nov 5

getHtmlPrintDocumentSource是Windows 10应用程序中的同步弃用API.我们将研究一些留给Windows 8和8.1的文档,以澄清这一点.

有关如何在JavaScript中使用的示例,请查看https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Printing/jsgetHtmlPrintDocumentSourceAsync.

这是代码:

// 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();
    });
}
Run Code Online (Sandbox Code Playgroud)