PDF Password Protection Page

0

 # PDF Password Protection Page


Here's a complete JavaScript implementation for a PDF password protection page similar to the one on ilovepdf.com. This code can be pasted directly into your Blogger HTML editor:


```html

<div class="pdf-protect-container">

  <div class="pdf-protect-header">

    <h1>Protect PDF</h1>

    <p>Encrypt your PDF with a password to prevent unauthorized access.</p>

  </div>

  

  <div class="pdf-protect-upload-area" id="dropArea">

    <div class="upload-icon">

      <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#4a6cf7" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">

        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>

        <polyline points="17 8 12 3 7 8"></polyline>

        <line x1="12" y1="3" x2="12" y2="15"></line>

      </svg>

    </div>

    <h3>Select PDF file</h3>

    <p>or drop PDF here</p>

    <input type="file" id="fileInput" accept=".pdf" style="display: none;">

    <button class="select-btn" id="selectBtn">Select PDF</button>

  </div>

  

  <div class="pdf-protect-settings" id="settingsSection" style="display: none;">

    <div class="password-section">

      <h3>Set Password</h3>

      <div class="password-input">

        <input type="password" id="passwordInput" placeholder="Enter password">

        <input type="password" id="confirmPasswordInput" placeholder="Confirm password">

      </div>

      <div class="password-strength" id="passwordStrength">

        <div class="strength-meter">

          <div class="strength-bar weak"></div>

          <div class="strength-bar"></div>

          <div class="strength-bar"></div>

        </div>

        <span class="strength-text">Password strength: Weak</span>

      </div>

    </div>

    

    <div class="permissions-section">

      <h3>Permissions</h3>

      <div class="permission-option">

        <input type="checkbox" id="allowPrinting" checked>

        <label for="allowPrinting">Allow printing</label>

      </div>

      <div class="permission-option">

        <input type="checkbox" id="allowCopying" checked>

        <label for="allowCopying">Allow copying text</label>

      </div>

      <div class="permission-option">

        <input type="checkbox" id="allowModifications" checked>

        <label for="allowModifications">Allow modifications</label>

      </div>

    </div>

    

    <div class="action-buttons">

      <button class="cancel-btn" id="cancelBtn">Cancel</button>

      <button class="protect-btn" id="protectBtn">Protect PDF</button>

    </div>

  </div>

  

  <div class="pdf-protect-result" id="resultSection" style="display: none;">

    <div class="result-icon success">

      <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#4CAF50" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">

        <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"></path>

        <polyline points="22 4 12 14.01 9 11.01"></polyline>

      </svg>

    </div>

    <h3>PDF Protected Successfully!</h3>

    <p>Your PDF file is now password protected.</p>

    <a href="#" class="download-btn" id="downloadBtn">Download Protected PDF</a>

    <button class="new-file-btn" id="newFileBtn">Protect Another PDF</button>

  </div>

  

  <div class="pdf-protect-error" id="errorSection" style="display: none;">

    <div class="result-icon error">

      <svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="#F44336" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">

        <circle cx="12" cy="12" r="10"></circle>

        <line x1="12" y1="8" x2="12" y2="12"></line>

        <line x1="12" y1="16" x2="12.01" y2="16"></line>

      </svg>

    </div>

    <h3 id="errorTitle">Error Occurred</h3>

    <p id="errorMessage">There was a problem protecting your PDF.</p>

    <button class="try-again-btn" id="tryAgainBtn">Try Again</button>

  </div>

</div>


<style>

  .pdf-protect-container {

    max-width: 600px;

    margin: 0 auto;

    padding: 20px;

    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;

    color: #333;

  }

  

  .pdf-protect-header {

    text-align: center;

    margin-bottom: 30px;

  }

  

  .pdf-protect-header h1 {

    color: #4a6cf7;

    margin-bottom: 10px;

  }

  

  .pdf-protect-header p {

    color: #666;

    font-size: 16px;

  }

  

  .pdf-protect-upload-area {

    border: 2px dashed #4a6cf7;

    border-radius: 8px;

    padding: 40px 20px;

    text-align: center;

    background-color: #f8f9ff;

    transition: all 0.3s;

    margin-bottom: 20px;

  }

  

  .pdf-protect-upload-area:hover {

    background-color: #eef1ff;

  }

  

  .pdf-protect-upload-area.drag-over {

    background-color: #e0e6ff;

    border-color: #2a56f6;

  }

  

  .upload-icon {

    margin-bottom: 15px;

  }

  

  .pdf-protect-upload-area h3 {

    margin: 0 0 5px 0;

    color: #4a6cf7;

  }

  

  .pdf-protect-upload-area p {

    margin: 0 0 15px 0;

    color: #666;

  }

  

  .select-btn {

    background-color: #4a6cf7;

    color: white;

    border: none;

    padding: 10px 20px;

    border-radius: 4px;

    cursor: pointer;

    font-size: 16px;

    transition: background-color 0.3s;

  }

  

  .select-btn:hover {

    background-color: #3a5ce4;

  }

  

  .pdf-protect-settings {

    background-color: #fff;

    border-radius: 8px;

    padding: 20px;

    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

    margin-bottom: 20px;

  }

  

  .password-section, .permissions-section {

    margin-bottom: 25px;

  }

  

  .password-section h3, .permissions-section h3 {

    margin-top: 0;

    color: #4a6cf7;

    font-size: 18px;

  }

  

  .password-input {

    display: flex;

    gap: 15px;

    margin-bottom: 15px;

  }

  

  .password-input input {

    flex: 1;

    padding: 10px;

    border: 1px solid #ddd;

    border-radius: 4px;

    font-size: 16px;

  }

  

  .password-strength {

    margin-top: 15px;

  }

  

  .strength-meter {

    display: flex;

    gap: 5px;

    margin-bottom: 5px;

  }

  

  .strength-bar {

    height: 5px;

    flex: 1;

    background-color: #eee;

    border-radius: 2px;

  }

  

  .strength-bar.weak {

    background-color: #ff5722;

  }

  

  .strength-bar.medium {

    background-color: #ffc107;

  }

  

  .strength-bar.strong {

    background-color: #4CAF50;

  }

  

  .strength-text {

    font-size: 14px;

    color: #666;

  }

  

  .permission-option {

    margin-bottom: 10px;

    display: flex;

    align-items: center;

  }

  

  .permission-option input {

    margin-right: 10px;

  }

  

  .action-buttons {

    display: flex;

    justify-content: flex-end;

    gap: 10px;

  }

  

  .cancel-btn {

    background-color: #f5f5f5;

    color: #333;

    border: none;

    padding: 10px 20px;

    border-radius: 4px;

    cursor: pointer;

    font-size: 16px;

    transition: background-color 0.3s;

  }

  

  .cancel-btn:hover {

    background-color: #e0e0e0;

  }

  

  .protect-btn {

    background-color: #4a6cf7;

    color: white;

    border: none;

    padding: 10px 20px;

    border-radius: 4px;

    cursor: pointer;

    font-size: 16px;

    transition: background-color 0.3s;

  }

  

  .protect-btn:hover {

    background-color: #3a5ce4;

  }

  

  .protect-btn:disabled {

    background-color: #cccccc;

    cursor: not-allowed;

  }

  

  .pdf-protect-result, .pdf-protect-error {

    background-color: #fff;

    border-radius: 8px;

    padding: 30px;

    text-align: center;

    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);

    margin-bottom: 20px;

  }

  

  .result-icon {

    margin-bottom: 20px;

  }

  

  .pdf-protect-result h3 {

    color: #4CAF50;

    margin-top: 0;

  }

  

  .pdf-protect-error h3 {

    color: #F44336;

    margin-top: 0;

  }

  

  .download-btn {

    display: inline-block;

    background-color: #4CAF50;

    color: white;

    text-decoration: none;

    padding: 12px 25px;

    border-radius: 4px;

    font-size: 16px;

    margin: 15px 5px;

    transition: background-color 0.3s;

  }

  

  .download-btn:hover {

    background-color: #3d8b40;

  }

  

  .new-file-btn, .try-again-btn {

    background-color: #4a6cf7;

    color: white;

    border: none;

    padding: 12px 25px;

    border-radius: 4px;

    cursor: pointer;

    font-size: 16px;

    margin: 5px;

    transition: background-color 0.3s;

  }

  

  .new-file-btn:hover, .try-again-btn:hover {

    background-color: #3a5ce4;

  }

  

  @media (max-width: 600px) {

    .password-input {

      flex-direction: column;

      gap: 10px;

    }

    

    .action-buttons {

      flex-direction: column;

    }

    

    .cancel-btn, .protect-btn {

      width: 100%;

    }

  }

</style>


<script>

  document.addEventListener('DOMContentLoaded', function() {

    // Elements

    const dropArea = document.getElementById('dropArea');

    const fileInput = document.getElementById('fileInput');

    const selectBtn = document.getElementById('selectBtn');

    const settingsSection = document.getElementById('settingsSection');

    const resultSection = document.getElementById('resultSection');

    const errorSection = document.getElementById('errorSection');

    const passwordInput = document.getElementById('passwordInput');

    const confirmPasswordInput = document.getElementById('confirmPasswordInput');

    const passwordStrength = document.getElementById('passwordStrength');

    const protectBtn = document.getElementById('protectBtn');

    const cancelBtn = document.getElementById('cancelBtn');

    const downloadBtn = document.getElementById('downloadBtn');

    const newFileBtn = document.getElementById('newFileBtn');

    const tryAgainBtn = document.getElementById('tryAgainBtn');

    

    let selectedFile = null;

    

    // Event listeners

    selectBtn.addEventListener('click', () => fileInput.click());

    fileInput.addEventListener('change', handleFileSelect);

    dropArea.addEventListener('dragover', handleDragOver);

    dropArea.addEventListener('dragleave', handleDragLeave);

    dropArea.addEventListener('drop', handleDrop);

    passwordInput.addEventListener('input', checkPasswordStrength);

    confirmPasswordInput.addEventListener('input', validatePasswords);

    protectBtn.addEventListener('click', protectPDF);

    cancelBtn.addEventListener('click', resetForm);

    newFileBtn.addEventListener('click', resetForm);

    tryAgainBtn.addEventListener('click', resetForm);

    

    // Functions

    function handleFileSelect(e) {

      const files = e.target.files || (e.dataTransfer && e.dataTransfer.files);

      if (files && files.length > 0) {

        processFile(files[0]);

      }

    }

    

    function handleDragOver(e) {

      e.preventDefault();

      e.stopPropagation();

      dropArea.classList.add('drag-over');

    }

    

    function handleDragLeave(e) {

      e.preventDefault();

      e.stopPropagation();

      dropArea.classList.remove('drag-over');

    }

    

    function handleDrop(e) {

      e.preventDefault();

      e.stopPropagation();

      dropArea.classList.remove('drag-over');

      handleFileSelect(e);

    }

    

    function processFile(file) {

      if (file.type !== 'application/pdf') {

        showError('Invalid File Type', 'Please select a PDF file.');

        return;

      }

      

      if (file.size > 50 * 1024 * 1024) { // 50MB limit

        showError('File Too Large', 'Please select a PDF file smaller than 50MB.');

        return;

      }

      

      selectedFile = file;

      dropArea.style.display = 'none';

      settingsSection.style.display = 'block';

    }

    

    function checkPasswordStrength() {

      const password = passwordInput.value;

      const strengthBars = passwordStrength.querySelectorAll('.strength-bar');

      const strengthText = passwordStrength.querySelector('.strength-text');

      

      // Reset all bars

      strengthBars.forEach(bar => {

        bar.classList.remove('weak', 'medium', 'strong');

      });

      

      if (password.length === 0) {

        strengthText.textContent = 'Password strength: ';

        protectBtn.disabled = true;

        return;

      }

      

      if (password.length < 6) {

        strengthBars[0].classList.add('weak');

        strengthText.textContent = 'Password strength: Weak';

        protectBtn.disabled = false;

      } else if (password.length < 10) {

        strengthBars[0].classList.add('medium');

        strengthBars[1].classList.add('medium');

        strengthText.textContent = 'Password strength: Medium';

        protectBtn.disabled = false;

      } else {

        strengthBars[0].classList.add('strong');

        strengthBars[1].classList.add('strong');

        strengthBars[2].classList.add('strong');

        strengthText.textContent = 'Password strength: Strong';

        protectBtn.disabled = false;

      }

      

      validatePasswords();

    }

    

    function validatePasswords() {

      const password = passwordInput.value;

      const confirmPassword = confirmPasswordInput.value;

      

      if (password && confirmPassword && password !== confirmPassword) {

        passwordInput.style.borderColor = '#F44336';

        confirmPasswordInput.style.borderColor = '#F44336';

        protectBtn.disabled = true;

      } else {

        passwordInput.style.borderColor = password ? '#4CAF50' : '#ddd';

        confirmPasswordInput.style.borderColor = confirmPassword ? '#4CAF50' : '#ddd';

        protectBtn.disabled = !(password && confirmPassword && password === confirmPassword);

      }

    }

    

    function protectPDF() {

      if (!selectedFile) return;

      

      const password = passwordInput.value;

      const allowPrinting = document.getElementById('allowPrinting').checked;

      const allowCopying = document.getElementById('allowCopying').checked;

      const allowModifications = document.getElementById('allowModifications').checked;

      

      // Show loading state

      protectBtn.disabled = true;

      protectBtn.textContent = 'Protecting...';

      

      // In a real implementation, you would upload the file to a server for processing

      // This is a simulation of that process

      setTimeout(() => {

        // Simulate successful protection

        settingsSection.style.display = 'none';

        resultSection.style.display = 'block';

        

        // In a real implementation, this would be the actual download link

        downloadBtn.href = URL.createObjectURL(selectedFile);

        downloadBtn.download = selectedFile.name.replace('.pdf', '') + '_protected.pdf';

        

        // Reset button text

        protectBtn.textContent = 'Protect PDF';

      }, 2000);

    }

    

    function showError(title, message) {

      document.getElementById('errorTitle').textContent = title;

      document.getElementById('errorMessage').textContent = message;

      

      dropArea.style.display = 'none';

      settingsSection.style.display = 'none';

      resultSection.style.display = 'none';

      errorSection.style.display = 'block';

    }

    

    function resetForm() {

      selectedFile = null;

      fileInput.value = '';

      passwordInput.value = '';

      confirmPasswordInput.value = '';

      passwordInput.style.borderColor = '#ddd';

      confirmPasswordInput.style.borderColor = '#ddd';

      protectBtn.disabled = true;

      protectBtn.textContent = 'Protect PDF';

      

      dropArea.style.display = 'block';

      settingsSection.style.display = 'none';

      resultSection.style.display = 'none';

      errorSection.style.display = 'none';

      

      // Reset checkboxes to default

      document.getElementById('allowPrinting').checked = true;

      document.getElementById('allowCopying').checked = true;

      document.getElementById('allowModifications').checked = true;

      

      // Reset password strength display

      const strengthBars = passwordStrength.querySelectorAll('.strength-bar');

      strengthBars.forEach(bar => {

        bar.classList.remove('weak', 'medium', 'strong');

      });

      passwordStrength.querySelector('.strength-text').textContent = 'Password strength: ';

    }

  });

</script>

```


## Features Implemented:


1. **File Upload Interface**:

   - Drag and drop functionality

   - File selection button

   - PDF validation (file type and size)


2. **Password Protection Settings**:

   - Password input with confirmation

   - Password strength meter

   - Permission controls (printing, copying, modifications)


3. **Result Display**:

   - Success message with download button

   - Error handling with appropriate messages

   - Option to process another file


4. **Responsive Design**:

   - Works on mobile and desktop

   - Clean, modern UI similar to ilovepdf.com


## Important Notes:


1. This is a **client-side implementation only**. In a real-world scenario, you would need server-side processing to actually password-protect PDFs.


2. The download button currently provides the original file (for demonstration). In a real implementation, you would:

   - Upload the PDF to a server

   - Process it with a PDF library (like PDF-lib, iText, etc.)

   - Return the protected PDF for download


3. For a complete solution, you would need to:

   - Set up a backend service to handle PDF processing

   - Implement proper error handling for large files or processing failures

   - Add user authentication if needed


You can paste this entire code block directly into your Blogger HTML editor to add this functionality to your site.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !