PDF-Reuse-Tutorial
view release on metacpan or search on metacpan
Tutorial.pm view on Meta::CPAN
Dim pgm
Dim ans
Set pgm = CreateObject("PDFReuse.Scriptlet")
ans = pgm.act("prFile~fromVB.pdf")
ans = pgm.act("prForm~lastYear.pdf")
ans = pgm.act("prText~107~685~Mr Vladimir Bosak")
ans = pgm.act("prEnd")
=for end
Run it from the command line with >cscript Test.vbs or double-click on it from
the explorer. Here is the same example in JScript
=for Test.js begin
// Test.js
// Use PDF::Reuse from JScript
//
var pgm = new ActiveXObject("PDFReuse.Scriptlet")
var ans = pgm.act("prFile~fromJS.pdf")
ans = pgm.act("prForm~lastYear.pdf")
ans = pgm.act("prText~107~685~Mr Java Script")
ans = pgm.act("prEnd")
=for end
Here is an example of how to use it from the Internet Explorer. Probably
it is not a very practical example ('Test.html):
=for Test.html begin
<HEAD><TITLE>A Simple First Page</TITLE>
<SCRIPT LANGUAGE="JScript">
<!--
function Button1_OnClick()
{ var pgm = PDFREUSE;
var ans = pgm.act("prFile~C:/Temp/temp/fromHTML.pdf");
ans = pgm.act("prForm~C:/Temp/temp/lastYear.pdf~1~1");
ans = pgm.act("prText~107~685~Mr JavaScript HTML");
ans = pgm.act("prEnd");
}
-->
</SCRIPT>
</HEAD>
<BODY>
<OBJECT ID=PDFREUSE WIDTH=1 HEIGHT=1
CLASSID='CLSID:6ede1a17-839b-4220-83e3-ac2ad44a45f5'>
</OBJECT>
<H3>A Simple First Page</H3><HR>
<FORM><INPUT NAME="Button1" TYPE="BUTTON" VALUE="Click Here"
onClick=Button1_OnClick()>
</FORM>
</BODY>
</HTML>
=for end
Change the directories (and CLASSID) so it can be run on your machine
If something goes wrong and it is within the control of PDF::Reuse, you
get an error log on the desktop.
The first time you run via an ActiveX object, it is fairly slow, but if you
do it repeatedly, the performance is quit acceptable.
=head2 Generate OO-code
You can generate graphic objects and subroutines from B<simple> PDF-files.
(If you want to modify the generated code you need to know a little about the graphic operators
of PDF. Look at "The PDF-reference Manual".)
In the distribution you should have a little program 'graphObj_pl', which I wrote
just for this tutorial. It is far from foolproof, and it could be much more advanced. Anyway
It could anyway be a starting point, and it has produced the graphic objects you will see
here.
We will write a program that produces weather symbols on a map, and we will use graphic objects,
generated from PDF-files.
The best thing, is probably to run the completed program first. You should have it
in the distribution with the name weatherObj_pl You also need the objects: cloud.pm,
sun.pm, lightn.pm, wind.pm, thermometer.pm, drop.pm, snow.pm. Also there should be
a map of eastern US: EUSA.pdf, and some weather data: weather.dat.
So run the program and look at the result. All the symbols can vary in size, color,
line width, rotation and skew. Of course they can inherit attributes from each other.
All the shapes are vector-based, so you can make them as big as you want and they will
keep there forms. Also you should notice the PDF-file will be fairly small. If you run
the program with weather.dat it will be smaller than 9 kB.
This is the way to make the symbols:
You start by drawing and painting with a program that produces postscript ( or EPS or
EPSF) or uncompressed PDF-files. I started with the free Pagedraw from http://www.mayura.com
and that's possible. You distill your files with the distiller from Adobe or use the "free"
GhostScript and Gsviewer. A disadvantage with these programs is that you get many
"external references", "name objects", and you have handle them in some way. (You can
do that by having the original PDF-files available. There your modules can find the
appropriate color spaces, fonts, graphic state dictionaries and so on.) But it is
simpler if you start with PDF-files that contain a minimum of "name objects", and
that's why I made most of the symbols here with Mayura Draw. It can export simple PDF-files
straight away.
When you have distilled or produced your PDF-file, you run the program graphObj_pl
like this:
perl graphObj_pl myGraphicObject.pdf
and you will (hopefully) get a module 'myGraphicObject.pm'. All the symbols are produced like that.
At the same time as you get the module, you also get a parameter file 'myGraphicObject.dat'.
If you look at the program 'graphObj_pl', you will see that it is not very sophisticated
It only takes the first stream it encounters, and tries to process it. If that is
not what you wanted, it will definitely fail. Then it splits the stream into words
and tries to make standardized lines with the operator at the end. (It
does not handle long sentences within parenthesis correctly.) It reads the
coordinates of every point to find the minimum values. It tries to analyze the stream
and make it into a module. You get an entry in the objects hash for every unique color, line width and so on.
"External objects" get function calls in the subroutine 'resources'.
When you use your modules in programs it is B<usually best to avoid external objects.>
If the module needs a font, you can e.g. explicitly give a standard one like 'H' or 'TR'
when you draw the object. That will make your code faster to execute and the PDF-file
( run in 4.968 seconds using v1.01-cache-2.11-cpan-56fb94df46f )