Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

Darkscribes Community

  1. Home
  2. Uncategorized
  3. Need solution that ensures PDFs can be previewed

Need solution that ensures PDFs can be previewed

Scheduled Pinned Locked Moved Uncategorized
3 Posts 3 Posters 0 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • migelammon@community.nodebb.orgM This user is from outside of this forum
    migelammon@community.nodebb.orgM This user is from outside of this forum
    [email protected]
    wrote last edited by
    #1

    I have written the following JavaScript code to allow students to preview PDF files uploaded to our forum. Our goal is to enable them to view lecture notes directly on the forum while preventing them from downloading the files.

    However, since the PDFs are embedded within an iframe, I haven't been able to completely block the "Save As" option. Even though previewing works fine, students can still download the PDF files, especially on mobile devices where the "Save to Files" option appears.

    I need a solution that ensures PDFs can be previewed but not downloaded on both desktop and mobile devices. How can I achieve this? Thank you.

    $(document).ready(function () {
        function processPDFs() {
            console.log("PDF önizleme işleniyor...");
     
            $('a[href$=".pdf"]').each(function () {
                let link = $(this).attr('href');
     
                if (!$(this).next('.pdf-container').length) {
                    if (isMobileDevice()) {
                        // 📱 **Mobil cihazlarda sadece PDF linkini göster, önizleme yok**
                        $(this).show();
                    } else {
                        // 💻 **Masaüstünde PDF'yi önizle**
                        $(this).hide(); // PDF linkini gizle
     
                        let container = $('<div class="pdf-container" style="width:100%;max-width:900px;height:700px;position:relative;margin-top:10px;border:1px solid #ccc;overflow:hidden"></div>');
                        let pdfObject = $('');
                        let fullscreenBtn = $('<button class="fullscreen-toggle" style="position:absolute;top:10px;right:10px;background:#000;color:#fff;border:none;padding:5px 10px;cursor:pointer;z-index:10">🔍 Tam Ekran</button>');
     
                        pdfObject.attr('data', link + "#toolbar=0");
     
                        fullscreenBtn.on('click', function () {
                            toggleFullscreen(container[0]);
                        });
     
                        container.append(pdfObject).append(fullscreenBtn);
                        $(this).after(container);
                    }
                }
            });
     
            $('.file a').each(function () {
                let fileLink = $(this).attr('href');
                if (fileLink.endsWith('.pdf') &amp;&amp; !$(this).next('.pdf-container').length) {
                    if (isMobileDevice()) {
                        // 📱 **Mobilde sadece PDF linki göster**
                        $(this).show();
                    } else {
                        // 💻 **Masaüstünde PDF'yi önizle**
                        $(this).hide();
     
                        let container = $('<div class="pdf-container" style="width:100%;max-width:900px;height:700px;position:relative;margin-top:10px;border:1px solid #ccc;overflow:hidden"></div>');
                        let pdfObject = $('');
                        let fullscreenBtn = $('<button class="fullscreen-toggle" style="position:absolute;top:10px;right:10px;background:#000;color:#fff;border:none;padding:5px 10px;cursor:pointer;z-index:10">🔍 Tam Ekran</button>');
     
                        pdfObject.attr('data', fileLink + "#toolbar=0");
     
                        fullscreenBtn.on('click', function () {
                            toggleFullscreen(container[0]);
                        });
     
                        container.append(pdfObject).append(fullscreenBtn);
                        $(this).after(container);
                    }
                }
            });
        }
     
        // 📌 **Mobil Cihaz Algılama**
        function isMobileDevice() {
            return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
        }
     
        // 📌 Tam Ekran Aç/Kapat Fonksiyonu
        function toggleFullscreen(element) {
            if (!document.fullscreenElement &amp;&amp; !document.mozFullScreenElement &amp;&amp; !document.webkitFullscreenElement &amp;&amp; !document.msFullscreenElement) {
                if (element.requestFullscreen) {
                    element.requestFullscreen();
                } else if (element.mozRequestFullScreen) {
                    element.mozRequestFullScreen();
                } else if (element.webkitRequestFullscreen) {
                    element.webkitRequestFullscreen();
                } else if (element.msRequestFullscreen) {
                    element.msRequestFullscreen();
                }
            } else {
                if (document.exitFullscreen) {
                    document.exitFullscreen();
                } else if (document.mozCancelFullScreen) {
                    document.mozCancelFullScreen();
                } else if (document.webkitExitFullscreen) {
                    document.webkitExitFullscreen();
                } else if (document.msExitFullscreen) {
                    document.msExitFullscreen();
                }
            }
        }
     
        // 📌 ESC ile Tam Ekrandan Çıkınca Butonu Güncelle
        $(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange msfullscreenchange", function () {
            $(".fullscreen-toggle").text(document.fullscreenElement ? "↩ Tam Ekrandan Çık" : "🔍 Tam Ekran");
        });
     
        // İlk yükleme
        processPDFs();
     
        // Sayfa değişimlerinde tekrar çalıştır (NodeBB SPA yapısına uygun)
        $(window).on('action:ajaxify.end', function () {
            processPDFs();
        });
     
        console.log("PDF izleme sistemi güncellendi: Mobilde sadece link gösteriliyor, masaüstünde önizleme aktif.");
    });
    
    eeeee@community.nodebb.orgE julian@community.nodebb.orgJ 2 Replies Last reply
    0
    • migelammon@community.nodebb.orgM [email protected]

      I have written the following JavaScript code to allow students to preview PDF files uploaded to our forum. Our goal is to enable them to view lecture notes directly on the forum while preventing them from downloading the files.

      However, since the PDFs are embedded within an iframe, I haven't been able to completely block the "Save As" option. Even though previewing works fine, students can still download the PDF files, especially on mobile devices where the "Save to Files" option appears.

      I need a solution that ensures PDFs can be previewed but not downloaded on both desktop and mobile devices. How can I achieve this? Thank you.

      $(document).ready(function () {
          function processPDFs() {
              console.log("PDF önizleme işleniyor...");
       
              $('a[href$=".pdf"]').each(function () {
                  let link = $(this).attr('href');
       
                  if (!$(this).next('.pdf-container').length) {
                      if (isMobileDevice()) {
                          // 📱 **Mobil cihazlarda sadece PDF linkini göster, önizleme yok**
                          $(this).show();
                      } else {
                          // 💻 **Masaüstünde PDF'yi önizle**
                          $(this).hide(); // PDF linkini gizle
       
                          let container = $('<div class="pdf-container" style="width:100%;max-width:900px;height:700px;position:relative;margin-top:10px;border:1px solid #ccc;overflow:hidden"></div>');
                          let pdfObject = $('');
                          let fullscreenBtn = $('<button class="fullscreen-toggle" style="position:absolute;top:10px;right:10px;background:#000;color:#fff;border:none;padding:5px 10px;cursor:pointer;z-index:10">🔍 Tam Ekran</button>');
       
                          pdfObject.attr('data', link + "#toolbar=0");
       
                          fullscreenBtn.on('click', function () {
                              toggleFullscreen(container[0]);
                          });
       
                          container.append(pdfObject).append(fullscreenBtn);
                          $(this).after(container);
                      }
                  }
              });
       
              $('.file a').each(function () {
                  let fileLink = $(this).attr('href');
                  if (fileLink.endsWith('.pdf') &amp;&amp; !$(this).next('.pdf-container').length) {
                      if (isMobileDevice()) {
                          // 📱 **Mobilde sadece PDF linki göster**
                          $(this).show();
                      } else {
                          // 💻 **Masaüstünde PDF'yi önizle**
                          $(this).hide();
       
                          let container = $('<div class="pdf-container" style="width:100%;max-width:900px;height:700px;position:relative;margin-top:10px;border:1px solid #ccc;overflow:hidden"></div>');
                          let pdfObject = $('');
                          let fullscreenBtn = $('<button class="fullscreen-toggle" style="position:absolute;top:10px;right:10px;background:#000;color:#fff;border:none;padding:5px 10px;cursor:pointer;z-index:10">🔍 Tam Ekran</button>');
       
                          pdfObject.attr('data', fileLink + "#toolbar=0");
       
                          fullscreenBtn.on('click', function () {
                              toggleFullscreen(container[0]);
                          });
       
                          container.append(pdfObject).append(fullscreenBtn);
                          $(this).after(container);
                      }
                  }
              });
          }
       
          // 📌 **Mobil Cihaz Algılama**
          function isMobileDevice() {
              return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
          }
       
          // 📌 Tam Ekran Aç/Kapat Fonksiyonu
          function toggleFullscreen(element) {
              if (!document.fullscreenElement &amp;&amp; !document.mozFullScreenElement &amp;&amp; !document.webkitFullscreenElement &amp;&amp; !document.msFullscreenElement) {
                  if (element.requestFullscreen) {
                      element.requestFullscreen();
                  } else if (element.mozRequestFullScreen) {
                      element.mozRequestFullScreen();
                  } else if (element.webkitRequestFullscreen) {
                      element.webkitRequestFullscreen();
                  } else if (element.msRequestFullscreen) {
                      element.msRequestFullscreen();
                  }
              } else {
                  if (document.exitFullscreen) {
                      document.exitFullscreen();
                  } else if (document.mozCancelFullScreen) {
                      document.mozCancelFullScreen();
                  } else if (document.webkitExitFullscreen) {
                      document.webkitExitFullscreen();
                  } else if (document.msExitFullscreen) {
                      document.msExitFullscreen();
                  }
              }
          }
       
          // 📌 ESC ile Tam Ekrandan Çıkınca Butonu Güncelle
          $(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange msfullscreenchange", function () {
              $(".fullscreen-toggle").text(document.fullscreenElement ? "↩ Tam Ekrandan Çık" : "🔍 Tam Ekran");
          });
       
          // İlk yükleme
          processPDFs();
       
          // Sayfa değişimlerinde tekrar çalıştır (NodeBB SPA yapısına uygun)
          $(window).on('action:ajaxify.end', function () {
              processPDFs();
          });
       
          console.log("PDF izleme sistemi güncellendi: Mobilde sadece link gösteriliyor, masaüstünde önizleme aktif.");
      });
      
      eeeee@community.nodebb.orgE This user is from outside of this forum
      eeeee@community.nodebb.orgE This user is from outside of this forum
      [email protected]
      wrote last edited by
      #2

      It is getting increasingly difficult to stop downloads of pdfs. If you know how to, can circumvent the 'not for share or download' rules on google shared drives for example.
      Also iphones have very accurate select text from screen built in, so they can grab the text from pdfs very accurately

      1 Reply Last reply
      0
      • migelammon@community.nodebb.orgM [email protected]

        I have written the following JavaScript code to allow students to preview PDF files uploaded to our forum. Our goal is to enable them to view lecture notes directly on the forum while preventing them from downloading the files.

        However, since the PDFs are embedded within an iframe, I haven't been able to completely block the "Save As" option. Even though previewing works fine, students can still download the PDF files, especially on mobile devices where the "Save to Files" option appears.

        I need a solution that ensures PDFs can be previewed but not downloaded on both desktop and mobile devices. How can I achieve this? Thank you.

        $(document).ready(function () {
            function processPDFs() {
                console.log("PDF önizleme işleniyor...");
         
                $('a[href$=".pdf"]').each(function () {
                    let link = $(this).attr('href');
         
                    if (!$(this).next('.pdf-container').length) {
                        if (isMobileDevice()) {
                            // 📱 **Mobil cihazlarda sadece PDF linkini göster, önizleme yok**
                            $(this).show();
                        } else {
                            // 💻 **Masaüstünde PDF'yi önizle**
                            $(this).hide(); // PDF linkini gizle
         
                            let container = $('<div class="pdf-container" style="width:100%;max-width:900px;height:700px;position:relative;margin-top:10px;border:1px solid #ccc;overflow:hidden"></div>');
                            let pdfObject = $('');
                            let fullscreenBtn = $('<button class="fullscreen-toggle" style="position:absolute;top:10px;right:10px;background:#000;color:#fff;border:none;padding:5px 10px;cursor:pointer;z-index:10">🔍 Tam Ekran</button>');
         
                            pdfObject.attr('data', link + "#toolbar=0");
         
                            fullscreenBtn.on('click', function () {
                                toggleFullscreen(container[0]);
                            });
         
                            container.append(pdfObject).append(fullscreenBtn);
                            $(this).after(container);
                        }
                    }
                });
         
                $('.file a').each(function () {
                    let fileLink = $(this).attr('href');
                    if (fileLink.endsWith('.pdf') &amp;&amp; !$(this).next('.pdf-container').length) {
                        if (isMobileDevice()) {
                            // 📱 **Mobilde sadece PDF linki göster**
                            $(this).show();
                        } else {
                            // 💻 **Masaüstünde PDF'yi önizle**
                            $(this).hide();
         
                            let container = $('<div class="pdf-container" style="width:100%;max-width:900px;height:700px;position:relative;margin-top:10px;border:1px solid #ccc;overflow:hidden"></div>');
                            let pdfObject = $('');
                            let fullscreenBtn = $('<button class="fullscreen-toggle" style="position:absolute;top:10px;right:10px;background:#000;color:#fff;border:none;padding:5px 10px;cursor:pointer;z-index:10">🔍 Tam Ekran</button>');
         
                            pdfObject.attr('data', fileLink + "#toolbar=0");
         
                            fullscreenBtn.on('click', function () {
                                toggleFullscreen(container[0]);
                            });
         
                            container.append(pdfObject).append(fullscreenBtn);
                            $(this).after(container);
                        }
                    }
                });
            }
         
            // 📌 **Mobil Cihaz Algılama**
            function isMobileDevice() {
                return /Mobi|Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
            }
         
            // 📌 Tam Ekran Aç/Kapat Fonksiyonu
            function toggleFullscreen(element) {
                if (!document.fullscreenElement &amp;&amp; !document.mozFullScreenElement &amp;&amp; !document.webkitFullscreenElement &amp;&amp; !document.msFullscreenElement) {
                    if (element.requestFullscreen) {
                        element.requestFullscreen();
                    } else if (element.mozRequestFullScreen) {
                        element.mozRequestFullScreen();
                    } else if (element.webkitRequestFullscreen) {
                        element.webkitRequestFullscreen();
                    } else if (element.msRequestFullscreen) {
                        element.msRequestFullscreen();
                    }
                } else {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.mozCancelFullScreen) {
                        document.mozCancelFullScreen();
                    } else if (document.webkitExitFullscreen) {
                        document.webkitExitFullscreen();
                    } else if (document.msExitFullscreen) {
                        document.msExitFullscreen();
                    }
                }
            }
         
            // 📌 ESC ile Tam Ekrandan Çıkınca Butonu Güncelle
            $(document).on("fullscreenchange mozfullscreenchange webkitfullscreenchange msfullscreenchange", function () {
                $(".fullscreen-toggle").text(document.fullscreenElement ? "↩ Tam Ekrandan Çık" : "🔍 Tam Ekran");
            });
         
            // İlk yükleme
            processPDFs();
         
            // Sayfa değişimlerinde tekrar çalıştır (NodeBB SPA yapısına uygun)
            $(window).on('action:ajaxify.end', function () {
                processPDFs();
            });
         
            console.log("PDF izleme sistemi güncellendi: Mobilde sadece link gösteriliyor, masaüstünde önizleme aktif.");
        });
        
        julian@community.nodebb.orgJ This user is from outside of this forum
        julian@community.nodebb.orgJ This user is from outside of this forum
        [email protected]
        wrote last edited by
        #3

        @eeeee is right, it's pretty much an arms race between those who want to get the file, and those who don't want the file downloaded.

        Keep in mind that if a browser is viewing the file, then it has de facto already downloaded the file. The "save as" functionality is mostly a convenience feature for you to re-locate the downloaded file elsewhere.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        Powered by NodeBB Contributors
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups