rogerCopy
@rogerCopy

Почему DomPDF (РНР 7.3.5.) генерирует не все PDF из списка?

Здравствуйте. Вот такое дело: получаю список ссылок на статьи вики, вот такой
https://ru.wikipedia.org/wiki/Узел_сети
https://ru.wikipedia.org/wiki/Устройство
https://ru.wikipedia.org/wiki/Клиент-сервер
https://ru.wikipedia.org/wiki/Интерфейс
https://ru.wikipedia.org/wiki/Компьютер
https://ru.wikipedia.org/wiki/Сервер_(аппаратное_о...
https://ru.wikipedia.org/wiki/Локальная_сеть
https://ru.wikipedia.org/wiki/Глобальная_сеть
https://ru.wikipedia.org/wiki/Компьютерная_сеть
https://ru.wikipedia.org/wiki/Протокол_передачи_данных
https://ru.wikipedia.org/wiki/USB


По нему с пом. домпдф получаю PDF-файлы. Но файл по последней статье (USB) не генерирует.

Показывает ошибки:
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 82
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 85
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 82
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 85
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 82
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 85
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 82
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 85
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 82
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 85
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 82
Notice: Undefined variable: title in /opt/lampp/htdocs/wikipdf.ru/index.php on line 85

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 327680 bytes) in /opt/lampp/htdocs/wikipdf.ru/dompdf/lib/Cpdf.php on line 2602


Текст программы:
<?php
error_reporting(E_ALL);
require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;
use Dompdf\Options;

$html = '';

//get initial page's html to grab needed links
$url = 'https://ru.wikipedia.org/wiki/Хост';
$htmlBasic = file_get_contents($url); 

$dom = new DOMDocument();
$dom->loadHTML($htmlBasic);

//get all a-tags from div.mw-body-content
$links = [];
$xPath = new DOMXPath($dom);
$anchorTags = $xPath->evaluate("//div[@class=\"mw-body-content\"]//a/@href");

//create an array[] of needed links to iterate trhough and to create PDF files from
foreach ($anchorTags as $anchorTag) {
    
    //if link is not already in array:    
    if ( !in_array($anchorTag->nodeValue, $links) ) {
      $links[] = $anchorTag->nodeValue;  
    }    
}

//handle the links for more usability
//get rid of garbage
foreach($links as $link) {

  //decode url to cyrillic
  $linkDecoded = urldecode($link);

  //if url contains garbage, delete 
  if ( (strpos($linkDecoded, 'Категори') === false) && (strpos($linkDecoded, 'Википедия') === false) && 
    (strpos($linkDecoded, 'index.php') === false) && (strpos($linkDecoded, 'Файл') === false) && 
    (strpos($linkDecoded, '#') === false) && (strpos($linkDecoded, 'Английский') === false) && 
    (strpos($linkDecoded, 'значения') === false) ) {
    
    //add 'https://ru.wikipedia.org' if needed
    if ($linkDecoded[0] === '/') {
      $fullLink  = trim('https://ru.wikipedia.org' . $linkDecoded);
 
      //PDF file title
      $title = substr($fullLink, strpos($fullLink, 'wiki/') +5); 

      echo $fullLink . '<br>';
      
      //get html of every article from array[]
      $html = file_get_contents($fullLink);
    }
  }
  
  //creating PDFs
  try {
    $options = new Options();
    $options->set('defaultFont', 'DejaVu Sans'); 
    $dompdf = new Dompdf($options);

    //an alleged workout to get images into pdf
    //according to https://github.com/dompdf/dompdf/wiki/Usage
    $context = stream_context_create([ 
      'ssl' => [ 
        'verify_peer' => FALSE, 
        'verify_peer_name' => FALSE,
        'allow_self_signed'=> TRUE 
      ] 
    ]);
    $dompdf->setHttpContext($context);

    //handle $html of an article
    $dompdf->loadHtml($html);
    $dompdf->setPaper('A4', 'landscape');
    $dompdf->render($title);

    $output = $dompdf->output();
    file_put_contents("/opt/lampp/htdocs/wikipdf.ru/$title.pdf", $output);
    
    //delete variable 
    unset($dompdf);

  } catch (Exception $e) { echo 'Выброшено исключение: ',  $e->getMessage(), "\n"; }    
}
  • Вопрос задан
  • 86 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
19 апр. 2024, в 05:01
999999 руб./за проект
19 апр. 2024, в 03:52
1000 руб./за проект
19 апр. 2024, в 03:01
1000 руб./за проект