- $containedHtml = $this->containHtml($html);
- $useWKHTML = config('snappy.pdf.binary') !== false && config('app.allow_untrusted_server_fetching') === true;
- if ($useWKHTML) {
- $pdf = SnappyPDF::loadHTML($containedHtml);
- $pdf->setOption('print-media-type', true);
- } else {
- $pdf = DomPDF::loadHTML($containedHtml);
+ $html = $this->containHtml($html);
+ $html = $this->replaceIframesWithLinks($html);
+ return $this->pdfGenerator->fromHtml($html);
+ }
+
+ /**
+ * Within the given HTML content, replace any iframe elements
+ * with anchor links within paragraph blocks.
+ */
+ protected function replaceIframesWithLinks(string $html): string
+ {
+ libxml_use_internal_errors(true);
+
+ $doc = new DOMDocument();
+ $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
+ $xPath = new DOMXPath($doc);
+
+
+ $iframes = $xPath->query('//iframe');
+ /** @var DOMElement $iframe */
+ foreach ($iframes as $iframe) {
+ $link = $iframe->getAttribute('src');
+ if (strpos($link, '//') === 0) {
+ $link = 'https:' . $link;
+ }
+
+ $anchor = $doc->createElement('a', $link);
+ $anchor->setAttribute('href', $link);
+ $paragraph = $doc->createElement('p');
+ $paragraph->appendChild($anchor);
+ $iframe->replaceWith($paragraph);