Spaces:
Sleeping
Sleeping
File size: 10,086 Bytes
ffc414e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document Converter</title>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@tailwindcss/typography/dist/typography.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Inter', sans-serif;
}
.drop-zone {
border: 2px dashed #cbd5e0;
transition: all 0.3s ease;
}
.drop-zone:hover, .drop-zone.dragging {
border-color: #4a5568;
background-color: #f7fafc;
}
.loading-spinner {
border: 3px solid #f3f3f3;
border-top: 3px solid #3498db;
border-radius: 50%;
width: 24px;
height: 24px;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body class="bg-gray-50">
<div id="app" class="min-h-screen py-12 px-4 sm:px-6 lg:px-8">
<div class="max-w-3xl mx-auto">
<!-- Header -->
<div class="text-center mb-12">
<h1 class="text-3xl font-bold text-gray-900 mb-2">Document Converter</h1>
<p class="text-gray-600">Convert your documents to various formats quickly and easily</p>
</div>
<!-- Main Content -->
<div class="bg-white rounded-lg shadow p-6">
<!-- File Upload Zone -->
<div
class="drop-zone rounded-lg p-8 text-center cursor-pointer mb-6"
:class="{ 'dragging': isDragging }"
@dragenter.prevent="isDragging = true"
@dragleave.prevent="isDragging = false"
@dragover.prevent
@drop.prevent="handleDrop"
@click="triggerFileInput">
<input
type="file"
ref="fileInput"
@change="handleFileSelect"
class="hidden">
<div v-if="!selectedFile">
<svg class="mx-auto h-12 w-12 text-gray-400" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"/>
</svg>
<p class="mt-2 text-gray-600">Drag and drop your file here or click to browse</p>
</div>
<div v-else class="text-gray-700">
<p class="font-medium">Selected file:</p>
<p class="text-sm">{{ selectedFile.name }}</p>
</div>
</div>
<!-- Conversion Options -->
<div class="mb-6">
<label class="block text-sm font-medium text-gray-700 mb-2">Output Format</label>
<select
v-model="outputFormat"
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500">
<option value="pdf">PDF</option>
<option value="docx">DOCX</option>
<option value="xlsx">XLSX</option>
<option value="png">PNG</option>
<option value="jpg">JPG</option>
<option value="csv">CSV</option>
</select>
</div>
<!-- Filter Options -->
<div class="mb-6" v-if="showFilterOptions">
<label class="block text-sm font-medium text-gray-700 mb-2">Filter Options</label>
<input
type="text"
v-model="filterOptions"
placeholder="e.g., PixelWidth=640,PixelHeight=480"
class="w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500">
<p class="mt-1 text-sm text-gray-500">{{ filterOptionsHint }}</p>
</div>
<!-- Convert Button -->
<button
@click="convertFile"
:disabled="!selectedFile || converting"
class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50 disabled:cursor-not-allowed">
<span v-if="!converting">Convert Document</span>
<div v-else class="loading-spinner"></div>
</button>
<!-- Status Messages -->
<div v-if="status" :class="statusClass" class="mt-4 p-4 rounded-md">
{{ status }}
</div>
</div>
</div>
</div>
<script>
const { createApp } = Vue
createApp({
data() {
return {
selectedFile: null,
outputFormat: 'pdf',
filterOptions: '',
isDragging: false,
converting: false,
status: '',
statusType: 'info'
}
},
computed: {
showFilterOptions() {
return ['png', 'jpg', 'csv'].includes(this.outputFormat)
},
filterOptionsHint() {
if (this.outputFormat === 'png' || this.outputFormat === 'jpg') {
return 'Example: PixelWidth=640,PixelHeight=480'
} else if (this.outputFormat === 'csv') {
return 'Example: 59,34,76,1 for CSV specific options'
}
return ''
},
statusClass() {
const baseClasses = 'text-sm p-4 rounded-md'
switch (this.statusType) {
case 'success':
return `${baseClasses} bg-green-50 text-green-700`
case 'error':
return `${baseClasses} bg-red-50 text-red-700`
default:
return `${baseClasses} bg-blue-50 text-blue-700`
}
}
},
methods: {
triggerFileInput() {
this.$refs.fileInput.click()
},
handleFileSelect(event) {
const file = event.target.files[0]
if (file) {
this.selectedFile = file
this.status = ''
}
},
handleDrop(event) {
this.isDragging = false
const file = event.dataTransfer.files[0]
if (file) {
this.selectedFile = file
this.status = ''
}
},
async convertFile() {
if (!this.selectedFile) return
this.converting = true
this.status = 'Converting document...'
this.statusType = 'info'
const formData = new FormData()
formData.append('file', this.selectedFile)
try {
const queryParams = new URLSearchParams({
output_format: this.outputFormat
})
if (this.filterOptions) {
queryParams.append('filter_options', this.filterOptions)
}
const response = await fetch(`/convert/?${queryParams}`, {
method: 'POST',
body: formData
})
if (!response.ok) {
throw new Error(`Conversion failed: ${response.statusText}`)
}
// Handle successful conversion
const blob = await response.blob()
const url = window.URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `converted.${this.outputFormat}`
document.body.appendChild(a)
a.click()
window.URL.revokeObjectURL(url)
document.body.removeChild(a)
this.status = 'Document converted successfully!'
this.statusType = 'success'
} catch (error) {
this.status = `Error: ${error.message}`
this.statusType = 'error'
} finally {
this.converting = false
}
}
}
}).mount('#app')
</script>
</body>
</html> |