Skip to content

Commit 0994a07

Browse files
authored
Merge pull request open-webui#10415 from labanzu/better-pdf-preview
feat: better pdf preview
2 parents a818d8e + f4e67bf commit 0994a07

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

backend/open_webui/routers/files.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -225,17 +225,24 @@ async def get_file_content_by_id(id: str, user=Depends(get_verified_user)):
225225
filename = file.meta.get("name", file.filename)
226226
encoded_filename = quote(filename) # RFC5987 encoding
227227

228+
content_type = file.meta.get("content_type")
229+
filename = file.meta.get("name", file.filename)
230+
encoded_filename = quote(filename)
228231
headers = {}
229-
if file.meta.get("content_type") not in [
230-
"application/pdf",
231-
"text/plain",
232-
]:
233-
headers = {
234-
**headers,
235-
"Content-Disposition": f"attachment; filename*=UTF-8''{encoded_filename}",
236-
}
237232

238-
return FileResponse(file_path, headers=headers)
233+
if content_type == "application/pdf" or filename.lower().endswith(
234+
".pdf"
235+
):
236+
headers["Content-Disposition"] = (
237+
f"inline; filename*=UTF-8''{encoded_filename}"
238+
)
239+
content_type = "application/pdf"
240+
elif content_type != "text/plain":
241+
headers["Content-Disposition"] = (
242+
f"attachment; filename*=UTF-8''{encoded_filename}"
243+
)
244+
245+
return FileResponse(file_path, headers=headers, media_type=content_type)
239246

240247
else:
241248
raise HTTPException(

src/lib/components/common/FileItemModal.svelte

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
22
import { getContext, onMount } from 'svelte';
33
import { formatFileSize, getLineCount } from '$lib/utils';
4+
import { WEBUI_API_BASE_URL } from '$lib/constants';
45
56
const i18n = getContext('i18n');
67
@@ -12,14 +13,14 @@
1213
1314
export let item;
1415
export let show = false;
15-
1616
export let edit = false;
1717
1818
let enableFullContent = false;
19+
$: isPDF = item?.meta?.content_type === 'application/pdf' ||
20+
(item?.name && item?.name.toLowerCase().endsWith('.pdf'));
1921
2022
onMount(() => {
2123
console.log(item);
22-
2324
if (item?.context === 'full') {
2425
enableFullContent = true;
2526
}
@@ -33,9 +34,13 @@
3334
<div>
3435
<div class=" font-medium text-lg dark:text-gray-100">
3536
<a
36-
href={item.url ? (item.type === 'file' ? `${item.url}/content` : `${item.url}`) : '#'}
37-
target="_blank"
37+
href="#"
3838
class="hover:underline line-clamp-1"
39+
on:click|preventDefault={() => {
40+
if (!isPDF && item.url) {
41+
window.open(item.type === 'file' ? `${item.url}/content` : `${item.url}`, '_blank');
42+
}
43+
}}
3944
>
4045
{item?.name ?? 'File'}
4146
</a>
@@ -101,8 +106,18 @@
101106
</div>
102107
</div>
103108

104-
<div class="max-h-96 overflow-scroll scrollbar-hidden text-xs whitespace-pre-wrap">
105-
{item?.file?.data?.content ?? 'No content'}
109+
<div class="max-h-[75vh] overflow-auto">
110+
{#if isPDF}
111+
<iframe
112+
title={item?.name}
113+
src={`${WEBUI_API_BASE_URL}/files/${item.id}/content`}
114+
class="w-full h-[70vh] border-0 rounded-lg mt-4"
115+
/>
116+
{:else}
117+
<div class="max-h-96 overflow-scroll scrollbar-hidden text-xs whitespace-pre-wrap">
118+
{item?.file?.data?.content ?? 'No content'}
119+
</div>
120+
{/if}
106121
</div>
107122
</div>
108123
</Modal>

src/lib/components/common/Modal.svelte

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
77
export let show = true;
88
export let size = 'md';
9-
109
export let containerClassName = 'p-3';
1110
export let className = 'bg-gray-50 dark:bg-gray-900 rounded-2xl';
1211
@@ -74,9 +73,7 @@
7473
}}
7574
>
7675
<div
77-
class=" m-auto max-w-full {sizeToWidth(size)} {size !== 'full'
78-
? 'mx-2'
79-
: ''} shadow-3xl min-h-fit scrollbar-hidden {className}"
76+
class="m-auto max-w-full {sizeToWidth(size)} {size !== 'full' ? 'mx-2' : ''} shadow-3xl min-h-fit scrollbar-hidden {className}"
8077
in:flyAndScale
8178
on:mousedown={(e) => {
8279
e.stopPropagation();

0 commit comments

Comments
 (0)