Where in the page generation pipeline can an HTML beautifier be placed? I need to view the HTML generated by Drupal and it is pretty hard. If I want to the final output to be put through an HTML beautifier before sending it off to the client where would I do that?

I am sure some of the page optimization utilities on the admin/config/development/performance or the advagg module are at the right place in the pipeline. What stage or APIs would I have to hook into to accomplish something this?

Are there any APIs which can be hooked into to accomplish this or even modules to this?
Yes, in advagg is a good example, you have to get the response content with $response-&gt;getContent() in a response subscriber http://cgit.drupalcode.org/advagg/tree/src/EventSubscriber/ResponseSubscriber.php?h=8.x-3.x

  public function onResponse(FilterResponseEvent $event) {
    // Only care about HTML responses.
    if (stripos($event-&gt;getResponse()-&gt;headers-&gt;get('Content-Type'), 'text/html') === FALSE) {
      return;
    }

    $response = $event-&gt;getResponse();
    $html_markup = $response-&gt;getContent();
    // modify html markup
    $response-&gt;setContent($html_markup);
  }