print - Dave's Blog

Search
My timeline on Mastodon

MSApp.getHtmlPrintDocumentSourceAsync - JavaScript UWP app printing

2017 Oct 11, 5:49

The documentation for printing in JavaScript UWP apps is out of date as it all references MSApp.getHtmlPrintDocumentSource but that method has been replaced by MSApp.getHtmlPrintDocumentSourceAsync since WinPhone 8.1.

Background

Previous to WinPhone 8.1 the WebView's HTML content ran on the UI thread of the app. This is troublesome for rendering arbitrary web content since in the extreme case the JavaScript of some arbitrary web page might just sit in a loop and never return control to your app's UI. With WinPhone 8.1 we added off thread WebView in which the WebView runs HTML content on a separate UI thread.

Off thread WebView required changing our MSApp.getHtmlPrintDocumentSource API which could no longer synchronously produce an HtmlPrintDocumentSource. With WebViews running on their own threads it may take some time for them to generate their print content for the HtmlPrintDocumentSource and we don't want to hang the app's UI thread in the interim. So the MSApp.getHtmlPrintDocumentSource API was replaced with MSApp.getHtmlPrintDocumentSourceAsync which returns a promise the resolved value of which is the eventual HtmlPrintDocumentSource.

Sample

However, the usage of the API is otherwise unchanged. So in sample code you see referencing MSApp.getHtmlPrintDocumentSource the sample code is still reasonable but you need to call MSApp.getHtmlPrintDocumentSourceAsync instead and wait for the promise to complete. For example the PrintManager docs has an example implementing a PrintTaskRequested event handler in a JavaScript UWP app.

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

Instead we need to obtain a deferral in the event handler so we can asynchronously wait for getHtmlPrintDocumentSourceAsync to complete:

    function onPrintTaskRequested(printEvent) {    
var printTask = printEvent.request.createPrintTask("Print Sample", function (args) {
const deferral = args.getDeferral();
MSApp.getHtmlPrintDocumentSourceAsync(document).then(htmlPrintDocumentSource => {
args.setSource(htmlPrintDocumentSource);
deferral.complete();
}, error => {
console.error("Error: " + error.message + " " + error.stack);
deferral.complete();
});
});
PermalinkCommentsjavascript MSApp printing programming uwp webview win10 windows

Retweet of mayabielinski

2016 Feb 9, 9:20
Gender bias on GitHub: women's contributions accepted more often than men's - except when gender is identifiable. https://peerj.com/preprints/1733/ 
PermalinkComments

Retweet of doctorow

2016 Feb 8, 5:08
A digital, 3D printed sundial whose precise holes cast a shadow displaying the current time https://boingboing.net/2016/02/09/a-digital-3d-printed-sundial.html … pic.twitter.com/zTSRoXL9a7
PermalinkComments

Tweet from David_Risney

2015 Oct 14, 2:54
Thor's hammer = Microwave's electromagnet + Arduino + fingerprint scanner. Inventor trolls Venice Beach bystanders: http://www.cnet.com/news/engineer-builds-working-thors-hammer-only-he-can-lift/ …
PermalinkComments

Retweet of SwiftOnSecurity

2015 Aug 16, 1:09
.@realnzall Don't worry, Tay has a physical hard drive wiper with verification pass and optional label printer 😁 pic.twitter.com/PgAqBKXgCH
PermalinkComments

Retweet of XFilesFiles

2015 Mar 23, 9:04
THE X-FILES IS BACK!!!!! http://www.hitfix.com/the-fien-print/no-conspiracy-fox-resurrects-the-x-files-for-6-episode-run …
PermalinkComments

Retweet of TheAtlantic

2015 Mar 20, 4:03
A new 3-D printer pulls fully formed objects out of liquid at 100 times the speed http://theatln.tc/199X3VM  pic.twitter.com/cMb6HSthL7
PermalinkComments

Retweet of engadget

2015 Mar 15, 10:04
Here's how you make a 3D printing gun using Lego http://engt.co/1CmjeEy  pic.twitter.com/ZBFc50xZL2
PermalinkComments

Tweet from David_Risney

2015 Feb 12, 12:03
Collection of mobile 3d printer robots build large structures: http://monograph.io/iaac/minibuilders … (aka the future)
PermalinkComments

Retweet of ericlaw

2015 Feb 1, 6:02
http://www.theonion.com/articles/fingerprints-on-lombardi-trophy-to-be-used-in-doze,37899/ …
PermalinkComments

C++ constructor member initializers run in member declaration order

2013 Jul 18, 3:29PermalinkCommentsc++ development programming technical

laughingsquid: Photos: MakerBot Retail Store in Manhattan

2012 Sep 20, 2:14


laughingsquid:

Photos: MakerBot Retail Store in Manhattan

PermalinkComments3d-printer maker-bot retail

Stripe CTF - Level 7

2012 Sep 13, 5:00PermalinkCommentshash internet length-extension security sha1 stripe-ctf technical web

Its a suitcase 3D printer. Nice soundtrack too.

2012 Jul 28, 1:54PermalinkCommentsmake 3d-printer video suitcase

(via Descriptive Camera) A digital camera sends photos to...

2012 Apr 25, 4:23


(via Descriptive Camera)

A digital camera sends photos to Mechanical Turk service to generate a textual description and print the result on a thermal printer.  Thus a camera that prints out a textual description of what you photographed.

PermalinkCommentshumor camera photo mechanical-turk

Free Universal Construction Kit is a set of 3D models you can...

2012 Mar 19, 4:05


Free Universal Construction Kit is a set of 3D models you can print on a 3D printer that allow you to connect Lego to Duplo to Lincoln Logs, etc.

PermalinkCommentstoy video 3d-printer

See http://boingboing.net/tag/TED2012 for Mark’s other TED...

2012 Feb 29, 10:17


See http://boingboing.net/tag/TED2012 for Mark’s other TED 2012 videos.

PermalinkCommentsted video boing-boing 3d-printer maker-bot

(via MakerBot Replicator, A New Larger 2 Color 3D Printer by...

2012 Jan 9, 5:43


(via MakerBot Replicator, A New Larger 2 Color 3D Printer by MakerBot)

PermalinkCommentsvideo maker-bot 3d-printer

A Perl Regular Expression That Matches Prime Numbers (catonmat.net)

2011 Dec 28, 3:36

perl -lne ‘(1x$_) =~ /^1?$|^(11+?)\1+$/ || print “$_ is prime”’

PermalinkCommentstechnical perl regexp prime math

Sprint loaded spyware on its Android phones

2011 Nov 30, 4:22

“TechCrunch and others are reporting that a program called “Carrier IQ” that comes pre-installed on Sprint phones has some pretty amazing spyware capabilities, right down to keylogging everything you do on the phone.”

PermalinkCommentstechnical cell-phone privacy security
Older Entries Creative Commons License Some rights reserved.