Cordovaで印刷機能を付けるにはcordova.plugins.printer.print

インストール

cordova.plugins.printer.printプラグインをインストールする。

使い方

cordova.plugins.printer.print();

基本動作としては、このスクリプトのみで印刷が可能。

HTMLタグで印刷

全体的なデザインではなく、その中の画像のみといった印刷を行いたい場合はHTMLタグを指定することも可能。

// 文字だけ
cordova.plugins.printer.print('Hello World!');

// 画像の場合
cordova.plugins.printer.print('<img src="URL">');

// テーブル構造などデザイン調整した場合
cordova.plugins.printer.print('<table><tr><td><img src="URL"></td><td><img src="URL"></td></tr></table>');

画像のリンク先やHTMLタグを引数とすることで印刷することが可能。

画像を直接渡す

画像を直接渡す方法も可能。

// ローカル画像を指定する場合
cordova.plugins.printer.print('file://img/logo.png');

// Dataファイルを渡す場合
cordova.plugins.printer.print('base64://...');

オプション

オプションを指定することで、縦・横の指定なども設定が可能。

opt = {
	orientation: 'portrait'
	, pageCount: 1
	, margin: false
};
cordova.plugins.printer.print('<img src="イメージURL">', opt);
NameDescriptionTypePlatform
nameThe name of the print job and of the document.Stringall
copiesThe number of copies for the print task.NumberiOS
Windows
pageCountLimits the pages to print even the document contains more.
To skip the last n pages you can assign a negative value on iOS.
NumberiOS
Android
duplexEither double-sided on short site (duplex:’short’), double-sided on long site (duplex:’long’) or single-sided (duplex:’none’).Stringall
orientationThe orientation of the printed content, portrait or landscape.Stringall
monochromeIf your application only prints black text, setting this property to true can result in better performance in many cases.Booleanall
photoSet to true to change the media type to photography for higher quality.BooleaniOS
Windows
autoFitSet to false to disable downscaling the image to fit into the content aread.BooleanAndroid
printerThe network URL to the printer.StringiOS
maxHeight
maxWidth
Defines the maximum size of the content area.UnitiOS
marginSet to false to avoid margins.Booleanall
margin.top
margin.left
margin.right
margin.bottom
The margins for each printed page. Each printer might have its own minimum margins depends on media type and paper format.UnitiOS
ui.hideNumberOfCopiesSet to true to hide the control for the number of copies.BooleaniOS
ui.hidePaperFormatSet to true to hide the control for the paper format.BooleaniOS
ui.top
ui.left
The position of the printer picker.NumberiPad
ui.height
ui.width
The size of the printer picker.NumberiPad
paper.width
paper.height
The dimensions of the paper – iOS will will try to choose a format which fits bests.UnitiOS
paper.nameThe name of the format like IsoA4 or Roll22Inch.
https://docs.microsoft.com/en-us/uwp/api/windows.graphics.printing.printmediasize
StringWindows
paper.lengthOn roll-fed printers you can decide when the printer cuts the paper.UnitiOS
font.nameThe name of the font familyStringiOS
font.sizeThe size of the fontNumberiOS
Android
font.italic
font.bold
Set to true to enable these font traits.BooleaniOS
font.alignPossible alignments are leftrightcenter and justified.StringiOS
font.colorThe color of the font in hexa-decimal RGB format – "FF0000" means red.StringiOS
header.height
footer.height
The height of the header or footer on each page.UnitiOS
header.labels
footer.labels
An array of labels to display. Only use if there are more then one.ArrayiOS
header.label.text
footer.label.text
The plain text to display. Use %ld to indicate where to insert the page index.
For example "Page %ld" would result into "Page 1""Page 2", …
StringiOS
header.label.top
header.label.right
header.label.left
header.label.bottom
footer.label.*
The relative position where to place the label within the footer or header area.UnitiOS
header.label.font
footer.label.font
The font attributes for the label.ObjectiOS
header.label.showPageIndex
footer.label.showPageIndex
Set to true if you want to display the page index.
BooleaniOS

ダイレクトプリント

For iOS its possible to send the content directly to the printer without any dialog. Todo so pass the network URL as an option:

cordova.plugins.printer.print(content, { printer: 'ipp://...' });

To let the user pick an available printer:

cordova.plugins.printer.pick(function (url) {});

It’s possible to specify the position of the picker:

cordova.plugins.printer.pick({ top: 40, left: 30 }, callback);

Printable Document Types

The list of supported document types differ between mobile platforms. As of writing, Windows UWP only supports HTML and plain text.

To get a list of all printable document types:

cordova.plugins.printer.getPrintableTypes(callback);

To check if printing is supported in general:

cordova.plugins.printer.canPrintItem(callback);

Or in particular:

cordova.plugins.printer.canPrintItem('file://css/index.css', callback);