client/extensions/woocommerce/woocommerce-services/state/shipping-label/actions.js
function downloadAndPrint( orderId, siteId, dispatch, getState, labels ) {
	// Backup the arguments for follow-up retries
	const downloadArgs = arguments;

	// No need to print the "Package 1 (of 1)" message if there's only 1 label
	const labelsToPrint =
		1 === labels.length
			? [ { labelId: labels[ 0 ].label_id } ]
			: labels.map( ( label, index ) => ( {
					caption: translate( 'PACKAGE %(num)d (OF %(total)d)', {
						args: {
							num: index + 1,
							total: labels.length,
						},
					} ),
					labelId: label.label_id,
			  } ) );

	const { paperSize } = getShippingLabel( getState(), orderId, siteId );
	const printUrl = getPrintURL( paperSize, labelsToPrint );

	const showSuccessNotice = () => {
		dispatch( createPrintSuccessNotice( labelsToPrint.length ) );
	};

	const retry = () => {
		dispatch( NoticeActions.removeNotice( PRINTING_FAILED_NOTICE_ID ) );
		dispatch(
			NoticeActions.infoNotice( translate( 'Printing…' ), {
				id: PRINTING_IN_PROGRESS_NOTICE_ID,
			} )
		);
		downloadAndPrint( ...downloadArgs );
	};

	const showErrorNotice = () => {
		// Manualy close the modal and remove old notices
		dispatch( exitPrintingFlow( orderId, siteId, true ) );
		dispatch( clearAvailableRates( orderId, siteId ) );
		dispatch( NoticeActions.removeNotice( PRINTING_IN_PROGRESS_NOTICE_ID ) );
		dispatch( createPrintFailureNotice( labels.length, retry ) );
	};

	let hasError = false;

	api
		.get( siteId, printUrl )
		.then( fileData => {
			dispatch( NoticeActions.removeNotice( PRINTING_IN_PROGRESS_NOTICE_ID ) );

			if ( 'addon' === getPDFSupport() ) {
				showSuccessNotice();
				// If the browser has a PDF "addon", we need another user click to trigger opening it in a new tab
				dispatch( {
					type: WOOCOMMERCE_SERVICES_SHIPPING_LABEL_SHOW_PRINT_CONFIRMATION,
					orderId,
					siteId,
					fileData,
					labels,
				} );
			} else {
				printDocument( fileData, getPDFFileName( orderId ) )
					.then( () => {
						showSuccessNotice();
					} )
					.catch( err => {
						dispatch( NoticeActions.errorNotice( err.toString() ) );
						hasError = true;
					} )
					.then( () => {
						handlePrintFinished( orderId, siteId, dispatch, getState, hasError, labels );
					} );
			}
		} )
		.catch( showErrorNotice );
}
function downloadAndPrint( orderId, siteId, dispatch, getState, labels ) {
	// Backup the arguments for follow-up retries
	const downloadArgs = arguments;

	// No need to print the "Package 1 (of 1)" message if there's only 1 label
	const labelsToPrint =
		1 === labels.length
			? [ { labelId: labels[ 0 ].label_id } ]
			: labels.map( ( label, index ) => ( {
					caption: translate( 'PACKAGE %(num)d (OF %(total)d)', {
						args: {
							num: index + 1,
							total: labels.length,
						},
					} ),
					labelId: label.label_id,
			  } ) );

	const { paperSize } = getShippingLabel( getState(), orderId, siteId );
	const printUrl = getPrintURL( paperSize, labelsToPrint );

	const showSuccessNotice = () => {
		dispatch( createPrintSuccessNotice( labelsToPrint.length ) );
	};

	const retry = () => {
		dispatch( NoticeActions.removeNotice( PRINTING_FAILED_NOTICE_ID ) );
		dispatch(
			NoticeActions.infoNotice( translate( 'Printing…' ), {
				id: PRINTING_IN_PROGRESS_NOTICE_ID,
			} )
		);
		downloadAndPrint( ...downloadArgs );
	};

	const showErrorNotice = () => {
		// Manualy close the modal and remove old notices
		dispatch( exitPrintingFlow( orderId, siteId, true ) );
		dispatch( clearAvailableRates( orderId, siteId ) );
		dispatch( NoticeActions.removeNotice( PRINTING_IN_PROGRESS_NOTICE_ID ) );
		dispatch( createPrintFailureNotice( labels.length, retry ) );
	};

	let hasError = false;

	api
		.get( siteId, printUrl )
		.then( fileData => {
			dispatch( NoticeActions.removeNotice( PRINTING_IN_PROGRESS_NOTICE_ID ) );

			if ( 'addon' === getPDFSupport() ) {
				showSuccessNotice();
				// If the browser has a PDF "addon", we need another user click to trigger opening it in a new tab
				dispatch( {
					type: WOOCOMMERCE_SERVICES_SHIPPING_LABEL_SHOW_PRINT_CONFIRMATION,
					orderId,
					siteId,
					fileData,
					labels,
				} );
			} else {
				printDocument( fileData, getPDFFileName( orderId ) )
					.then( () => {
						showSuccessNotice();
					} )
					.catch( err => {
						dispatch( NoticeActions.errorNotice( err.toString() ) );
						hasError = true;
					} )
					.then( () => {
						handlePrintFinished( orderId, siteId, dispatch, getState, hasError, labels );
					} );
			}
		} )
		.catch( showErrorNotice );
}
Attempts to download and print all labels that were just generated.

@param {number}   orderId  The ID of the order that is being edited.
@param {number}   siteId   The ID of the current site.
@param {Function} dispatch A Redux dispatcher.
@param {Function} getState A function that returns the current state.
@param {Array}    labels   An array of labels that should be downloaded and printed.