Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created March 3, 2009 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save edvakf/73183 to your computer and use it in GitHub Desktop.
Save edvakf/73183 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'base64'
require 'uri'
id = $$
#id = Time.now.strftime('%Y%m%d%H%M')
pdffile = ARGV[1] ? ARGV[1] : ARGV[0]
tmpfile = "/tmp/pdf_#{id}_%02d.svg"
puts tmpfile
system("/opt/local/bin/pdf2svg \"#{pdffile}\" \"#{tmpfile}\" all")
#system("open \"#{tmpfile.sub(/%02d/,'001')}\"")
html = <<_END
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Converted from PDF : #{File.basename(pdffile)}</title>
<style>
@media print{
.svg{
display:inline !important;
}
.pagenum{
display:none !important;
}
}
</style>
<script type="text/javascript">
(function(){
var st = document.createElement('style');
st.type = 'text/css';
st.textContent = '.svg{display:none;}';
document.documentElement.appendChild(st);
window.onload = function(){
document.getElementsByTagName('img')[0].style.display = 'inline';
};
})()
toggleShow = function(self){
var id = self.getAttribute('href').replace(/^#/,'page');
var svg = document.getElementById(id);
if(svg.style.display != 'inline'){
svg.style.display = 'inline';
}else{
svg.style.display = 'none';
}
}
openPage = function(self){
window.open(self.getElementsByTagName('img')[0].src);
}
</script>
</head>
<body>
<h1>Converted from PDF : #{File.basename(pdffile)}</h1>
<!-- svgs -->
</body>
</html>
_END
i = 1
while(true)
begin
file = File.open("#{sprintf(tmpfile,i)}")
rescue => e
puts e
break
end
num = sprintf("%02d",i)
#svg = 'data:image/svg+xml,'+URI.escape(file.read)
svg = 'data:image/svg+xml;base64,' + Base64.encode64(file.read.gsub("\n","")).gsub("\n","")
html.sub!('<!-- svgs -->',<<_END)
<h2 class=\"pagenum\">
<a href=\"##{num}\" onclick=\"toggleShow(this);\">
page #{num}
</a>
</h2>
<p>
<a onclick=\"void(openPage(this));\" href=\"#\">
<img alt=\"image #{num}\" id=\"page#{num}\" class=\"svg\" src=\"#{svg}\" />
</a>
</p>
<!-- svgs -->
_END
file.close
File.delete("#{sprintf(tmpfile,i)}")
i += 1
end
File.open("/tmp/svg_#{id}.html","w") do |f|
f.print html
end
system("open #{"/tmp/svg_#{id}.html"}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment