Skip to content

Instantly share code, notes, and snippets.

@alex3305
Last active November 12, 2019 07:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex3305/c1f402602d810bc93c70 to your computer and use it in GitHub Desktop.
Save alex3305/c1f402602d810bc93c70 to your computer and use it in GitHub Desktop.
HTML to PDF converter with marginless cover page with Snappy and PDF Merge
<?php
/**
* PDF exporter for Mediawiki. This will load a specified content page and
* will append a seperate, specified cover page to the returning document.
*
* Requirements:
* - PHP 5.1+
* - wkhtmltox (wkhtmltopdf) 0.12+ (http://www.wkhtmltopdf.org)
* - KnpLabs Snappy (https://github.com/KnpLabs/snappy/)
* - PDF Merger for PHP (http://pdfmerger.codeplex.com/)
*
* Input:
* @param page URLEncoded String Page to be loaded.
*
* @author Alex van den Hoogen
* @copyright (c) 2014, Alex van den Hoogen
*/
/** Load Snappy, wkhtmltopdf and PDF Merger for PHP. */
require_once 'PDFMerger.php';
require_once 'autoload.php';
use Knp\Snappy\Pdf;
$coverpage = new Pdf('/usr/local/bin/wkhtmltox/bin/wkhtmltopdf');
$content = new Pdf('/usr/local/bin/wkhtmltox/bin/wkhtmltopdf');
$pdf = new PDFMerger;
/** Parse GET input */
$page = filter_input(INPUT_GET, 'page');
if (empty($page)) { ?>
<DOCTYPE html>
<html>
<head>
<title>PDF downloader</title>
<meta name="googlebot" content="noarchive,nofollow,noindex,noodp,nosnippit">
<meta name="robots" content="noarchive,nofollow,noindex,noodp,nosnippit">
<style type="text/css">
body {color: #000; background: #eee; font: 12pt Verdana, Arial, Helvetica, sans-serif; margin: 0 auto; padding: 0; text-align: center;}
#content {width: 500px; height: 300px; background: #fff; top: 50px; position: absolute; box-sizing: border-box; padding: 60px 30px; left: 50%; margin-left: -250px; box-shadow: 0 0 8px #888; border-radius: 4px;}
#content ul {list-style-type: none; margin: 0; padding: 0;}
#content li {margin: 20px 0;}
#content h1 {margin: 0; padding: 0;}
</style>
<script type="text/javascript">
function viewPdf() {
var value = document.getElementById('document').value;
window.location.replace(document.URL + '?page=' + value);
}
</script>
</head>
<body>
<div id='content'>
<h1>Mediawiki download PDF</h1>
<ul>
<!-- You can add more items here. -->
<li><input type="text" name="document" id="document" placeholder="Name of wiki page" required>
<input type="button" name="view" id="view" value="Download page" onclick="viewPdf();"></li>
</ul>
</div>
</body>
</html>
<?php exit; }
/** Set headers */
header('Content-Type: application/pdf');
/** Set cover page and write to disk */
$coverpage->setOption('no-background', false);
$coverpage->setOption('background', true);
$coverpage->setOption('disable-javascript', true);
$coverpage->setOption('print-media-type', false);
$coverpage->setOption('margin-bottom', 0);
$coverpage->setOption('margin-left', 0);
$coverpage->setOption('margin-right', 0);
$coverpage->setOption('margin-top', 0);
# The cover is just a simple HTML file with some PHP variables that are replaced by the title of the page.
$fp = fopen(getcwd() . '/cover.pdf', 'w');
fwrite($fp, $coverpage->getOutput("pdf/cover.php?title=$page"));
fclose($fp);
/** Set content options and write to disk */
# Set a custom cookie, so wkhtmltopdf can login to your wiki! Get this information from your browser.
$content->setOption('cookie',
array('WIKNAME_wikiToken' => '',
'WIKINAME_wikiUserID' => '',
'WIKINAME_wikiUserName' => ''));
$content->setOption('no-background', false);
$content->setOption('background', true);
$content->setOption('disable-javascript', false);
$content->setOption('print-media-type', true);
$content->setOption('margin-bottom', '2.54cm');
$content->setOption('margin-left', '2.54cm');
$content->setOption('margin-right', '2.54cm');
$content->setOption('margin-top', '2.54cm');
$content->setOption('header-spacing', '8');
$content->setOption('footer-spacing', '8');
$content->setOption('header-left', str_replace('_', " ", $page));
$content->setOption('header-right', 'Author');
$content->setOption('footer-left', 'Document');
$content->setOption('footer-right', 'Page [page] / [topage]');
$fp = fopen(getcwd() . '/content.pdf', 'w');
fwrite($fp, $content->getOutput("http://YOURWIKI.EXAMPLE.COM/index.php/$page"));
fclose($fp);
/** Join both PDFs and export to browser. */
$pdf->addPDF('cover.pdf', '1')
->addPDF('content.pdf', 'all')
->merge('browser', 'output.pdf');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment