Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS 

Auto download pdf from www.estado.com.br

// This script downloads all pdfs from http://jpdf.estado.com.br
// Note, you must subscribe this newspaper in order to download the pdfs
// You must have wget installed

export http_proxy="http://10.1.1.1:8000"

cookie='sMkjwKA67H8FDcsZX5'
dd=`date +%d`
mm=`date +%m`
yyyy=`date +%Y`

index="http://jpdf.estado.com.br/menupdfi.php?E=SP&D=$dd/$mm/$yyyy&A=/estadopdf/sp/paginas/$yyyy/$mm/$dd/A01.pdf"

rm index.txt
./wget/wget -nc -k -S -U Mozilla --proxy --header "Cookie: User=$cookie " -O index.txt $index
if [ ! -f index.txt ]; then exit 1; fi

l=`gawk  'BEGIN {FS="\""} /option VALUE="\/estadopdf/ { print $2 }' index.txt`

for x in $l; do 

	# Ignora os classificados
	if [ ${x%01.pdf} -eq "Cl" ]; then continue; fi
	# Ignora o Guia
	if [ ${x%01.pdf} -eq "Q" ]; then continue; fi

	y=http://jpdf.estado.com.br${x%01.pdf}
	i=1
	flag=0
	
	while [ $i -lt 40 ]; do
		filename=`printf "%s%02d.pdf\n" $y $i`

		echo "=================================================================="
		echo $filename
		echo "=================================================================="
		./wget/wget -P estado -nc -k -S -U Mozilla --proxy --header "Cookie: User=$cookie " $filename
		if [ $? -eq 1 ]; then
			let flag=flag+1
			if [ $flag -gt 1 ]; then 
				flag=0
				echo "Proximo caderno..."; break;
			fi 
		fi
		
		sleep 1
		let i=i+1
	done
done

pman -- create, print, save, view PDF man pages

pman -- create, print, save, view PDF man pages

Author: ntk
License: The MIT License, Copyright (c) 2007 ntk
Description: (batch) convert man pages into PDF documents and save them to a specified directory; (batch) print or view PDF man pages from the command line
Platform: Mac OS X 10.4.10; man bash
Installation: put pman() into ~/.bash_login (or alternatives)





# Usage:
   
pman ls; pman getopts                                 # convert a singel man page to a PDF file, save and open it
pman 8 sticky                                         # same with manual section number
pman m toe                                     
pman -b ls 'open(2)' dd "chmod(2)" curl 'open(n)'     # batch convert man pages into PDF files
pman -p rm srm open\(2\) 'toe(m)' 'ncurses(3)'        # print man pages using the default printer



pman() {

section="$1"    
manpage="$2"    

mandir="/Users/Shared/manpages"    #  save the created PDF man pages to the specified directory


# batch process man pages to PDF files with the "-b" switch and save them to $mandir
# example: pman -b ls 'open(2)' dd 'chmod(2)' 'open(n)' 'sticky(8)'
# cf. man -aW open for "man n open"

if [[ "$1" = "-b" ]]; then         

if [[ ! -d $mandir ]]; then        
   mkdir -p $mandir
   chmod 1777 $mandir
fi

shift   # remove "-b" from "$@"

for manfile in "$@"; do 

# example for $manfile: open(2)
manpage="`echo $manfile | grep -Eos '^[^\(]+'`"                              # extract name of man page
section="`echo $manfile | grep -Eos '\([^\)]+\)' | grep -Eos '[^\(\)]+'`"    # extract section of man page

if [[ ! "$section" ]]; then
   section="1"
fi

if [[ ! -f "`man ${section} -W ${manpage} 2>/dev/null`" ]]; then
#if [[ ! -f "`man -W ${section} ${manpage} 2>/dev/null `" ]]; then
   echo "No such man page: man ${section} ${manpage}"
   continue
fi

manfile="${mandir}/${manpage}(${section}).pdf"
echo "$manfile"

if [[ ! -f "$manfile" ]]; then
   man $section -t $manpage 2>/dev/null | pstopdf -i -o "$manfile" 2>/dev/null
   chmod 1755 "$manfile"
   # hide file extension .pdf
   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi
fi

done

return 0
   
fi          # END of batch processing man pages to PDF files



# print PDF man pages using the default printer (see man lpr and man lpoptions)
# if necessary, create the specified PDF man pages and save them to $mandir
# example: pman -p rm srm

if [[ "$1" = "-p" ]]; then         

if [[ ! -d $mandir ]]; then        
   mkdir -p $mandir
   chmod 1777 $mandir
fi

shift   # remove "-p" from "$@"

for manfile in "$@"; do 

# example for $manfile: open(2)
manpage="`echo $manfile | grep -Eos '^[^\(]+'`"                              # extract name of man page
section="`echo $manfile | grep -Eos '\([^\)]+\)' | grep -Eos '[^\(\)]+'`"    # extract section of man page

if [[ ! "$section" ]]; then
   section="1"
fi

if [[ ! -f "`man ${section} -W ${manpage} 2>/dev/null`" ]]; then
   echo "No such man page: man ${section} ${manpage}"
   continue
fi

manfile="${mandir}/${manpage}(${section}).pdf"
echo "$manfile"

if [[ ! -f "$manfile" ]]; then
   man -t $section $manpage 2>/dev/null | pstopdf -i -o "$manfile" 2>/dev/null
   chmod 1755 "$manfile"
   # hide file extension .pdf
   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi
   lpr "$manfile"
else
   lpr "$manfile"
fi

done

return 0
   
fi          # END of printing man pages using the default printer



# convert a single man page to a PDF file, save it to $mandir and then open it in a PDF viewer

if [[ -z "$1" ]] || [[ $# -gt 2 ]]; then       # check number of arguments
#if [[ -z "$1" || $# -gt 2 ]]; then  
#if [ -z "$1" -o $# -gt 2 ]; then
  echo "Wrong number of arguments!" 
  return 1
fi 

if [[ ! "$manpage" ]]; then     # turn "pman ls" into "pman 1 ls"
   manpage="$section"         # if $manpage is an empty string because there has been no "$2" then $manpage is set to "$section" and ...
   section="1"                # ... $section is set to "1"
fi                            

if [[ ! -f "`man ${section} -W ${manpage} 2>/dev/null`" ]]; then
   echo "No such man page: man ${section} ${manpage}"
   return 1
fi

if [[ ! -d $mandir ]]; then
   mkdir -p $mandir
   chmod 1777 $mandir
fi

manfile="${mandir}/${manpage}(${section}).pdf"

if [[ -f "$manfile" ]]; then
   open "$manfile"
else
   man $section -t $manpage 2>/dev/null | pstopdf -i -o "$manfile" 2>/dev/null
   chmod 1755 "$manfile"
   # hide file extension .pdf
   if [[ -f /Developer/Tools/SetFile ]]; then /Developer/Tools/SetFile -a E "$manfile"; fi
   open "$manfile"
fi

return 0

}



PDF Word Link

// Find Word(s) on Current Page and add link

var numWords = this.getPageNumWords(this.pageNum);
p = this.pageNum;

for ( var i=0; i<numWords; i++){
    var ckWord =  this.getPageNthWord(p,i,true);;

    if(ckWord == "Solo")
    {
        var q = this.getPageNthWordQuads(p,i);

        m = (new Matrix2D).fromRotated(this,p);
        mInv = m.invert();
        r = mInv.transform(q);
        r = r.toString();
        r = r.split(",");
        l = addLink(p, [r[4], r[5], r[2], r[3]]);
        l.borderColor = color.red;
        l.borderWidth = 1;
        l.setAction("this.getUrl('http://www.adobe.com/');");

    }


}

Simple % bar in a pdf using Ruby

Will create a bar, filled to the percentage provided, and draw it in a PDF.

Requires PDF Writer, call method after setting up PDF to @pdf

  def generate_pdf_bar(p = 50, x = 10, y = 'center', w = 200, h = 10)
     # adjustments
    if y == 'center'
      y = @pdf.page_height / 2 - h
    else
      y = @pdf.page_height - y - h
    end
    p = w / 100 * p

      # Empty
    @pdf.stroke_color  Color::RGB::Black
    @pdf.rectangle(x, y, w, h).close_stroke

     # Fill
    @pdf.fill_color    Color::RGB::Black
    @pdf.stroke_color  Color::RGB::Black
    @pdf.rectangle(x, y, p, h).close_fill_stroke
  end

Export Adobe FDF and XFDF from ActiveRecord

I needed to export XFDF from an application. This code is kind of untested, but is based on the solution I came up with. I would appreciate responses/modifications. It's very straightforward mixin stuff, for the most part.
# (X)FDF Export for ActiveRecord
# Based on Justin Koivisto's FDF library for PHP
# Author: Sean Cribbs, seancribbs_AT_gmail_DOT_com, http://seancribbs.com
module FDF
  def self.included(base)
    base.extend ClassMethods
  end
  
  module ClassMethods
    # Options:
    #  <tt>:filename</tt> - The filename of the associated PDF document.  REQUIRED!
    #  <tt>:indentation</tt> - How much to indent the resulting XFDF (XML)
    #  <tt>:include</tt> - Which associated models to include in the generated XFDF.
    #  <tt>:exclude_attributes</tt> - Which attributes of the current model should not be exported.  By default all non-internal attributes are exported (i.e. everything but _id fields).
    #  <tt>:include_attributes</tt> - Which attributes of the current model should be exported in addition to the default. By default all non-internal attributes are exported (i.e. everything but _id fields).
    #  <tt>:attributes</tt> - Override which attributes to export.
    def exports_xfdf(options = {})
      raise ArgumentError, "A :filename option must be specified." unless options[:filename]
      options[:indentation] ||= 2
      options[:include] = options[:include].is_a?(Array) ? options[:include] : [options[:include]].compact
      unless included_modules.include? XFDFMethods
        class_inheritable_accessor :xfdf_options
        extend ClassMethods
        include XFDFMethods
      end
      self.xfdf_options = options
    end
    
    # Options:
    #  <tt>:filename</tt> - The filename of the associated PDF document.  REQUIRED!
    #  <tt>:include</tt> - Which associated models to include in the generated FDF.
    #  <tt>:exclude_attributes</tt> - Which attributes of the current model should not be exported.  By default all non-internal attributes are exported (i.e. everything but _id fields).
    #  <tt>:include_attributes</tt> - Which attributes of the current model should be exported in addition to the default. By default all non-internal attributes are exported (i.e. everything but _id fields).
    #  <tt>:attributes</tt> - Override which attributes to export.
    def exports_fdf(options = {})
      raise ArgumentError, "A :filename option must be specified." unless options[:filename]
      options[:include] = options[:include].is_a?(Array) ? options[:include] : [options[:include]].compact
      unless included_modules.include? FDFMethods
        class_inheritable_accessor :fdf_options
        extend ClassMethods
        include FDFMethods
      end
      self.fdf_options = options
    end
  end

  module XFDFMethods
    def to_xfdf(options = {})
      options.reverse_merge! self.class.xfdf_options
      fields = Util.collect_values(self, self.class.content_columns.map(&:name), options)
      filename = options[:filename]
      xml = Builder::XmlMarkup.new :indentation => options[:indentation]
      xml.instruct!
      xml.xfdf("xmlns" => "http://ns.adobe.com/xfdf/", "xml:space" => "preserve") {
        xml.f :href => filename
        xml.fields {
          fields.each do |field, value|
            xml.field(:name => field) {
              if value.is_a? Array
                  value.each {|item| xml.value(item.to_s) }
              else
                xml.value(value.to_s)
              end
            }
          end
        }
      }
      xml.target!
    end    
  end
  
  module FDFMethods
    def to_fdf(options={})
      options.reverse_merge! self.class.fdf_options
      fields = Util.collect_values(self, self.class.content_columns.map(&:name), options)
      filename = options[:filename]
      data = "%FDF-1.2\n%âã�?Ó\n1 0 obj\n<< \n/FDF << /Fields [ "
      fields.each do |field, value|
        if value.is_a? Array
          data << "<</T(#{field})/V["
          value.each {|v| data << "(#{v.strip})"}
          data << "]>>"
        else
          data << "<</T(#{field})/V(#{value.strip})>>"
        end
      end
    end
    data << "] \n/F (#{filename}) /ID [ <#{MD5.md5(Time.now).to_s}>\n ] >>" <<
            " \n>> \nendobj\ntrailer\n" << "<<\n/Root 1 0 R \n\n>>\n%%EOF\n"
  end
  
  module Util
    def self.collect_values(object, defaults, options = {})
      attrs = []
      if options[:attributes]
        attrs = stringify_all(options[:attributes]) rescue []
      else        
        [:include_attributes, :exclude_attributes].each do |opt|
          options[opt] = stringify_all(options[opt]) rescue []
        end
        attrs = stringify_all(defaults) + options[:include_attributes] - options[:exclude_attributes]
      end
      fields = attrs.inject({}) do |hash, key|
        value = object.send(key) rescue nil
        hash.merge key => value
      end
      fields.merge collect_association_values(object, options)
    end
    
    def self.collect_association_values(object, options = {})
      return {} if options[:include].blank?
      values = {}
      options[:include].each do |association|
        unless object.send(association).blank?
          models = object.send(association)
          unless models.is_a? Array
            columns = models.class.content_columns.map(&:name)
            values.merge! association_dump(association, models, columns)
          else
            models.each_with_index do |model, index|
              columns = model.class.content_columns.map(&:name)
              values.merge! association_dump("#{association.singularize}_#{index+1}", model, columns)
            end
          end
        end
      end
      values
    end
    
    def self.association_dump(prefix, object, attributes)
      attributes.inject({}) do |hash, attr|
        value = object.send(attr) rescue nil
        hash.merge "#{prefix}_#{attr}" => value
      end
    end
    
    def self.stringify_all(ary)
      ary.compact.map(&:to_s).uniq
    end
  end
end
ActiveRecord::Base.send :include, FDF


Also available from: http://pastie.caboo.se/38835

Add a watermark to a multi-page PDF (a-la Pragmatic Programmers)

This adds a text watermark to every page of a PDF document, as used by Pragmatic Programmers. Nothing secret here, more of a way to excercise "human" DRM. Used as:

   perl makepdf.pl "John Doe"


use PDF::Reuse;
use PDF::Reuse::Util;
use strict;

my ($name) = @ARGV;

prFile('book_for_someone.pdf');

my $sourcePdf = 'pdf_that_your_designer_made.pdf';
my $greeting = "This book is personalized for " . $name;

my $left = 1;
while ($left) {
   prFont('HO');
   prAdd("0 0 0 rg\n0 g\nf\n");
   prText( 38, 800, $greeting);
   $left = prSinglePage($sourcePdf);   
} 

prEnd;

Creating a PDF using iText and ColdFusion

// description of your code here

<cfscript>
// create document
document 		= CreateObject("java", "com.lowagie.text.Document");
document.init();

// writer
fileIO 			= CreateObject("java", "java.io.FileOutputStream");
fileIO.init(pdf_path);
writer 			= CreateObject("java", "com.lowagie.text.pdf.PdfWriter");
writer.getInstance(document, fileIO);
document.open();

// newsinfo header image
Image 			= CreateObject("java", "com.lowagie.text.Image");
jpg 			= Image.getInstance(header_image);
jpg.setAbsolutePosition(28, 713);
jpg.setDpi(300,300);
document.add(jpg);

// top margin; dumb i know but i was in a hurry
paragraph = CreateObject("java", "com.lowagie.text.Paragraph");
paragraph.init(" ");
for (i=0; i lt 9; i=i+1) {
	document.add(paragraph);
}

// the fonts
FontFactory 	= createobject("java", "com.lowagie.text.FontFactory");
Font 			= createObject("java", "com.lowagie.text.Font");
TimesLargeBI 	= Font.init(Font.TIMES_ROMAN, 14.0, Font.BOLDITALIC);
TimesNormal 	= Font.init(Font.TIMES_ROMAN, 12.0);

// all the text
paragraph 		= CreateObject("java", "com.lowagie.text.Paragraph");

paragraph.init("Hello World!", TimesLargeBI);
paragraph.setIndentationLeft(indentation_left);
paragraph.setIndentationRight(indentation_right);
document.add(paragraph);

paragraph.init("#dateFormat(now(), 'long')#", TimesNormal);
paragraph.setIndentationLeft(indentation_left);
paragraph.setIndentationRight(indentation_right);
document.add(paragraph);

document.close();
</cfscript> 

Finding pdf paper from CiteULike

I use CiteULike (http://citeulike.org) to manage papers a lot.
Unfortunately, I don't have access to paid site. But some authors
are generous enough to put pdf on his own site, which I find
using google search.
javascript:(function(){
var title = document.title.substring(11);
var query = '%22' + title + '%22 ' + 'filetype:pdf';
var url = 'http://www.google.com/search?q=' + encodeURI(query);
window.open(url);
})();


It simply takes the paper title from windows title. Then it searches the title(enclosed in quotation) for pdf files of the same name.
« Newer Snippets
Older Snippets »
Showing 1-8 of 8 total  RSS