How to create a series of PDF files in PHP 7.3.5 using domdpf?

Hello, I will try to explain:
PHP 7.3.5
dompdf latest version https://github.com/dompdf/

1. want to download Wikipedia.
1.1. take statue, for example $url = 'https://ru.wikipedia.org/wiki/Хост';
1.2. pumped need links to related articles from a specific div-a of article 1.1.:
get this:
5e27054f4176a373589286.png

1.3. next, I want to loop through each link with a picture to create a PDF, but it turns out only to make for the 1st link

here is the whole code:

<?php
// reference the namespace Dompdf
require_once 'dompdf/autoload.inc.php';

use Dompdf\Dompdf;
use Dompdf\Options;


//download image
/*function file_get_contents_curl($url) { 
 $ch = curl_init(); 

 curl_setopt($ch, CURLOPT_HEADER, 0); 
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
 curl_setopt($ch, CURLOPT_URL, $url); 

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

 return $data; 
} 

//$data = file_get_contents_curl( $url ); 
//$fp = 'logo-1.png'; 
//file_put_contents( $fp, $data ); 
//echo "File downloaded!"
*/

// instantiate and use the dompdf class
//get HTML by URL
$url = 'https://ru.wikipedia.org/wiki/Хост';
$html = file_get_contents($url);

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

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

foreach ($anchorTags as $anchorTag) {
 $links[] = $anchorTag->nodeValue;
}

foreach($links as $link) {

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

 //if the url contains unneeded words and delete them
 if ( (strpos($linkDecoded, 'Category') === false) && (strpos($linkDecoded, 'Wikipedia') === false) && 
 (strpos($linkDecoded, 'index.php') === false) && (strpos($linkDecoded, 'File') === false) && 
 (strpos($linkDecoded, '#') === false) ) {

 //if $linkDecoded[0] === '/' -> add 'https://ru.wikipedia.org'
 if ($linkDecoded[0] === '/') {
 $linkDecoded = 'https://wikipedia.ru.org' . $linkDecoded . '<br>';

 // 5 === len(wiki/); https://stackoverflow.com/questions/11290279/get-everything-after-word
 $title = substr($linkDecoded, strpos($linkDecoded, 'wiki/') +5); 

 echo $linkDecoded;

 //DOMpdf -> pdf
 $options = new Options();

 $options->set('defaultFont', 'DejaVu Sans');

 //wasn't here him if that to remove
 $html = file_get_contents($url);

 $dompdf = new Dompdf($options);
$dompdf->loadHtml($html);

 //(Optional) Setup the paper size and orientation
 $dompdf->setPaper('A4', 'landscape');
 // Render the HTML as PDF
$dompdf->render();

 // Output the generated PDF to the Browser
 //title is basename === https://wikipedia.ru.org/wiki/ -> Protocolversion
 $dompdf->stream($title); 
 } 
}
}


someone tell me how to make a PDF with all the links of the picture?
April 4th 20 at 13:12
2 answers
April 4th 20 at 13:14
Solution
the problem is that the render method generates return and it turns out that in a loop works only one turn. you need a loop to collect message html using string concatenation and then one big html file for conversion to pdf
around so you must be
$html = ";

foreach($links as $link) {

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

 //if the url contains unneeded words and delete them
 if ( (strpos($linkDecoded, 'Category') === false) && (strpos($linkDecoded, 'Wikipedia') === false) && 
 (strpos($linkDecoded, 'index.php') === false) && (strpos($linkDecoded, 'File') === false) && 
 (strpos($linkDecoded, '#') === false) ) {

 //if $linkDecoded[0] === '/' -> add 'https://ru.wikipedia.org'
 if ($linkDecoded[0] === '/') {
 $linkDecoded = 'https://wikipedia.ru.org' . $linkDecoded . '<br>';

 // 5 === len(wiki/); https://stackoverflow.com/questions/11290279/get-everything-after-word
 $title = substr($linkDecoded, strpos($linkDecoded, 'wiki/') +5); 

 echo $linkDecoded;



 //wasn't here him if that to remove
 $html .= file_get_contents($url);
 } 
}
}

//DOMpdf -> pdf
 $options = new Options();

 $options->set('defaultFont', 'DejaVu Sans');

$dompdf = new Dompdf($options);
$dompdf->loadHtml($html);

 //(Optional) Setup the paper size and orientation
 $dompdf->setPaper('A4', 'landscape');
 // Render the HTML as PDF
$dompdf->render();

 // Output the generated PDF to the Browser
 //title is basename === https://wikipedia.ru.org/wiki/ -> Protocolversion
 $dompdf->stream($title);
thanks , as always, summed newinternet:
instead
$linkDecoded = 'https://wikipedia.ru.org' . $linkDecoded;


need
$linkDecoded = 'https://<b>ru.wikipedia.org</b>' . $linkDecoded;
- marcel.Kris commented on April 4th 20 at 13:17
April 4th 20 at 13:16
thanks , as always, summed newinternet:
instead
$linkDecoded = 'https://wikipedia.ru.org' . $linkDecoded;


need
$linkDecoded = 'https://ru.wikipedia.org' . $linkDecoded;

Find more questions by tags PHPPDF