Example #1
0
def attach_files(ly, wait = 0):
    """
    Checks the ly file for includes, and attaches all files
    to the ly's PDF file.

    Optionally waits the number of seconds before copying the new
    PDF, so kpdf gets time to update, and will not read a truncated file.
    """
    import shutil, subprocess, tempfile

    directory, filename = os.path.split(ly)
    files = list(set(find_included_files(filename, directory)))
    pdf = os.path.splitext(ly)[0] + '.pdf'

    handle, temp = tempfile.mkstemp('.pdf')
    os.close(handle)

    cmd = [pdftk(), pdf, 'attach_files'] + files + ['output', temp]

    try:
        retcode = subprocess.call(cmd, cwd = directory)
    except OSError, e:
        log.fail(_("Could not start Pdftk: %s") % e)
        os.remove(temp)
Example #2
0
    cmd = [pdftk(), pdf, 'attach_files'] + files + ['output', temp]

    try:
        retcode = subprocess.call(cmd, cwd = directory)
    except OSError, e:
        log.fail(_("Could not start Pdftk: %s") % e)
        os.remove(temp)
    else:
        if retcode == 0:
            # copy temp to the pdf, but do it not too quick if wait
            # was specified, otherwise KPDF could still be reading, and
            # will then possibly read a truncated file.
            def finish():
                shutil.copy(temp, pdf)
                os.remove(temp)
                log.ok(_(
                    "Embedded file %s in PDF.",
                    "Embedded files %s in PDF.",
                    len(files)
                    ) % '[%s]' % ', '.join(files))
            QTimer.singleShot(int(wait * 1000), finish)
        else:
            os.remove(temp)
            log.fail('%s %s' % (
                _("Embedding files in PDF failed."),
                _("Return code: %i") % retcode))


# kate: indent-width 4;