Linux PDF – Modifying a PDF While Open in a PDF Viewer

pdfUbuntuwindows

When I am working with graphs in R, I try to output them directly into PDF. For e.g. the code for creating a graph is as follows.

library(ggplot2)
levels_fp = ggplot(data=df_forest_2,
    aes(x = levels,y=center, ymin=lower, ymax=upper))+
    geom_pointrange(aes(col=levels))+
    geom_hline(aes(fill=levels),yintercept =1, linetype=2)+
    xlab('Outcomes Evaluated')+ geom_errorbar(aes(ymin=lower, ymax=upper,col=levels),width=0.2,cex=1)+ 
    facet_wrap(~outcome,strip.position="top",nrow=1,scales = "free_x") + theme(
    axis.title = element_text(size = 18),
    axis.text = element_text(size = 14),
    legend.text = element_text(size = 16),
    legend.title = element_text(size = 16),
  
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
      
    axis.line = element_line(colour = "black"),
    legend.box.background = element_rect(),axis.text.x = element_blank(), strip.text.x = element_text(size = 14)
  ) + ylab("Odds Ratio")+labs(col="Interventions")

pdf(file="Forest_levels_2.pdf", width=12,height =12 ) 
levels_fp
dev.off()

The thing I am intrigued about is: suppose I have kept the pdf (which I output from the previous step, namely Forest_levels_2.pdf) open in a PDF viewer. Then, if I run the code again, the file gets output and overwrites the previous PDF, and the output in PDF viewer is also instantly changed.

But if I try to do the same in Windows, that is I have kept the PDF open with a PDF viewer, and I try to run the code, the following error will come –> cannot access pdf. Why there is a difference in behavior in Linux (I am working with Ubuntu 20.04)?

Adding the output of the Windows error

Error in pdf(file = "Forest_levels_2.pdf", width = 12, height = 12) : 
  cannot open file 'Forest_levels_2.pdf'

In order to remove some variables, I tried to download the Evince PDF viewer for windows (Evince 2.32) and then opened the PDF using Evince in Windows and did the same experiment again.

Guess what happened!?!

I was able to successfully modify the open PDF in Windows. Evince works same in both Ubuntu and Windows. So probably, it is more dependent on the property of PDF viewer also.

Best Answer

This is a function of the PDF viewer you are using.

I suspect, the PDF viewer opens the file, and then reads/processes the contents. It does not have to close the file, but it might.

Next, it sets up an inotify, meaning that if someone writes to the file, your PDF viewer will be notified.

When you wrote to the file, your PDF viewer received a signal that the file it was watching changed. This gives the PDF viewer a chance to re-read the file and re-process the contents.

Something similar is possible in Windows, but the PDF viewer you're using simply hasn't implemented that. When your Windows PDF viewer opens a PDF, it gets exclusive access by default to the PDF file which will cause another program to get an error when they attempt to open the file for writing. The viewer would have to make system calls to explicitly drop the exclusive access in order to operate in a similar manner to the linux version.


I found an interesting wikipedia article on File locking. It says:

Windows inherits the semantics of share-access controls from the MS-DOS system, where sharing was introduced in MS-DOS 3.3 . Thus, an application must explicitly allow sharing when it opens a file; otherwise it has exclusive read, write, and delete access to the file until closed (other types of access, such as those to retrieve the attributes of a file are allowed.)

It also says:

Unix-like operating systems (including Linux and Apple's macOS) do not normally automatically lock open files.

Related Question