The PrintingTools service and its client print widgets in the ArcGIS web APIs are intended for basic printing. For printing workflows in which the web map needs to be modified or exported using advanced options, use a convert function in the Python arcpy module: ConvertWebMapToMapDocument for ArcMap and ConvertWebMapToArcGISProject for ArcGIS Pro.
Web map printing in ArcGIS hinges on the ability of the web map to be represented as JSON. Esri has created the ExportWebMap specification that defines how a web map can be structured as JSON with extent and layer information for printing. The ArcGIS web APIs can read your current web map and make the JSON for you. In a simple printing scenario, the ArcGIS Server PrintingTools service reads the JSON and returns a map image that can be printed.
In advanced printing scenarios, the web map JSON does not have to be sent to the PrintingTools service. Instead, you can convert the JSON into an ArcMap document (.mxd) or ArcGIS Pro project (.aprx).
Converting a web map to an ArcMap map document using Python
To convert a web map JSON into a map document (.mxd), use the ConvertWebMapToMapDocument function. Once the web map is converted, the full state of the web map exists in the map document. The document can then be further modified before being printed or exported using a function such as ExportToPDF or ExportToPNG. You might even choose to add the map to a map book before printing it, or switch out some of the service layers in the map with your own vector layers.
Converting a web map to an ArcGIS Pro project using Python
To convert a web map JSON into an ArcGIS Pro project (.aprx), use the ConvertWebMapToArcGISProject function. Once the web map is converted, the full state of the web map exists in the project. You can then modify the web map further before printing, or export it using another Python function. As with ArcMap, you can also add the map to a map book before printing it, or switch out some of the service layers in the map with your own vector layers.
For more detailed discussion, scenarios, and code samples, see Web map printing with arcpy.mp, which explains how to author staged layout templates, author a Python script for a web map, and create web tools and web apps.
Calling your scripts from the web
Once you've written an arcpy script for printing, ArcGIS Enterprise offers geoprocessing services that allow you to expose your scripts, tools, and models as web services. This makes them easily callable from the ArcGIS web APIs.
You can choose to encapsulate your Python printing script in a geoprocessing script tool. Do a test run of your tool and then publish the result as an ArcGIS Enterprise geoprocessing service.
The easiest approach when developing this type of geoprocessing script tool is to make sure that it exposes the same parameters as the out-of-the-box PrintingTools service, or a subset thereof. The input JSON parameter is the minimum required parameter needed. If you design your service in this way, the printing widgets from ArcGIS Web AppBuilder and printing tasks in the ArcGIS web mapping APIs and web viewers will be able to use your service without any custom coding.