private function getContents($pdf_file) { $this->locked = true; // echo $file; $info = new Pdf($pdf_file); $pdf = new Base($pdf_file, array('singlePage' => true, 'noFrames' => false)); $pages = $info->getPages(); // print_r($pages); // print_r($pages); $random_dir = uniqid(); $outputDir = Config::get('pdftohtml.output', dirname(__FILE__) . '/../output/' . $random_dir); if (!file_exists($outputDir)) { mkdir($outputDir, 0777, true); } $pdf->setOutputDirectory($outputDir); $pdf->generate(); $fileinfo = pathinfo($pdf_file); $base_path = $pdf->outputDir . '/' . $fileinfo['filename']; $contents = array(); for ($i = 1; $i <= $pages; $i++) { $content = file_get_contents($base_path . '-' . $i . '.html'); if ($this->inlineCss()) { // $content = str_replace(array('<!--','-->'),'',$content); $parser = new Emogrifier($content); // print_r($parser); $content = $parser->emogrify(); } file_put_contents($base_path . '-' . $i . '.html', $content); $contents[$i] = file_get_contents($base_path . '-' . $i . '.html'); } $this->contents = $contents; $this->goToPage(1); }
/** * Replaces the default mail handling with a check for inline styles. If found * we run the email through emogrifier to inline the styles. * @param bool $isPlain * @return $this */ protected function parseVariables($isPlain = false) { parent::parseVariables($isPlain); // if it's an html email, filter it through emogrifier if (!$isPlain && preg_match('/<style[^>]*>(?:<\\!--)?(.*)(?:-->)?<\\/style>/ims', $this->body, $match)) { $css = $match[1]; $html = str_replace(array("<p>\n<table>", "</table>\n</p>", '© ', $match[0]), array("<table>", "</table>", '', ''), $this->body); $emog = new Emogrifier($html, $css); $this->body = $emog->emogrify(); } return $this; }
/** * Inline both the embedded css, and css from an external file, into html * * @param HTML $htmlContent * @param string $cssFile path and filename * @return HTML with inlined CSS */ public static function convert($htmlContent, $cssfile) { $emog = new Emogrifier($htmlContent); // Apply the css file to Emogrifier if ($cssfile) { $cssFileLocation = join(DIRECTORY_SEPARATOR, array(Director::baseFolder(), $cssfile)); $cssFileHandler = fopen($cssFileLocation, 'r'); $css = fread($cssFileHandler, filesize($cssFileLocation)); fclose($cssFileHandler); $emog->setCss($css); } return $emog->emogrify(); }
/** * Inline the CSS before an email is sent. * * @param \Swift_Events_SendEvent $evt */ public function beforeSendPerformed(\Swift_Events_SendEvent $evt) { $message = $evt->getMessage(); if ($message->getContentType() === 'text/html' || $message->getContentType() === 'multipart/alternative' && $message->getBody()) { $this->inliner->setHtml($message->getBody()); $message->setBody($this->inliner->emogrify()); } foreach ($message->getChildren() as $part) { if (strpos($part->getContentType(), 'text/html') === 0) { $this->inliner->setHtml($part->getBody()); $message->setBody($this->inliner->emogrify()); } } }
/** * @test */ public function emogrifierNotMapsCssToHtmlIfFeatureIsNotEnabled() { $css = 'img {float: right;}'; $this->subject->setHtml($this->html5DocumentType . '<html><body><img></body></html>'); $this->subject->setCss($css); $html = $this->subject->emogrify(); self::assertNotContains('<img align="right', $html); }
/** * @test * @param string $attribute the class attribute value * @dataProvider cssClassesWithWhitespaceDataProvider */ public function emogrifierMapsClassWithWiteSpaceInAttribute($attribute) { $css = '.test {color: red;}'; $this->subject->setHtml($this->html5DocumentType . '<html><body><div class="' . $attribute . '"></div></body></html>'); $this->subject->setCss($css); $html = $this->subject->emogrify(); self::assertContains('style="color: red;"', $html); }
/** * @test */ public function emptyMediaQueriesAreRemoved() { $emptyQuery = '@media all and (max-width: 500px) { }'; $this->subject->setCss($emptyQuery); $this->subject->setHtml($this->html5DocumentType . '<html><body><p></p></body></html>'); $result = $this->subject->emogrify(); self::assertNotContains($emptyQuery, $result); }
/** * @test * @param string $body The HTML * @param string $css The complete CSS * @param string $tagName The name of the tag that should be modified * @param string $attributes The attributes that are expected on the element * * @dataProvider cssToHtmlMappingDataProvider */ public function emogrifierMapsCssToHtml($body, $css, $tagName, $attributes) { $this->subject->setHtml($this->html5DocumentType . '<html><body>' . $body . '</body></html>'); $this->subject->setCss($css); $this->subject->enableCssToHtmlMapping(); $html = $this->subject->emogrify(); self::assertContains('<' . $tagName . ' ' . $attributes, $html); }
/** * @test */ public function emogrifierAddsCopyOfCssStylesAsStyleNode() { $this->subject->appendStylesToHead(); $this->subject->setCss('p { color: blue; }'); $this->subject->setHtml($this->html5DocumentType . '<html><body><style type="text/css">p { padding: 10px; }</style><p></p></body></html>'); $result = $this->subject->emogrify(); self::assertContains('p { color: blue; }', $result); self::assertContains('p { padding: 10px; }', $result); }
/** * @test * @param string $dataUriMediaType * @dataProvider dataUriMediaTypeDataProvider */ public function dataUrisAreConserved($dataUriMediaType) { $html = $this->html5DocumentType . '<html></html>'; $this->subject->setHtml($html); $styleRule = 'background-image: url(data:image/png' . $dataUriMediaType . ',iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAABUk' . 'lEQVQ4y81UsY6CQBCdWXBjYWFMjEgAE0piY8c38B9+iX+ksaHCgs5YWEhIrJCQYGJBomiC7lzhVcfqEa+5KXfey3s783bRdd00TR' . 'VFAQAAICJEhN/q8Xjoug7D4RA+qsFgwDjn9QYiTiaT+Xx+OByOx+NqtapjWq0WjEajekPTtCAIiIiIyrKMoqiOMQxDlVqyLMt1XQ' . 'A4nU6z2Wy9XkthEnK/3zdN8znC/X7v+36WZfJ7120vFos4joUQRHS5XDabzXK5bGrbtu1er/dtTFU1TWu3202VHceZTqe3242Itt' . 'ut53nj8bip8m6345wLIQCgKIowDIuikAoz6Wm3233mjHPe6XRe5UROJqImIWPwh/pvZMbYM2GKorx5oUw6m+v1miTJ+XzO8/x+v7' . '+UtizrM8+GYahVVSFik9/jxy6rqlJN02SM1cmI+GbbQghd178AAO2FXws6LwMAAAAASUVORK5CYII=);'; $this->subject->setCss('html {' . $styleRule . '}'); $result = $this->subject->emogrify(); self::assertContains('<html style="' . $styleRule . '">', $result); }
/** * @test */ public function ignoresWhitespaceAfterImportant() { $css = 'p { margin: 1px !important' . self::LF . ' ;}'; $html = $this->html5DocumentType . self::LF . '<html><head</head><body><p style="margin: 2px; ">some content</p></body></html>'; $expected = '<p style="margin: 1px !important;">'; $this->subject->setHtml($html); $this->subject->setCss($css); self::assertContains($expected, $this->subject->emogrify()); }
/** * @test */ public function emogrifyMergesCssWithMixedUnits() { $css = 'p { margin: 1px; padding-bottom:0;}'; $html = $this->html5DocumentType . self::LF . '<html><head><style>#topWrap p {margin:0;padding-bottom: 1px;}</style></head>' . '<body><div id="topWrap"><p style="text-align: center;">some content</p></div></body></html>'; $expected = '<p style="text-align: center; margin: 0; padding-bottom: 1px;">'; $this->subject->setHtml($html); $this->subject->setCss($css); $this->assertContains($expected, $this->subject->emogrify()); }
/** * @param string $html * @param string $css * @param bool $keepHiddenNodes * @param bool $disableBackupCssNode * @param bool $disableInlineStyleAttributes * @param bool $appendStylesToHead * @return string */ public function process($html, $css, $keepHiddenNodes = true, $disableBackupCssNode = false, $disableInlineStyleAttributes = false, $appendStylesToHead = false) { $inliner = new Emogrifier($html, $css); if ($keepHiddenNodes) { $inliner->disableInvisibleNodeRemoval(); } if ($disableBackupCssNode) { $inliner->disableBackupCssNode(); } if ($disableInlineStyleAttributes) { $inliner->disableInlineStyleAttributesParsing(); } if ($appendStylesToHead) { $inliner->appendStylesToHead(); } return $inliner->emogrify(); }
/** * @param array $data * * @throws \TijsVerkoyen\CssToInlineStyles\Exception */ protected function setHtml(array $data) { $html = view('emails.' . $this->template, ['user' => $this->user, 'data' => $data])->render(); $css = file_get_contents(asset('assets/css/bootstrap.css')); libxml_use_internal_errors(true); $emogrifier = new Emogrifier(); $emogrifier->setHtml($html); $emogrifier->setCss($css); $this->html = $emogrifier->emogrify(); }
/** * The method replaces css class to inline css rules. * @param $content * @return string */ private function setInlineCss($content) { $content = str_replace(['<!--', '-->'], '', $content); $parser = new Emogrifier($content); return $parser->emogrify(); }