Sunday, February 28, 2010

Wikipedia to pamphlet with Firebug

I wanted to make converting the articles to pamphlets less cumbersome, and I found the following method.

After you find the article on wikipedia using Firefox, get the printable version (click the "Printable version" link in the left nav).

Open up Firebug and click the "CSS" tab.

Make sure you are using the commonPrint.css file and click "Edit".

Scroll slightly down and change the declaration for #toc to "display: none".

Scroll down about a quarter and change div.tright from "float:right; clear:right;" to "float:left;" leaving the rest of the declaration alone.

[Edit] Or you can create a bookmark of the following javascript to do this automatically:

javascript:function%20addCss(cssCode)%20{var%20styleElement%20=%20document.createElement("style");%20%20styleElement.type%20=%20"text/css";%20%20if%20(styleElement.styleSheet)%20{%20%20%20%20styleElement.styleSheet.cssText%20=%20cssCode;%20%20}%20else%20{%20%20%20%20styleElement.appendChild(document.createTextNode(cssCode));%20%20}%20%20document.getElementsByTagName("head")[0].appendChild(styleElement);};%20addCss('#toc%20{display:%20none}\n%20div.tright%20{float:left;%20clear:%20none;}\np%20{line-height:1.5}');

[/Edit]

Create a custom print setting by choosing File > Page Setup > Paper size > Manage Custom Settings. Set the attributes to:
* Width: 8.5 inch
* Height: 11.00 inch
* Top: 0
* Bottom: 0
* Left: 0
* Right: 1.9 in

Apply these settings before closing.

Print to postscript file (eg output.ps).

Convert this file to an imposed postscript file with the following script:

pstops
"4:1L@0.68(8in,.5in)+2L@0.68(8in,6in),0L@0.68(8in,6in)+3L@0.68(8in,.5in)"
output.ps outputImp.ps


Convert outputImp.ps to pdf with

ps2pdf outputImp.ps


And you are done!
[Edit]

You can also use the following Python script to get imposition of greater than 4 pages:

#!/usr/bin/python
import os, sys

ratio = '0.68'
x = '8'
top_y = '.5'
bottom_y = '6'

def get_page_count(file_name):
if not os.path.exists(file_name):
raise Exception(file_name + " is not a file")
file = open(file_name, 'r')
cnt = 0;
for line in file:
if '%%Page:' in line:
cnt += 1
return cnt

def best_modulus(x, y, m):
least, most = min(x,y), max(x,y)

if m % most == 0:
return most
if (m % least == 0) or (least - (m % least) < most - (m % most)):
return least
return most

def best_modulus_of_list(list, m):
best = list[0]
for item in list:
best = best_modulus(best, item, m)
return best


if __name__ == '__main__':
if len(sys.argv) < 2:
raise Exception("please give the postscript file name")
filename = sys.argv[1]
if filename.endswith('.ps'):
filename = filename[0:-3]

ps_name = filename + '.ps'
pages = get_page_count(ps_name)

best_mod = best_modulus_of_list((4,8,12,16),pages)
print pages

imp_name = filename + 'Imp.ps'
pdf_name = filename + 'Imp.pdf'

if os.path.exists(imp_name):
os.system('rm ' + filename + 'Imp.*')

pstops_cmd = 'pstops "' + str(best_mod) + ':'

for i in xrange(0, best_mod, 4):
if i > 0:
pstops_cmd += ','
increment = i / 2
pstops_cmd += '%sL@%s(%sin,%sin)' % (increment, ratio, x, bottom_y)
pstops_cmd += '+%sL@%s(%sin,%sin)' % (best_mod - 1 - increment, ratio, x, top_y)
pstops_cmd += ',%sL@%s(%sin,%sin)' % (increment + 1, ratio, x, top_y)
pstops_cmd += '+%sL@%s(%sin,%sin)' % (best_mod - 2 - increment, ratio, x, bottom_y)

pstops_cmd += '" ' + ps_name + ' ' + imp_name
print pstops_cmd
os.system(pstops_cmd)

os.system('ps2pdf ' + imp_name)


[/Edit]