Some time ago, we wrote an article on how to merge PDF product specifications to printed Purchase orders. Read the article >>
The solution presented in the above article is based on capability to merge PDF documents using Docentric Free Edition utilities. It enables you to view, email, archive or distribute a D365FO report merged with any PDF attachments. Report can be either Docentric- or SSRS- based, and report distribution covers all Docentric print destinations (Screen, Email, Print archive, File, Printer with the Print as PDF option turned on) except printing to network printers (Printer print destination).
We are now going to extend this solution to support printing of reports merged with PDF attachments to network printers in D365FO cloud environments. The only limitation that will remain is printing to network printers in on-premises environments.
The Coding Part - With Added Support for Printing to Network Printers
In the previous article, we provided the working code for merging the Purchase order report with PDF attachments fetched from the corresponding products (the Item records), assuming that these attachments are the product specifications related to the printing Purchase order lines. Of course, you can filter attachments on Items per Tags, Category, Attachment description, etc. The printing Purchase order report, after it's merged with the product specifications, is further regularly viewed, emailed, archived or distributed using Docentric print destinations.
We're now adding a new code - an additional event handler for the same delegate DocReportRunDelegates.generateReportContent(), to support also the printing of a Purchase order report merged with the product specifications (fetched as Attachments which are PDF documents) to the network printer that has been selected on the Print destination settings form > Docentric Printer print destination. Let's take a look.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
using DOCFC = Docentric.AX.Framework.Collections; using DOCDPC = Docentric.AX.Documents.Pdf.Conversion; using System.Collections; using System.IO; public class DocPrintPdfAttachmentsToPrinterAsEmf { /// <summary> /// After the PurchPurchaseOrder.Report has been generated, the delegate method prints additional PDF attachments. These attachments are converted from PDF to EMF images, which are supported by the DRA print service. /// </summary> /// <param name = "_printReportSettings">Specifies the settings for printing the report.</param> /// <param name = "_printedReport">Represents the printed report.</param> /// <param name = "_result">The delegate result used to cancel the report print execution.</param> [SubscribesTo(classStr(DocReportRunDelegates), delegateStr(DocReportRunDelegates, generateReportContent))] public static void DocReportRunDelegates_generateReportContent(DocPrintReportSettings _printReportSettings, DocPrintedReport _printedReport, DocEventHandlerResult _result) { // Exit if we are printing anything else then PurchPurchaseOrder.Report if (_printReportSettings.parmReportId() != ssrsReportStr(PurchPurchaseOrder, Report)) return; // Exit if we are not printing to printer print destination if (_printReportSettings.parmPrintDestination() != DocPrintDestination::Printer) return; // Exit if we are not printing in native printer format (EMF); Skip if "Print as PDF" is used. if (_printedReport.outputFileFormat() != DocFileFormat::EMF) return; //================================================================================================= PurchTable purchTable; PurchLine purchLine; InventTable inventTable; EcoResProduct product; DocuRef docuRef; DocuType docuType; // Define a new list of byte array for additional attachments and prepare the conversion options DOCDPC.DocImageFormat imgFormat; DOCDPC.DocImageConversionOptions conversionOptions = new DOCDPC.DocImageConversionOptions(DocGlobalHelper::getDotNetEnumValueForXppEnumValue(imgFormat.GetType(), enumNum(DocFileFormat), DocFileFormat::EMF)); DOCFC.DocByteArrayList emfByteArrayList = new DOCFC.DocByteArrayList(); // The generated report EMF pages in list of byte array. IList pages = _printedReport.getReportContentImageList(); // Get the PurchTable record, based on the VendPurchOrderJour information purchTable = VendPurchOrderJour::findRecId( _printReportSettings.parmArchiveContract().parmJournalRecId()).purchTable(); // Or you can get it this way: //purchTable = PurchTable::find(_printReportSettings.parmArchiveContract().parmSourceTableId()); // Loop through the products related to the purchase order lines. // By doing this, we ensure that each product is included only once, even if it appears on multiple purchase order lines. while select RecId, TableId, DisplayProductNumber from product exists join inventTable where inventTable.Product == product.RecId exists join purchLine where purchLine.ItemId == inventTable.ItemId && purchLine.PurchId == purchTable.PurchId { // Fetch attachments for the current product. while select docuRef where docuRef.RefTableId == product.TableId && docuRef.RefRecId == product.RecId exists join docuType where docuType.TypeId == docuRef.TypeId && (docutype.TypeGroup == DocuTypeGroup::File || docuType.TypeGroup == DocuTypeGroup::Image || docuType.TypeGroup == DocuTypeGroup::Document) { DocuValue docuValue = docuRef.docuValue(); // Only PDF attachments will be printed; other attachments will be ignored. if (docuValue.FileType != 'pdf') { continue; } // Each PDF attachment is converted into a series of EMF images, which are then added to the list of EMF byte arrays. info("Attachment for item id: " + product.DisplayProductNumber + " --> " + docuRef.Name); try { using (System.IO.Stream docuRefStream = DocumentManagement::getAttachmentStream(docuRef)) { // Convert PDF to EMF and add all the EMF pages to the list emfByteArrayList.AddRange(DocDocumentHelper::convertPdf2Emf(docuRefStream, conversionOptions)); } } catch (Exception::CLRError) { DocGlobalHelper::handleClrException(funcName(), "Error occurred while adding the list of EMF byte arrays to the collection."); } catch { DocGlobalHelper::handleException(funcName(), "An error occurred while adding the list of EMF byte arrays to the collection."); } } } // The report is printed as a collection of EMF images in byte array. // If we have any additional pages to print we add then to the original list. if (emfByteArrayList.Count > 0) { for (int i=0; i < emfByteArrayList.Count; i++) { pages.Add(emfByteArrayList.GetByIndex(i)); } } } } |
Check the previous article: Append PDF Product Specifications to Printed Purchase Orders in D365FO >>