Pdftk.exe -- permissions issues when deleting generated PDFs

Posted on 2011-10-03
Last Modified: 2012-05-12
I am using the common PDF command line program, pdftk.exe, to generated custom-made PDFs with variable data. In time, there is a need to delete some of these, for which I am using the PHP unlink() function.

Every once in a while, one rogue PDF that has been generated from pdftk.exe (called from the same PHP program) coughs up some permissions issues, keeping it from being deleted. I am perplexed because it is only this one rogue one once in a while, and all the others delete just fine. I also don't think it is an issue with the specific BASE pdf used to generate the one with variable data, because I know I have successfully deleted some generated from that same base PDF.

Can you clue me in? What could be causing this just once in a while? There are very few clues, and like I said the base PDF doesn't appear to be a key to the cause.
Question by:jristov
    LVL 107

    Expert Comment

    by:Ray Paseur
    Permissions issues are usually driven by the interplay of a program, a file system and an operating system.  If you want to tell us a little more about those things we might be able to offer some help.  The situation you describe might be driven by a chmod() statement in a conditional control structure.
    LVL 44

    Accepted Solution

    Is it possible that the file that you want to remove is actually in use (e.g. open in Reader or any other application, or being served by the web server)?

    Author Comment

    khkremer: Yes, it appears you have hit the nail right on the head! Thank you! But, a related question that pops up is: How do I know if this file is currently open or being used somewhere? Is there a way in PHP to know this (perhaps through an "error handler" that returns the permission denied error)?
    LVL 44

    Expert Comment

    by:Karl Heinz Kremer
    It looks like you are using Windows (based on the .exe extension to pdftk), this is not a platform I am very familiar with.

    The unlink() function returns a boolean: TRUE on success and FALSE on failure. You should evaluate that return value to see if your unlink call was successful. I would hope that it reports a problem if the call fails due to the busy condition. Usually (again, this may not be valid for Windows), you can move a file that is open, so you could just move it out of the way, and then when in certain intervals process the "these files were in use at one point in time, but should be deleted" directory and try to delete the files in it.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Update 21-May-2015: I temporarily removed the source code and the code snippets to make major changes to the program. Regards, Joe INTRODUCTION This Article is a follow-up to the Article entitled How To Rename-Move a Batch of PDF Files Based o…
    *Adobe Acrobat 9 was used for this article.  Particular steps may vary depending on software versions. Adobe Acrobat has many, many variables that my be utilized to customize your forms for clarity and ease of use. The Form Editing Tool will be y…
    The viewer will learn how to dynamically set the form action using jQuery.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now