Skip to main content
Participant
March 21, 2008
Question

How to set a PDF to expire (Working Script)

  • March 21, 2008
  • 35 replies
  • 89952 views
Here is a little script I made up the other night. You can use it to allow a PDF to be opened only until a set date. I use this for when my employees go to service a customer. I want them to be able to see the customer's information, but only for 24 to 48 hours.<br /><br />CheckExpiration()<br /><br />function CheckExpiration()<br />{<br />/*-----START EDIT-----*/<br />var LastDay = 21<br />var LastMonth = 3<br />var LastYear = 2008<br />/*-----END EDIT-------*/<br /><br />/* DO NOT EDIT PAST HERE !!! */<br />var today = new Date();<br />var myDate=new Date();<br />LastMonth = LastMonth - 1<br />myDate.setFullYear(LastYear,LastMonth,LastDay);<br /><br />if (myDate<today)<br /> {<br /> this.closeDoc(1);<br /> app.alert("This files has expired.",1,0,"Expired");<br /> }<br />}
    This topic has been closed for replies.

    35 replies

    Participant
    September 18, 2008
    Hi,

    I have posted the same problem in forum at:

    M. Ahmad, "How to close a PDF doc opended in IE web browser using JavaScript?" #, 16 Sep 2008 6:44 am

    The example of my script works in pdf but not in browser, as most of you are having the same problem.

    I will just say:
    1) SetPageAction is not the right place to put your javascript for this purpose. As the script will not run untill file is not opened to that page or user does not go to that page.
    2) I put this script as Document Level Script and that way it will run as soon as the file is opened.
    3) Yes, the script can be added to one file or a group of files through BATCH PROCESSING. To do this you have to write another script to add this expiry script to the file/files automatically thorugh batch processing

    4) I agree, it is not the true security but it is better than having nothing at all.

    Thanks.
    M.Ahmad
    August 2, 2008
    try Adobe Document Center. you will need Acrobat 9 for a Mac (as I understand it - I have a client that told me that yesterday...)
    Participant
    August 2, 2008
    Can you recommend any? I am a mac user. I don't have a big budget I am a freelancer?
    Bernd Alheit
    Community Expert
    Community Expert
    August 2, 2008
    Use digital rights management (DRM) software.
    Participant
    August 2, 2008
    Thank you for your time Steve.

    Sorry I should have been a little more clearer, I am actually a designer (not a programmer)
    so the above is a little overwhelming, are you able to set it out step by step?

    Basically what I am trying to do is to set an expiration date on pdf files that I supply to my clients, so that once the file has expired it also can not be opened and edited via Photoshop/Illustrator should the pdf file land in the hands of another designer.

    Is this possible?
    August 2, 2008
    Here's a slightly different method, but it works..... (and, there is certainly no 'work around' for anyone to get data you don't want them to get!) <br /> <br />It uses the Action on Open (sorry, I don't know the 'official' name of this - I'll describe it below..) in the pdf to send the user to a server checking mechanism (in this case, a date checker - but you could do the same thing with lots of stuff...) <br /> <br />Here's how I set it up and it works great! <br /> <br />1. Start a new document that you can get a form field into (don't use LiveDesign for this - that's too fancy... - just make a document in Word that says ______ then print to pdf to get a nice simple pdf that you can edit directly in Acrobat) <br /> <br />2. Do a 'Run Form Field Recognition' and Acrobat will find the blank as a field. <br /> <br />3. Edit this field to have a "key" name (my example, I will use "today" which tells me this is the day that my document starts). <br /> <br />4. Under the 'Options' tab, make a Default Value of today's date (this will be the 'test against' date - for my example I'm showing how to expire a document in 30 days from creation date, but you could use anything you like - it is simple to modify!) <br /> <br />5. From the 'Format' tab, use the Select format category of Date. Use a date format you like (my example uses mm/dd/yyyy) Note that this MUST match the server side code described later). <br /> <br />6. For a 'live' document, you may want to go to the General tab and make the field Hidden as well as other things to not see the 'false' page, but that is outside the scope of this example. <br /> <br />OK, now on to setting up the document to automatically send this info to your server for testing... <br /> <br />1. Go to the 'Pages' view (click on the icon at the top left) <br />2. right-click on the page, select Page Properties. <br />3. Under the 'Actions' tab, select "Page Open" and "Submit a form" from the drop down boxes. <br />4. Enter a URL to your web page (see below) that does the checking (like http://mytestdomain.com/Scripts/checkexpiredate.php so it is easy to remember) <br />5. Check the HTML radio button. <br />6. Check the 'Only these...' button and then 'Select fields'. Make sure your "key" field is checked as well as the 'Include Selected' button (sometimes, for me, these weren't - I don't know why, so check it!) <br /> <br />Now, on to the server side... <br /> <br />Here's the code that goes into the php server file; <br /> <br /><?php<br />$day = date("d");<br />$month = date("m");<br />$year = date("Y");<br />$today = substr($_REQUEST["today"],3,2);<br />$tomonth = substr($_REQUEST["today"],0,2);<br />$toyear = substr($_REQUEST["today"],6,4);<br /> $tsp = mktime(0, 0, 0, $month, $day, $year);<br /> $tsd = mktime(0, 0, 0, $tomonth, $today, $toyear);<br /> $xdays = ($tsp - $tsd)/(24*60*60); // as calculated from dates<br />$maxdays = 30; //set this to whatever your 'expire from today' date is<br />if ($xdays >= $maxdays)<br /> //do what you like to tell the user it is expired<br />else<br /> //show the info you want them to see<br />?> <br /> <br />i (the info shown (or not) is outside the scope of this message, this is just a 'one way to make this work' example. In my system, I use a program called fpdf [http://fpdf.org] to create the pdf from 'scratch', building it to show the reader what I want them to see - whether it is a "Sorry, this is expired" document, or the data that they came for if the time hasn't expired.) <br /> <br />THAT'S IT!!! It works great and you can use it to do tons of stuff - just set the "key" on the original document and make the server code check whatever it is you want! <br /> <br />Pretty cool, I think! (and, it only took me, a novice programmer, about 3 hours to figure it all out!) <br /> <br />Best of success with it - enjoy!
    Participant
    August 2, 2008
    Yes the script does work to expire the pdf, however you can still open the pdf file through Photoshop or Illustrator even though it has expired. Is there any other solution?
    Inspiring
    June 16, 2008
    You need to add the variables for hours, minutes, seconds and milliseconds to the code. A generalized version that will work with omitted time elements follows.

    function CheckExpiration(LastYear, LastMonth, LastDate, LastHour, LastMin, LastSec, LastMS) {
    // document level function to see if passed date less than today's date
    // check that numbers are passed as parameters
    if (isNaN(LastYear) ) LastYear = 1900;
    if (isNaN(LastMonth) ) LastMonth = 1;
    if (isNaN(LastDate) ) LastDate = 1;
    if (isNaN(LastHour) ) LastHour = 0;
    if (isNaN(LastMin) ) LastMin = 0;
    if (isNaN(LastSec) ) LastSec= 0;
    if (isNaN(LastMS) ) LastMS = 0;

    LastMonth = LastMonth - 1; // adjust the passed month to the zero based month
    // make the expiration date time object a numeric value
    var myDate = new Date( Number(LastYear), Number(LastMonth), Number(LastDate), Number(LastHour), Number(LastMin), Number(LastSec), Number(LastMS) ).valueOf(); // convert passed expiration date time to a date time object value

    // get the current date time's object as a numeric value
    var today = new Date().valueOf();

    // return logical value of the comparison of the passed expiration date value to today - if true document has expired
    return (myDate < today);
    }

    // the following code has to be executed after the above function is defined

    // edit following fields
    var ExpireYear = 2008; // 2008
    var ExpireMonth = 3; // March
    var ExpireDate = 21; // 21st
    var ExpireHour = 12; // noon
    var ExpireMin = 0;

    // the following code has to executed after the above function and variables are defined.

    // test for expired document based on result of the passed time elements compared to the current date and time as returned by the CheckExpiration() function.
    if (CheckExpiration(ExireYear, ExpireMonth, ExoireDate, ExpireHour, ExpireMin) ) {
    this.closeDoc(1);
    app.alert("This files has expired.",1,0,"Expired");
    }
    Participant
    June 16, 2008
    Do you know how to add a specific time to the expiry?
    Patrick_Leckey
    Participating Frequently
    May 20, 2008
    They may be "worthless" solutions for non-internet-connect scenarios, but that doesn't make the implementation any more secure. It's still a laughably insecure approach to expiring a document. Unless you are in complete control of the viewing environment, I know for a fact that a lot of corporate deployments of Acrobat default to JavaScript turned off - which means to them, your document never expires. A lot of home users turn JavaScript off too because they don't "trust" JavaScript. Not to mention all the people that use non-Adobe viewers (Foxit Reader for example) that may not handle the JavaScript correctly and cause unknown results.

    There is no perfect solution for expiring a PDF in a non-internet-connected scenario. If you can't control the timechecking in a known-safe server environment and have to rely on information from the local system, your security it lost since anybody can do anything to the local system. If you're looking for something showy that will make people feel warm and think that you have some form of security on your documents, use the above script. But I would be weary about passing it off, especially in a professional environment, as "secure". Anybody who wants to spend 5 minutes on Google looking at PDF security will realize your "security" is a complete sham and that could reflect badly on you.

    The best option to secure a PDF in a non-connected environment is to apply document encryption and only give the password to those who need to view the document.

    Oh and LiveCycle Rights Management ES has plenty of fallback configuration options for how to handle non-connected environments with policy-protected PDFs.