PDF Snake Scripting Tutorial

 

PDF Snake home

scripting reference

table of contents

Data Mailer

Fortunately, there is a lot of very useful Python code in the installed PDF Snake scripts that we can access very easily. In particular, in the Nup.py script, there is a function called FitSlot() which will put a page of any size into a rectangle of any size, and optionally preserve the aspect ratio. So, all we need to do is import Nup and call the FitSlot() function to put the picture of the house exactly where we want it. Here's the result, and here's the code:

import
acrobat
import
sys
import
os
.
path
from
Nup
import
FitSlot
# FORMAT:
# NAME, FEATURE1, FEATURE2, FEATURE3, FEATURE4,
# FEATURE5, FEATURE6, PICTUREFILE
data
=
(
(
'Jeffrey'
,
'3 Bedrooms'
,
'2 Bathrooms'
,
'A Swimming Pool'
,
''
,
''
,
''
,
'house1.pdf'
)
,
(
'Gary'
,
'5 Large Bedrooms'
,
'3 Bathrooms'
,
'3-Car Garage'
,
"Lot's of Shady Trees"
,
'Satelite Dish'
,
'Hot Tub'
,
'house2.pdf'
)
)
def
MakeSubsDictionary
(
data
)
:
dictSubs
=
{
'/N'
:
data
[
0
]
,
'/F1'
:
data
[
1
]
,
'/F2'
:
data
[
2
]
,
'/F3'
:
data
[
3
]
,
'/F4'
:
data
[
4
]
,
'/F5'
:
data
[
5
]
,
'/F6'
:
data
[
6
]
}
return
dictSubs
def
FindTutorialFile
(
sFileName
)
:
sThisScriptFilePath
=
sys
.
argv
[
0
]
sTutorialDirectory
=
os
.
path
.
split
(
sThisScriptFilePath
)
[
0
]
return
os
.
path
.
join
(
sTutorialDirectory
,
sFileName
)
def
AddHouseToPage
(
page
,
sHouseFile
)
:
doc
=
acrobat
.
documentOpen
(
sHouseFile
)
# this is the rect where we want to put the picture of the house
rectSlot
=
acrobat
.
rect
(
422
,
180
,
180
,
10
)
matrixSlot
=
FitSlot
(
page
.
inverseMatrix
*
rectSlot
,
doc
[
0
]
.
cropBox
*
doc
[
0
]
.
defaultMatrix
)
doc
[
0
]
.
contentsCopyToPage
(
page
,
matrixSlot
*
doc
[
0
]
.
defaultMatrix
)
doc
.
close
(
)
def
DataMailer
(
sTemplateFile
,
tupData
)
:
doc
=
acrobat
.
documentOpen
(
sTemplateFile
)
docNew
=
acrobat
.
documentNew
(
)
docNew
[
:
]
=
doc
[
0
:
1
]
*
len
(
tupData
)
for
i
in
range
(
len
(
tupData
)
)
:
dictSubs
=
MakeSubsDictionary
(
tupData
[
i
]
)
docNew
[
i
]
.
textrunReplace
(
dictSubs
)
AddHouseToPage
(
docNew
[
i
]
,
FindTutorialFile
(
tupData
[
i
]
[
7
]
)
)
return
docNew doc
=
DataMailer
(
FindTutorialFile
(
'template.pdf'
)
,
data
)
doc
.
forceVisible
(
)

Next Page >>