How to render HTML page with attachments only from the render array? I would like to render a full html page(with html and body tags) with content provided by a render array.

What do I need to do in order to NOT render attachments(CSS, JS, drupalSettings) from active theme or modules but solely from attachments defined in the provided render array?



So far this is quite close but I still see attachments from base theme:

\Drupal::service('bare_html_page_renderer')
      -&gt;renderBarePage(
        $my_render_array,
        'Page label',
        'page',
        []
      );


Using my own, empty, theme hasn't helped.



Ok, I had to duplicate what bare_html_page_renderer did since it attached "default" libraries form the system module. So now with my own theme all looks good but the structure is not right. Seems like the placeholders are not working properly.

My html.html.twig source code looks like:

&lt;!DOCTYPE html&gt;
&lt;html{{ html_attributes }}&gt;
&lt;head&gt;
  &lt;head-placeholder token="{{ placeholder_token }}"&gt;
  &lt;title&gt;{{ head_title|safe_join(' | ') }}&lt;/title&gt;
  &lt;css-placeholder token="{{ placeholder_token }}"&gt;
  &lt;js-placeholder token="{{ placeholder_token }}"&gt;
&lt;/head&gt;
&lt;body{{ attributes }}&gt;
{{ page }}
&lt;js-bottom-placeholder token="{{ placeholder_token }}"&gt;
&lt;/body&gt;
&lt;/html&gt;


but the rendered HTML will looks like:

&lt;html dir="ltr" lang="en"&gt;
&lt;head&gt; &lt;/head&gt;
&lt;body&gt;
&lt;head-placeholder token="..."&gt;
&lt;title&gt;Page title&lt;/title&gt;
&lt;link rel="stylesheet" href="..." media="all"&gt;
&lt;div&gt;the rendered content&lt;/div&gt;
&lt;script src="..."&gt;
&lt;/head-placeholder&gt;
&lt;/body&gt;
&lt;/html&gt;




So the issue was the there has to be at least one attribute in the header, otherwise the 'head' placeholder won't be replaced. So adding a favicon or generator or any metatag will do the trick.

Unfortunately this works only for route response with theme negotiator but not if I just want to print the pure html code since the active theme will be applied.
So this is a working solution. The response has getContent() method so it can be used in code and not only as route response.

function renderHtml($some_content) {
    $html = [
      '#type' =&gt; 'html',
      'page' =&gt; [
        '#type' =&gt; 'page',
        '#title' =&gt; 'Some page',
        'content' =&gt; $some_content,
        '#attached' =&gt; [
          // At least one of these have to have value,
          // otherwise the head placeholder won't get replaced.
          'html_head_link' =&gt; [],
          'html_head' =&gt; []
        ]
      ]
    ];

    $html['page']['#attached']['html_head'][] = [[
      '#type' =&gt; 'html_tag',
      '#tag' =&gt; 'meta',
      '#attributes' =&gt; array(
        'name' =&gt; 'Sometag',
        'content' =&gt; 'somevalue',
      ),
    ], 'someid'];

    $active_theme = \Drupal::theme()-&gt;getActiveTheme();
    $render_theme_extension = \Drupal::service('theme_handler')-&gt;getTheme('rendering_theme');
    $new_active_theme = \Drupal::service('theme.initialization')-&gt;getActiveTheme($render_theme_extension);
    \Drupal::theme()-&gt;setActiveTheme($new_active_theme);

    \Drupal::service('renderer')-&gt;renderRoot($html);
    $response = new HtmlResponse();
    $response-&gt;setContent($html);
    $response-&gt;setMaxAge(0);
    $response = \Drupal::service('html_response.attachments_processor')-&gt;processAttachments($response);

    \Drupal::theme()-&gt;setActiveTheme($active_theme);

    return $response;
}


Note: when you are using this via ajax callback, you have to unset the ajax_page_state from the post request, otherwise you'll be most likely be missing some libraries.