DataScience Workbook / 07: Data Acquisition and Wrangling / 1. Remote data access / 1.3. Remote data preview / 1.3.2. Viewing PDF and PNG files using X11 SSH connection

Introduction

PDF (Portable Document Format) is a widely used file format designed to present documents consistently across multiple devices and platforms, preserving fonts, images, graphics, and layout. It is ideal for distributing read-only documents that need to maintain exact formatting for printing or professional presentation.

PNG (Portable Network Graphics) is a popular image file format known for its lossless compression, allowing for detailed, high-quality images without data loss. It supports transparency and is commonly used for web graphics, digital art, and anywhere image clarity and fidelity are important. PNG is ideal for storing line drawings, text, and iconic graphics at a small file size.

X11 forwarding in an SSH connection allows users to run software on a remote machine while forwarding the graphical output to their local machine’s display. This technique is especially useful for viewing or interacting with graphical applications, like PDF viewers, on a server where no direct graphical user interface is available.

PDF files

Most HPC infrastructures allows X11 forwarding. You can enable this when you are logging in to the remote host using the -X option.
For example:

ssh -X username@nova.its.iastate.edu
(username@nova.its.iastate.edu) Verification code:
(username@nova.its.iastate.edu) Password:
Last login: Tue May 14 11:31:09 2024 from 173.22.227.6

Use the SSH command to connect to your remote server with X11 forwarding enabled.
Include the -Y flag, which is more secure than -X for enabling trusted X11 forwarding:

ssh -Y username@nova.its.iastate.edu

Once logged in, you can now open any GUI interface programs on the remote-host and it appears as a new window in your local machine.

Although, there are plenty of program options to open a PDF file, you can almost always find the firefox browser in every machine. This should be sufficient to open a PDF file.
To get started, type:

firefox

Please note that you may be prompted to install or update XQuartz on your local machine, especially if you are using a Mac. XQuartz is essential for handling the X11 forwarding process, allowing graphical data from the remote server to be displayed properly on your computer. Ensure that you have the latest version of XQuartz installed to avoid compatibility issues.

Finally, a new window of web browser will open:

You can use a few approaches to open a specific file in Firefox:

  1. Go to File » Open file » and select the pdf file you want to open.
  2. If you don’t find the menu tab, pressing alt + f should bring up the file menu.
  3. You can also replace your starting URL with the file:///. This will render the graphical interface of the remote file system, enabling you to easily navigate through it until you locate your PDF file.

    Remember, performance and responsiveness might vary depending on your network speed and the graphical demands of applications being forwarded. Also, ensure that network permissions and firewall settings allow for X11 forwarding.

  4. If you know the file path, you can open a file directly in a terminal by typing:
    firefox file:///path/to/your/file.pdf
    

    firefox file:////project/geospatial/ODM/RESULTS/odm_report/report.pdf

    The file will be opened in a new pop-up window or as a new tab in your browser, providing a convenient and isolated view of the content.

If you don’t have a PDF file readily available on your remote cluster, you can quickly download a sample PDF file from Wikipedia:

# download the PDF file
wget https://en.wikipedia.org/api/rest_v1/page/pdf/List_of_PDF_software
# make sure the file has .pdf extension
mv List_of_PDF_software List_of_PDF_software.pdf
# preview the PDF in Firefox web browser
firefox List_of_PDF_software.pdf

Due to the limited responsiveness often associated with X11 forwarding, it is recommended to directly open a file from a known path in the terminal for a more efficient and reliable experience.

PNG files

Again, you need X11 forwarding enabled to do this.

Let’s try a few approaches. If you don’t have a PNG image readily available, navigate to the desired location in the remote file system and download the Linux logo:

wget https://upload.wikimedia.org/wikipedia/commons/thumb/3/35/Tux.svg/800px-Tux.svg.png
mv 800px-Tux.svg.png example.png
ls
example.png

ImageMagic

  1. If your machine/HPC infrastructure has imagemagick module installed, load it:
    module load imagemagick
    
  2. To open any picture files, you can just do:
    display example.png
    

Gnuplot

GNU Plot, primarily a plotting utility, is not directly used for simply displaying PNG files like a conventional image viewer. However, you can easily trick it to display your PNG file in a pop-up preview.

  1. If your machine/HPC infrastructure has gnuplot module installed, load it:
    module load gnuplot
    
  2. Launch interactive gnuplot session directly in your terminal:
    gnuplot
    
    gnuplot
    
     G N U P L O T
     Version 5.4 patchlevel 8    last modified 2023-06-01
    
     Copyright (C) 1986-1993, 1998, 2004, 2007-2023
     Thomas Williams, Colin Kelley and many others
    
     gnuplot home:     http://www.gnuplot.info
     faq, bugs, etc:   type "help FAQ"
     immediate help:   type "help"  (plot window: hit 'h')
    
     Terminal type is now 'x11'

    You need to ensure that GNU Plot uses a terminal type that supports displaying graphical output, such as wxt, qt, or x11 depending on what is available on your system. In the interactive gnuplot session, set terminal to ‘wxt’ or ‘x11’ for enabling pop-up GUI window:

    gnuplot
    set terminal wxt size 800,600 enhanced font 'Verdana,10' persist
  3. Then, load the PNG image into the plot area:
     plot 'path/to/your/image.png' binary filetype=png with rgbimage
    
    plot 'example.png' binary filetype=png with rgbimage

Firefox

If you don’t have any other program installed, you can try the firefox method.
Although, to view PNG files, you may have to install the addon before you open the file. Addons are easy to add, once the firefox is opened, press ctrl + shift + A, that will open add-ons page (you can also just type about:addons to open this). Search for Perfect View ⤴ add-on, and add it to your firefox.

Now you can open the file normally as you opened the PDF files:

firefox example.png