@@ -28,7 +28,7 @@ import './lib/renderer/lightbox'
2828import { renderCSVPreview } from './lib/renderer/csvpreview'
2929
3030import { escapeAttrValue } from './render'
31- import { sanitizeUrl } from './utils'
31+ import { sanitizeUrl , isPdfUrl } from './utils'
3232
3333import markdownit from 'markdown-it'
3434import markdownitContainer from 'markdown-it-container'
@@ -634,11 +634,41 @@ export function finishView (view) {
634634 const cleanUrl = sanitizeUrl ( url )
635635 const inner = $ ( '<div></div>' )
636636 $ ( this ) . append ( inner )
637- setTimeout ( ( ) => {
638- PDFObject . embed ( cleanUrl , inner , {
639- height : '400px'
637+ 638+ // First check URL format
639+ const isPDFByExtension = / \. p d f ( \? .* ) ? $ / i. test ( cleanUrl ) || cleanUrl . includes ( 'pdf' )
640+ 641+ if ( isPDFByExtension ) {
642+ // Show loading message while we check content type
643+ const loadingMessage = $ ( '<div class="alert alert-info">Verifying PDF file...</div>' )
644+ inner . html ( loadingMessage )
645+ 646+ // Perform additional validation with HEAD request
647+ isPdfUrl ( cleanUrl ) . then ( isPDFByContentType => {
648+ if ( isPDFByContentType ) {
649+ // Valid PDF by content type, embed it
650+ PDFObject . embed ( cleanUrl , inner , {
651+ height : '400px'
652+ } )
653+ } else {
654+ // URL format looks like PDF but content type doesn't match
655+ inner . html ( '<div class="alert alert-warning">The URL looks like a PDF but the server didn\'t confirm it has a PDF content type.</div>' )
656+ console . warn ( 'URL has PDF extension but content type is not application/pdf:' , cleanUrl )
657+ 658+ // Try to embed anyway as a fallback
659+ setTimeout ( ( ) => {
660+ PDFObject . embed ( cleanUrl , inner , {
661+ height : '400px' ,
662+ fallbackLink : 'This doesn\'t appear to be a valid PDF. <a href="[url]">Click here to try downloading it directly</a>.'
663+ } )
664+ } , 1 )
665+ }
640666 } )
641- } , 1 )
667+ } else {
668+ // Not a valid PDF URL by extension
669+ inner . html ( '<div class="alert alert-danger">Invalid PDF URL. The URL must point to a PDF file.</div>' )
670+ console . warn ( 'Invalid PDF URL format:' , cleanUrl )
671+ }
642672 } )
643673 // syntax highlighting
644674 view . find ( 'code.raw' ) . removeClass ( 'raw' )
0 commit comments