Saturday, 20 August 2011

Comics to PDF

Hi Readers,

I'm not a comis guy but there are some I'm really interested to follow. The more I read online the more I realize it's a horrible experience for consuming.



I was checking the image format and fortunately it had a sequence number: comic_#.png, where # is the number of the comic. So I created a small script that shows only the images in a plain html page:

<style>body {background: #000;}</style>
<center>
<?php
$from = $_GET['from'];
$to   = $_GET['to'];
for ($i = $from; $i <= $to; $i++) {
  echo '<img src="SOURCE_URL' . $i . '.png" /><br />';
} 
?>
</center>

That was much better already. But then I got the feeling I want it on my iPhone/iPad. But from the browser you would have to pinch each image, again, horrible ux.

Let's create a PDF. First you need the source, so I downloaded all the images with cURL's sequence feature:

curl -O http://URL_TO_COMICS/comic_[1-3000].png

Now I need a tool that compose all into one single PDF. I played with the built in Preview app, if you open the first image, save as PDF, and then drag the rest onto the first slide you can save it, but it gives a terrible margin around the images. I was looking for apps online, but no one could make it without margin - else it wasn't free neither.
So I downloaded fPDF and compose this little script that makes my perfect PDF of comics:

<?php

// Call: script.php?from=1&to=3000

require_once 'PATH_TO_FPDF_LIB/fpdf.php';

$from = $_GET['from'];
$to   = $_GET['to'];

$pixel_rate = 3.778; // Don't ask, just use it ;)

$pdf = new FPDF();

for ($i = $from; $i <= $to; $i++) {
  $imagesize = getimagesize('comic_' . $i . '.png');
  $pdf->AddPage('P', array(($imagesize[0] / $pixel_rate), ($imagesize[1] / $pixel_rate)));
  $pdf->Image('comic_' . $i . '.png', 0, 0);
}

$pdf->Output();

It's actually faster than I thought, creating a document of 500 slides takes ~2 seconds. Pretty good.

Cheers,
Peter

No comments:

Post a Comment

Note: only a member of this blog may post a comment.