Skip to content

Instantly share code, notes, and snippets.

@christianmagill
Created December 31, 2016 17:23
Show Gist options
  • Save christianmagill/f3ffa60648729eca8d334159406cf55c to your computer and use it in GitHub Desktop.
Save christianmagill/f3ffa60648729eca8d334159406cf55c to your computer and use it in GitHub Desktop.
<?php
namespace Roots\Sage\PDF;
use Roots\Sage\Assets;
/*
add_filter( 'query_vars', function ( $query_vars ) {
$query_vars[] = 'pdf';
return $query_vars;
} );
add_action( 'init', function(){
});
*/
add_action( 'template_redirect', function () {
// todo: add conditional
include dirname( __FILE__, 2 ) . "/vendor/pdflayer.class.php";
$stylesheet = Assets\asset_path( 'styles/pdf.css' );
$stylesheet = str_replace( 'https://influencebydesign.dev', 'http://staging.influencebydesign.com', $stylesheet);
$logo = Assets\asset_path( 'images/logo.svg' );
$logo = str_replace( 'https://influencebydesign.dev', 'http://staging.influencebydesign.com', $logo);
$header_html = "
<html>
<head>
<link rel='stylesheet' href='$stylesheet'>
<style>
@page {size:8.5in 2in; margin: 0}
</style>
</head>
<body class='header'>
<div class='content'>
<h1>Header [page] of [sitepages]</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus asperiores aut debitis eligendi ex modi neque obcaecati omnis, perspiciatis porro.</p><p>Amet, animi, at aut blanditiis dicta dignissimos dolore eos magnam quidem quis ratione sint sit vero. Incidunt ipsam nesciunt sit?</p><p>Accusantium ad beatae cupiditate deserunt ea impedit inventore magni nemo nesciunt sit! Autem deserunt dolore officiis optio possimus. Impedit, quae.</p><p>Accusantium adipisci aliquam aut dignissimos ea facilis iure laudantium nam neque, numquam officia officiis optio ratione sed vel vitae voluptas.</p><p>Aspernatur illum labore maiores natus quia repellat, voluptates? Accusamus, doloribus ipsa laborum maiores nesciunt pariatur quibusdam repellat. Ab, amet tempore?</p>
</div>
</body>
</html>
";
$document_html = "
<html>
<head>
<link rel='stylesheet' href='$stylesheet'>
</head>
<body class='document'>
<div class='content'>
<img src='$logo' alt='' width='368' height='69'>
<h1>Content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus asperiores aut debitis eligendi ex modi neque obcaecati omnis, perspiciatis porro.</p><p>Amet, animi, at aut blanditiis dicta dignissimos dolore eos magnam quidem quis ratione sint sit vero. Incidunt ipsam nesciunt sit?</p><p>Accusantium ad beatae cupiditate deserunt ea impedit inventore magni nemo nesciunt sit! Autem deserunt dolore officiis optio possimus. Impedit, quae.</p><p>Accusantium adipisci aliquam aut dignissimos ea facilis iure laudantium nam neque, numquam officia officiis optio ratione sed vel vitae voluptas.</p><p>Aspernatur illum labore maiores natus quia repellat, voluptates? Accusamus, doloribus ipsa laborum maiores nesciunt pariatur quibusdam repellat. Ab, amet tempore?</p>
</div>
</body>
</html>
";
$footer_html = "
<html>
<head>
<link rel='stylesheet' href='$stylesheet'>
</head>
<body class='footer'>
<div class='content'>
<h1>Footer</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus asperiores aut debitis eligendi ex modi neque obcaecati omnis, perspiciatis porro.</p><p>Amet, animi, at aut blanditiis dicta dignissimos dolore eos magnam quidem quis ratione sint sit vero. Incidunt ipsam nesciunt sit?</p><p>Accusantium ad beatae cupiditate deserunt ea impedit inventore magni nemo nesciunt sit! Autem deserunt dolore officiis optio possimus. Impedit, quae.</p><p>Accusantium adipisci aliquam aut dignissimos ea facilis iure laudantium nam neque, numquam officia officiis optio ratione sed vel vitae voluptas.</p><p>Aspernatur illum labore maiores natus quia repellat, voluptates? Accusamus, doloribus ipsa laborum maiores nesciunt pariatur quibusdam repellat. Ab, amet tempore?</p>
</div>
</body>
</html>
";
$html2pdf = new \pdflayer();
$html2pdf->set_params( [
'test' => 1,
'header_html' => $header_html,
'document_html' => $document_html,
// 'footer_html' => $footer_html,
'custom_unit' => 'in',
'margin-top' => 0,
'margin_right' => 0,
'margin_bottom' => 0,
'margin_left' => 0,
'page_size' => 'Letter',
// 'dpi' => 300
] );
$html2pdf->convert();
// print_r($html2pdf);
$html2pdf->display_pdf();
exit;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment