DevopediaOrg / webapp

A dummy repo as a placeholder for Devopedia webapp. It will be used mainly to track issues.
7 stars 0 forks source link

Printable PDF version of an article #534

Open arvindpdmn opened 3 years ago

arvindpdmn commented 3 years ago

As in title. Inspired by this feature in Wikipedia.

arvindpdmn commented 3 years ago

mpdf could be considered:

arvindpdmn commented 4 months ago

Limitations:

arvindpdmn commented 4 months ago

Some trial code:

    $text = $content->getBody();
    $text = str_replace("/images/", "images/", $text);
    $text = preg_replace('/src="(.*?)" data-fullsrc="(.*?)"/', 'src="\2" data-fullsrc="\2"', $text);
    $text = preg_replace('/<body class=/', '<body style="margin-left:80px" class=', $text);
    $text = preg_replace('/<div class="article-middle/', '<div style="margin-left:80px" class="article-middle', $text);
    $text = preg_replace('/<figure class=/', '<figure style="border:0px" class=', $text);
    $mpdf = new Mpdf([
        'mode' => 'utf-8',
        'format' => 'A4-P',
        'orientation' => 'P'
    ]);
    //$mpdf->showImageErrors = true;
    $mpdf->WriteHTML($text);
    $mpdf->Output('download.pdf', 'F');

Tried dompdf package but mpdf seems to be better:

    $dompdf = new Dompdf();
    $dompdf->loadHtml($text);
    $dompdf->setPaper('A4', 'portrait');
    $dompdf->render();
    file_put_contents("download.pdf", $dompdf->output());