Skip to main content
Glama

directory_to_pdf

Convert images from a directory into a single PDF document with optional title and file pattern matching.

Instructions

Convert all images in a directory to a PDF document. Args: image_dir: Directory containing images output_path: Path for the output PDF file title: Optional title for the PDF document pattern: File pattern to match (default: all files) Returns: JSON string with conversion results.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
image_dirYes
output_pathYes
titleNo
patternNo*

Implementation Reference

  • MCP tool handler for 'directory_to_pdf'. This is the primary entry point for the tool, decorated with @mcp.tool(). It resolves the PDFConverter instance and calls its directory_to_pdf method, then returns JSON results.
    @mcp.tool() async def directory_to_pdf( image_dir: str, output_path: str, title: Optional[str] = None, pattern: str = "*" ) -> str: """ Convert all images in a directory to a PDF document. Args: image_dir: Directory containing images output_path: Path for the output PDF file title: Optional title for the PDF document pattern: File pattern to match (default: all files) Returns: JSON string with conversion results. """ try: pc = get_pdf_converter() pdf_path = pc.directory_to_pdf( image_dir=image_dir, output_path=output_path, pattern=pattern, title=title ) pdf_info = pc.get_pdf_info(pdf_path) result = { "status": "success", "input_directory": image_dir, "pattern": pattern, "output_pdf": pdf_path, "pdf_info": pdf_info } return json.dumps(result, indent=2) except Exception as e: logger.error(f"Failed to convert directory to PDF: {e}") return json.dumps({ "status": "error", "error": str(e), "input_directory": image_dir, "output_pdf": output_path })
  • Core helper method in PDFConverter class that implements the directory-to-PDF conversion logic. It discovers image files matching the pattern in the directory, sorts them, and delegates to images_to_pdf for the actual conversion.
    def directory_to_pdf(self, image_dir: str, output_path: str, pattern: str = "*", title: Optional[str] = None) -> str: """ Convert all images in a directory to PDF. Args: image_dir: Directory containing images output_path: Output PDF file path pattern: File pattern to match (default: *) title: PDF document title (optional) Returns: Path to created PDF file """ try: image_paths = [] # Find all image files in directory logger.info(f"Searching for images in {image_dir} with pattern '{pattern}'") # Check if pattern already includes an extension if pattern.endswith(('.png', '.jpg', '.jpeg', '.bmp', '.tiff', '.PNG', '.JPG', '.JPEG', '.BMP', '.TIFF')): # Pattern already has extension, use as-is image_paths.extend(Path(image_dir).glob(pattern)) logger.info(f"Using pattern with existing extension: {pattern}") else: # Pattern doesn't have extension, append supported formats for ext in self.supported_formats: glob_pattern = f"{pattern}{ext}" found_files = list(Path(image_dir).glob(glob_pattern)) image_paths.extend(found_files) logger.debug(f"Pattern '{glob_pattern}' found {len(found_files)} files") # Also check uppercase extensions glob_pattern_upper = f"{pattern}{ext.upper()}" found_files_upper = list(Path(image_dir).glob(glob_pattern_upper)) image_paths.extend(found_files_upper) logger.debug(f"Pattern '{glob_pattern_upper}' found {len(found_files_upper)} files") image_paths = [str(p) for p in sorted(image_paths)] logger.info(f"Total image files found: {len(image_paths)}") if not image_paths: raise ValueError(f"No image files found in {image_dir}") logger.info(f"Found {len(image_paths)} images in {image_dir}") return self.images_to_pdf(image_paths, output_path, sort_files=True, title=title) except Exception as e: logger.error(f"Failed to convert directory {image_dir} to PDF: {e}") raise
  • server.py:365-365 (registration)
    The @mcp.tool() decorator registers the directory_to_pdf function as an MCP tool. Note: included separately for clarity, but part of the handler block.
    @mcp.tool()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PovedaAqui/auto-snap-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server