<commit_before><?php

namespace AppBundle\Service;

/**
 * Client to connect to docker-wkhtmltopdf-aas
 * https://github.com/openlabs/docker-wkhtmltopdf-aas.
 */
class WkHtmlToPdfGenerator
{
    /**
     * @var string
     */
    private $url;

    /**
     * @param string $url
     */
    public function __construct($url, $timeoutSeconds)
    {
        $this->url = $url;
        $this->timeoutSeconds = $timeoutSeconds;
    }

    /**
     * @return bool true if working
     */
    public function isAlive()
    {
        $pdf = $this->getPdfFromHtml('test');

        return strlen($pdf) > 5000 && preg_match('/PDF-\d/', $pdf);
    }

    /**
     * @param string $html
     *
     * @return string pdf
     */
    public function getPdfFromHtml($html)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeoutSeconds);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeoutSeconds); //timeout in seconds
        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-type: application/json']); // Assuming you're requesting JSON
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $body = json_encode([
            'contents' => base64_encode($html),
        ]);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

        return curl_exec($ch);
    }
}
<commit_msg>DDPB-1172: Fixing encoding on PDF generation<commit_after><?php

namespace AppBundle\Service;

/**
 * Client to connect to docker-wkhtmltopdf-aas
 * https://github.com/openlabs/docker-wkhtmltopdf-aas.
 */
class WkHtmlToPdfGenerator
{
    /**
     * @var string
     */
    private $url;

    /**
     * @param string $url
     */
    public function __construct($url, $timeoutSeconds)
    {
        $this->url = $url;
        $this->timeoutSeconds = $timeoutSeconds;
    }

    /**
     * @return bool true if working
     */
    public function isAlive()
    {
        $pdf = $this->getPdfFromHtml('test');

        return strlen($pdf) > 5000 && preg_match('/PDF-\d/', $pdf);
    }

    /**
     * @param string $html
     *
     * @return string pdf
     */
    public function getPdfFromHtml($html)
    {
        //Example from https://github.com/openlabs/docker-wkhtmltopdf-aas/issues/18
        $data = array(
            'contents' => base64_encode($html),
            'options' => array(
                'encoding' => 'utf-8'
            ),
        );
        $dataString = json_encode($data);
        $headers = array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($dataString),
        );

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $dataString);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $result = curl_exec($ch);
        curl_close($ch);

        return $result;
    }
}

{'fraction_non_alphanumeric': 0.14532756900565347, 'fraction_numerical': 0.006651147322913203, 'mean_word_length': 2.23093447905478, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7651420', 'n_tokens_mistral': 1010, 'n_tokens_neox': 954, 'n_words': 180}