this.isPrinting = true;
- this.applyTemplate(printReq);
-
- // Give templates a chance to render before printing
- setTimeout(() => {
- this.dispatchPrint(printReq);
- this.reset();
+ this.applyTemplate(printReq).then(() => {
+ // Give templates a chance to render before printing
+ setTimeout(() => {
+ this.dispatchPrint(printReq);
+ this.reset();
+ });
});
}
- applyTemplate(printReq: PrintRequest) {
+ applyTemplate(printReq: PrintRequest): Promise<any> {
if (printReq.template) {
- // Inline template. Let Angular do the interpolationwork.
+ // Local Angular template.
this.template = printReq.template;
this.context = {$implicit: printReq.contextData};
- return;
+ return Promise.resolve();
+ }
+
+ let promise;
+
+ // Precompiled text
+ if (printReq.text) {
+ promise = Promise.resolve();
+
+ } else if (printReq.templateName || printReq.templateId) {
+
+ promise = this.printer.compileRemoteTemplate(printReq).then(
+ response => {
+ printReq.text = response.content;
+ printReq.contentType = response.contentType;
+ },
+ err => {
+ console.error("Error compiling template", printReq);
+ return Promise.reject(new Error(
+ 'Error compiling server-hosted print template'));
+ }
+ );
+
+ } else {
+ console.error("Cannot find template", printReq);
+ return Promise.reject(new Error("Cannot find print template"));
}
- if (printReq.text && true /* !this.hatch.isActive */) {
- // Insert HTML into the browser DOM for in-browser printing only.
+ return promise.then(() => {
- if (printReq.contentType === 'text/plain') {
- // Wrap text/plain content in pre's to prevent
- // unintended html formatting.
+ if (printReq.contentType === 'text/plain' && true /* this.hatch.isActive */) {
+ // When adding text output to DOM for rendering, wrap in
+ // pre to avoid unintended HTML formatting.
printReq.text = `<pre>${printReq.text}</pre>`;
}
this.htmlContainer.innerHTML = printReq.text;
- }
+ });
}
// Clear the print data
printViaHatch(printReq: PrintRequest) {
// Send a full HTML document to Hatch
- const html = `<html><body>${printReq.text}</body></html>`;
+ let html = printReq.text;
+ if (printReq.contentType === 'text/html') {
+ html = `<html><body>${printReq.text}</body></html>`;
+ }
/*
this.hatch.print({
import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
import {FormatService} from '@eg/core/format.service';
import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
+import {SampleDataService} from '@eg/share/util/sample-data.service';
@Component({
templateUrl: 'sandbox.component.html'
private strings: StringService,
private toast: ToastService,
private format: FormatService,
- private printer: PrintService
+ private printer: PrintService,
+ private samples: SampleDataService
) {
}
testServerPrint() {
// Note these values can be IDL objects or plain hashes.
- const patron = this.idl.create('au');
- const address = this.idl.create('aua');
- patron.first_given_name('Crosby');
- patron.second_given_name('Stills');
- patron.family_name('Nash');
- address.street1('123 Pineapple Road');
- address.street2('Apt #4');
- address.city('Bahama');
- address.state('NC');
- address.post_code('555444');
-
const templateData = {
- patron: patron,
- address: address
+ patron: this.samples.listOfThings('au')[0],
+ address: this.samples.listOfThings('aua')[0]
}
// NOTE: eventually this will be baked into the print service.
- this.printer.compileRemoteTemplate({
- templateName: 'address-label',
+ this.printer.print({
+ templateName: 'patron_address',
contextData: templateData,
printContext: 'default'
- }).then(
- response => {
- console.log(response.contentType);
- console.log(response.content);
- this.printer.print({
- printContext: 'default',
- contentType: response.contentType,
- text: response.content,
- showDialog: true
- });
- }
- );
+ });
}
}
-