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 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 ArcGIS Pro project (.aprx).
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. 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.