'SetaPDF Merger for Wordpress result with error “Headers already been send in”

I am using SetaPDF Merger and I am almost finished with my project to set it up for my WordPress site. But of course I run into trubble. I have a repeatable field in a metabox that will send the url so that it engraves a merge file as a result. Why am I getting this error? Please help me out.

$autopro = get_post_meta( get_the_ID(), '_da_attachments', true);
$merger = new SetaPDF_Merger();

$merger->addFile([
    'filename' => realpath(__DIR__ . "/../../../../") . "/wp-content/uploads/2022/03/Valsignelsen.pdf",
    'outlinesConfig' => [
        SetaPDF_Merger::OUTLINES_COPY => SetaPDF_Merger::COPY_OUTLINES_TO_ROOT
    ]
]);        
      if ( $autopro ) {
        foreach ( $autopro as $auto ) {

            echo
            '
            $merger->addFile([
    "filename" => realpath(__DIR__ . "/../../../../") . "';
    
    echo parse_url(wp_get_attachment_url($auto ['file_id']) , PHP_URL_PATH);
    
    echo '",
    "outlinesConfig" => [
        SetaPDF_Merger::OUTLINES_COPY => SetaPDF_Merger::COPY_OUTLINES_TO_ROOT
    ]
]);';

        }
    }

My error log shows:

$merger->addFile([ "filename" => realpath(__DIR__ . "/../../../../") . "/wp-content/uploads/2022/03/Valsignelsen.pdf", "outlinesConfig" => [ SetaPDF_Merger::OUTLINES_COPY => SetaPDF_Merger::COPY_OUTLINES_TO_ROOT ] ]); 


$merger->addFile([ "filename" => realpath(__DIR__ . "/../../../../") . "/wp-content/uploads/2022/03/A-merry-Christmas.pdf", "outlinesConfig" => [ SetaPDF_Merger::OUTLINES_COPY => SetaPDF_Merger::COPY_OUTLINES_TO_ROOT ] ]);

Fatal error: Uncaught SetaPDF_Core_Writer_Exception: Headers already been send in /home2/notparti/public_html/wp-content/plugins/dk-pdf/includes/dkpdf-functions.php on line 137 in /home2/notparti/public_html/wp-content/plugins/dk-pdf/vendor/SetaPDF/Core/Writer/Http.php:100 Stack trace: #0 /home2/notparti/public_html/wp-content/plugins/dk-pdf/vendor/SetaPDF/Core/Document.php(1715): SetaPDF_Core_Writer_Http->finish() #1 /home2/notparti/public_html/wp-content/plugins/dk-pdf/includes/dkpdf-functions.php(168): SetaPDF_Core_Document->finish() #2 /home2/notparti/public_html/wp-includes/class-wp-hook.php(307): dkpdf_output_pdf(Object(WP)) #3 /home2/notparti/public_html/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters('', Array) #4 /home2/notparti/public_html/wp-includes/plugin.php(522): WP_Hook->do_action(Array) #5 /home2/notparti/public_html/wp-includes/class-wp.php(771): do_action_ref_array('wp', Array) #6 /home2/notparti/public_html/wp-includes/functions.php(1310): WP->main('') #7 /home2/notparti/public_html/wp-blog- in /home2/notparti/public_html/wp-content/plugins/dk-pdf/vendor/SetaPDF/Core/Writer/Http.php on line 100

If I replace the loop (first code example) with

$merger->addFile([
    'filename' => realpath(__DIR__ . "/../../../../") . "/wp-content/uploads/2022/03/Valsignelsen.pdf",
    'outlinesConfig' => [
        SetaPDF_Merger::OUTLINES_COPY => SetaPDF_Merger::COPY_OUTLINES_TO_ROOT
    ]
]);

Then it’s working properly… Why can’t I use the loop?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source