logo

pdf2img filter

← Back to Filter List

pdf2img


Runs ghostscript to convert PDF files to images. An image file can only hold a single page of PDF, so this defaults to returning page 1. The `page` setting can be used to specify other pages.

Aliases for this filter

  • pdf2img
  • pdftoimg
  • pdf2png

Converts from file formats:

  • .pdf

To file formats:

  • .png

Available settings:

SettingDescriptionDefault
add-new-filesBoolean or list of extensions/patterns to match.False
added-in-versionDexy version when this filter was first available.
additional-doc-filtersFilters to apply to additional documents created as side effects.{}
additional-doc-settingsSettings to apply to additional documents created as side effects.{}
argsArguments to be passed to the executable.
check-return-codeWhether to look for nonzero return code.True
clargsArguments to be passed to the executable (same as 'args').
command-stringThe full command string.%(prog)s -dSAFER -dNOPAUSE -dBATCH %(format)s -r%(res)s -sOutputFile="%%d-%(output_file)s" "%(script_file)s"
data-typeAlias of custom data class to use to store filter output.generic
envDictionary of key-value pairs to be added to environment for runs.{}
examplesTemplates which should be used as examples for this filter.[]
exclude-add-new-filesList of patterns to skip even if they match add-new-files.[]
exclude-new-files-from-dirList of directories to skip when adding new files.[]
executableThe executable to be rungs
extFile extension to output.None
ext-to-formatA dict of mappings from file extensions to format parameters that need to be passed on the command line, e.g. for ghostscript .png => png16m{'.jpg': 'jpeg', '.png': 'png16m'}
extension-mapDictionary mapping input extensions to default output extensions.None
format-specifierThe string used to specify the format switch, include trailing space if needed.-sDEVICE=
helpHelpstring for plugin.Runs ghostscript to convert PDF files to images. An image file can only hold a single page of PDF, so this defaults to returning page 1. The `page` setting can be used to specify other pages.
initial-timeout10
input-extensionsList of extensions which this filter can accept as input.['.pdf']
keep-originalsWhether, if additional-doc-filters are specified, the original unmodified docs should also be added.False
make-dummy-outputWhether to make a dummy output file when one is not generated and add-new-files is True.False
mkdirA directory which should be created in working dir.None
mkdirsA list of directories which should be created in working dir.[]
nodocWhether filter should be excluded from documentation.False
outputWhether to output results of this filter by default by reporters such as 'output' or 'website'.False
output-extensionsList of extensions which this filter can produce as output.['.png']
override-workspace-exclude-filtersIf True, document will be populated to other workspaces ignoring workspace-exclude-filters.False
pageWhich page of the PDF to return as an image1
path-extensionsstrings to extend path with[]
preserve-prior-data-classWhether output data class should be set to match the input data class.False
record-varsWhether to add code that will automatically record values of variables.False
require-outputShould dexy raise an exception if no output is produced by this filter?True
resResolution of image.300
scriptargsArguments to be passed to the executable.
tagsTags which describe the filter.['pdf', 'gs']
timeout10
use-wdWhether to use a custom working directory when running filter.True
variablesA dictionary of variable names and values to make available to this filter.{}
varsA dictionary of variable names and values to make available to this filter.{}
version-commandCommand to call to return version of installed software.gs --version
windows-version-commandCommand to call on windows to return version of installed software.None
workspace-exclude-filtersFilters whose output should be excluded from workspace.['pyg']
workspace-includesIf set to a list of filenames or extensions, only these will be populated to working dir.None
write-stderr-to-stdoutShould stderr be piped to stdout?True
Filter Source Code
class Pdf2ImgSubprocessFilter(SubprocessExtToFormatFilter):
    """
    Runs ghostscript to convert PDF files to images.

    An image file can only hold a single page of PDF, so this defaults to
    returning page 1. The `page` setting can be used to specify other pages.
    """
    aliases = ['pdf2img', 'pdftoimg', 'pdf2png']
    _settings = {
            'res' : ("Resolution of image.", 300),
            'page' : ("Which page of the PDF to return as an image", 1),
            'executable' : 'gs',
            'version-command' : 'gs --version',
            'tags' : ['pdf', 'gs'],
            'input-extensions' : ['.pdf'],
            'output-extensions' : ['.png'],
            'ext-to-format' : {
                '.png' : 'png16m',
                '.jpg' : 'jpeg'
                },
            'format-specifier' : '-sDEVICE=',
            'command-string' : '%(prog)s -dSAFER -dNOPAUSE -dBATCH %(format)s -r%(res)s -sOutputFile="%%d-%(output_file)s" "%(script_file)s"'
            }

    def process(self):
        self.populate_workspace()

        command = self.command_string()
        proc, stdout = self.run_command(command, self.setup_env())
        self.handle_subprocess_proc_return(command, proc.returncode, stdout)

        page = self.setting('page')
        page_file = "%s-%s" % (page, self.output_data.basename())

        wd = self.parent_work_dir()
        page_path = os.path.join(wd, page_file)
        shutil.copyfile(page_path, self.output_filepath())

Content © 2020 Dr. Ana Nelson | Site Design © Copyright 2011 Andre Gagnon | All Rights Reserved.