Creating PDF on controller after $site->createChild()

Hey there.

I tried different way’s - but none of them work.
I want after $site->createChild() that it create on the fly a pdf file.
I got all my code in a controller.

If i just post in my template <h1>Test<h1> - Works.
Is maybe render to “much”? is there a better way?

Finally what I want is, that after the child is create load html of it and make pdf :slight_smile:

Way 1 - endless loading after create (over form - frontend)

try {
                
$projectName = $data["projectName"];
$projectSlug = str::slug($data["projectName"]."-".date("d.m.Y, h:i"));

$posts = $site->createChild([
    "slug"     => $projectSlug ,
    "template" => "posts",
    "content"  => $data
]);

$postsDirectory = $posts->contentFileDirectory();


if ($posts) {
    $mpdf = new \Mpdf\Mpdf();
    ob_start();

    echo $posts->render();
    $html = ob_get_contents(); 
    ob_end_clean();

    $mpdf->WriteHTML($html);
    $mpdf->Output( $postsDirectory .'/'. $projectName .'.pdf', \Mpdf\Output\Destination::FILE);

  }

Way 2 - endless loading after create (over form - frontend)

try {
                
$projectName = $data["projectName"];
$projectSlug = str::slug($data["projectName"]."-".date("d.m.Y, h:i"));

$posts = $site->createChild([
    "slug"     => $projectSlug ,
    "template" => "posts",
    "content"  => $data
]);

$postsDirectory = $posts->contentFileDirectory();


if ($posts) {
    $mpdf = new \Mpdf\Mpdf();
    ob_start();

    $kirby->impersonate('kirby', function () use($posts) {
                    echo $posts->render();
                    return;
                    });
    $html = ob_get_contents();
    ob_end_clean();

    $mpdf->WriteHTML($html);
    $mpdf->Output( $postsDirectory .'/'. $projectName .'.pdf', \Mpdf\Output\Destination::FILE);

  }

Way 3 - endless loading after create (over form - frontend)

try {
                
$projectName = $data["projectName"];
$projectSlug = str::slug($data["projectName"]."-".date("d.m.Y, h:i"));

$posts = $site->createChild([
    "slug"     => $projectSlug ,
    "template" => "posts",
    "content"  => $data
]);

$postsDirectory = $posts->contentFileDirectory();


if ($posts) {
    $mpdf = new \Mpdf\Mpdf();
    ob_start();

    $content = file_get_contents("http://localhost:8000/project-29-06-2022-06-22"); 
    
                    $html = ob_get_contents($content);
                    ob_end_clean();

    $mpdf->WriteHTML($html);
    $mpdf->Output( $postsDirectory .'/'. $projectName .'.pdf', \Mpdf\Output\Destination::FILE);

  }

Could you provide a stripped down demo version of this for download somewhere?

Not sure from the top of my head what’s happening there.

@texnixe yeah i could - I can add it to a git repro - if you can PM me your mail or git user?

So - we got a solution - I switch for local env to Valet and now it works with

$mpdf = new \Mpdf\Mpdf();

                    $html = $post->render()
    
                    $mpdf->WriteHTML($html);
                    $mpdf->Output($postDirectory .'/'. $projectName .'.pdf', \Mpdf\Output\Destination::FILE);