Print PDF?

I have an applescript document application that stores a bunch of data in data sources. I would like to be able to print that data in a (PDF?) form that I have set up (like let’s say a tax form would be set up to be filled out). However, I am not finding any documentation on how to do this in AS. Can someone send me in the right direction?

Thanks,

John

The trick is that WebKit allows inline pdf’s in tags… here’s the rest:

make a web page that looks like so:

...

in your css file:

.form_space{position:absolute;}
#space1{top:x;left:y;width:w;height:h}

Create sample text and adjust the css until you are happy, then have a handler in your app that takes each cell value from your data source and wraps them in spans with successive id values, put the html header bit and footer bit around it, write it out to a file, and tell safari to open location, hit print and voila! (Note that the example code below is not optimized and assumes the existence of variables that you will make from the template work above… )

example handler:

on generateForm(entries)
set thePage to htmlHeader
set spaceIndex to 1
repeat with eachEntry in entries
set thePage to thePage & “<SPAN CLASS=“form_space” ID=“space” & spaceIndex & “”>” & eachEntry & “”
set spaceIndex to spaceIndex + 1
end repeat
set thePage to thePage & htmlFooter
set thePath to ((path to Desktop as string) & “:form.html”
set theFile to open for access file thePath with write permission
set eof of theFile to 0
write thePage to theFile starting at eof
close theFile
tell application “Safari” to open file thePath
end generateForm

Wow - I never considered using Safari as a tool for displaying a form and printing. Nice use of resources! I will have to study up on html to make this work . . .

It does, however, seem odd that one would have to go outside applescript studio to perform such a standard function. I am looking at the Quartz 2D capabilities in AS – I wonder if anybody has experience using Quartz 2D to create and print forms?

Thanks!

-John

for laying out on top of the form here’s the pieces of html you’ll need to know:

  1. CSS File

create a plain text file–say: ~/Documents/my_form.css

In this file type the following bits:


.form_space{position:absolute;}
#space1{top:y;left:x;width:w;height:h;}

you change the x,y,w,h to match the space you want on the form (eg. top:52px;left:450px;width:300px;height:24px;)… repeat this for each space of the form.

  1. html variables in applescript

property htmlHeader : "<HTML><HEAD><LINK REF=StyleSheet HREF="/Users/#username#/Documents/my_form.css"/></HEAD><BODY><IMG SRC="/Users/#username#/Documents/my_form.pdf"></IMG>"

property htmlFooter : "</BODY></HTML>"

A) Now you can use the handler in my previous post. It expects a list of text entries… (or you can embed html in those to layout images, java applets, and so on)

B) Using and generating html like this is what most of the internet is based on, it’s a stable technology, easy to work with, adaptable, cross platform… Don’t go into raw drawing API’s unless you need to generate images that you can’t prepare beforehand… and even then… trust me. (If you want it’s also possible to embed a webview in your own application and send it javascript statements to make it reload and refresh within your own app so you can fool people into thinking you did all the tough work of programming a drawing API)

Howdy…

So all you want to do is open a window, fill in a fancy form, and then print it… right? If so, there are much easier ways than what’s proposed above. It doesn’t require any html, css, or external apps. AND… when you go to print it asks you if you want to create a pdf out of it…if that’s your flavor. Saving to pdf will save the same contents it would otherwise print. Here’s what I did…

The basics…

  1. Create a window.
  2. Drag an NSBox to the window. Set the ‘box type’ to invisible in it’s attributes.

The image…
3) Create an image that looks like your form. It could be a jpg, gif, even an actual pdf file. Add it to your project with the “Add Files…” menu item in PB/xcode or by dragging it to the project.
4) Drag an image view into the box. Place your image in it. Make sure that the image is small enough to print out in one page, or you’ll have to find a way to get this to work with multiple pages (like multiple windows or form layouts).

The form…
5) Drag text fields into the box, aligning them to fit around your image, and making sure they are layered above the image. If you do the image first, and then the text fields this shouldn’t be an issue.

Essentially you are creating a regular form, with an image view containing your form’s “graphics” layed out underneath. The key to this, is that you do it all within a NSBox, for this reason…

  1. Create a button or other control that will signal the print action (or use the “Print…” File menu item).
  2. Connect the button’s ‘print:’ action TO THE BOX. Do this by holding control, clicking on the button, and dragging to the box. In the connections pane select print: and then click the “connect” button on the bottom to make the connection.

Now whenever you click the button (menu item… whatever) it will open the print dialog. Because it’s connected to the box and not the file’s owner or the window, it prints only the contents of the box. If you use the ‘print…’ menu item, make sure to disconnect the default print connection made to the file’s owner.

I tried it with an image and two text fields and got this… Click here. This is actually a screenshot of the pdf file I saved to, but it’s exactly the same as what I printed out.

Notes:
As you can see from my test image, if your image is an easy white background with black text over it, you might be able to use form-style text fields. If it’s a dark image or you want no borders, use regular text fields and make the color something contrasting… like the white that I used over the dark green (or black over white, obviously :)). I used a form-style text field on top, and a regular field on the bottom with the text color set to white. MAKE SURE to set the “Editable” attribute for any regular text fields you use to true in IB.

You might want to resize the box to snugly fit the image view, so there’s no possibility of the app trying to print anything from outside the bounds of the image area.

As for the objects themselves, you mentioned using a data source. Because the form is a regular applescript studio application, you can manipulate the text fields as usual…entering and editing data via your scripts. Just write your code that manages the fields and work with it as any other application. Make sure to add the (of box “theBox”) reference because it’s in the box, though. Note that the AS names are not even required (at least not in my test) unless you are going to use them to reference the fields for scripting purposes. If you’re just filling them in manually and printing without any script interaction, they can just be generic, nameless objects. Another nice thing, is that you can have other elements in the window, and as long as they’re not in the box, they won’t get printed.

Hope this helps…
j

Just FYI…

Quartz, apple’s primary imaging API essentially is pdf. In a roundabout way, everything done via basic applescript in terms of graphics and visual processing is done via the same protocols as pdf file creation.

So any time you wish to have access to pdf functions in your apps, it’s really not too far away. This is why saving to pdf is not a problem, and viewing pdfs is as easy as viewing any other supported image format…if not easier (on a core processing level, of course)

Cheers,
j