Skip to main content
Glama

merge_pdfs

Combine multiple PDF files from local paths or URLs into a single document. Specify the order of files and save the merged PDF to your chosen location.

Instructions

Merge multiple PDF files into one.

Supports both local file paths and URLs. URLs will be downloaded to temporary
files before merging. Mixed local and URL paths are supported.

Args:
    pdf_paths: List of paths to PDF files to merge (in order) - can include URLs
    output_path: Path where the merged PDF will be saved (must be local path)
    
Returns:
    Success message with merge details or error message

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
pdf_pathsYes
output_pathYes

Implementation Reference

  • Implementation of the merge_pdfs MCP tool, which resolves input paths (including URLs), validates the output path, and merges PDFs using PyPDF2.
    async def merge_pdfs(pdf_paths: List[str], output_path: str) -> str:
        """Merge multiple PDF files into one.
        
        Supports both local file paths and URLs. URLs will be downloaded to temporary
        files before merging. Mixed local and URL paths are supported.
    
        Args:
            pdf_paths: List of paths to PDF files to merge (in order) - can include URLs
            output_path: Path where the merged PDF will be saved (must be local path)
            
        Returns:
            Success message with merge details or error message
        """
        # Resolve all input paths (download URLs if needed)
        actual_paths = []
        for pdf_path in pdf_paths:
            try:
                actual_path = resolve_path(pdf_path)
                
                # Validate local path if not URL
                if not is_url(pdf_path):
                    is_valid, error_msg = validate_path(pdf_path)
                    if not is_valid:
                        return error_msg
                
                actual_paths.append(actual_path)
                
            except Exception as e:
                return f"Error resolving path '{pdf_path}': {str(e)}"
        
        # Validate output path (must be local)
        if is_url(output_path):
            return "Error: Output path cannot be a URL, must be a local file path"
        
        is_valid, error_msg = validate_path(output_path)
        if not is_valid:
            return error_msg
        
        try:
            pdf_writer = PyPDF2.PdfWriter()
            total_pages_merged = 0
            
            for i, actual_path in enumerate(actual_paths):
                original_path = pdf_paths[i]
                try:
                    with open(actual_path, 'rb') as pdf_file:
                        pdf_reader = PyPDF2.PdfReader(pdf_file)
                        pages_count = len(pdf_reader.pages)
                        
                        for page in pdf_reader.pages:
                            pdf_writer.add_page(page)
                        
                        total_pages_merged += pages_count
                        logging.info(f"Added {pages_count} pages from {original_path}")
                
                except Exception as e:
                    return f"Error reading PDF '{original_path}': {str(e)}"
            
            # Write the merged PDF
            with open(output_path, 'wb') as output_file:
                pdf_writer.write(output_file)
            
            return f"Successfully merged {len(pdf_paths)} PDFs into '{output_path}'\nTotal pages: {total_pages_merged}"
            
        except Exception as e:
            return f"Error merging PDFs: {str(e)}"

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/lockon-n/pdf-tools-mcp'

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