Detect if PDF contains searchable text - r

I want to read the text of 16000 pdf files in an R dataframe. Most of the pdfs are no Problem, pdf_text("link") gives the right output. The problem is that some PDFS have no copyable text. With them I get a following (sample) output:
"0123145456789 20ÿ\016\017ÿ\020\021\022ÿ\023\024\020\024\025\022ÿ\026\023ÿ\027\026\016\022\020\025\030ÿ\031\026\032\033\034\030ÿÿ\035\026\036\022\025\016\026\025ÿ\035 \037\025\033\022\032ÿ !\021\032\026\024\023\r\n ()*+ÿ-./.0012ÿ34ÿ5656ÿ7M9^.C=19ÿO=CDÿ-_`MQÿ-19GEHFC=19ÿ\.GW:CG7\r\n ()5+ÿ8.9:2ÿ;4ÿ5656ÿ7N10=#=:GÿJ1>ÿCD:ÿa>:.Cÿa01V.0ÿ<DECA1O9ÿ.9Aÿ\:K19A7\r\n ())+ÿ<:=0:>2ÿ5656ÿ7-1/=AÿY#191H=#Gbÿ̀:CC:Aÿ.9Aÿ>:.0cC=H:ÿF.F:>G7\r\n"
The solution is of course
a. Do not read out all these texts
b. Read the texts by OCR
But since this method either excludes many texts or is very time-consuming, I would like to recognize the problematic texts beforehand. Is there an easy way to do this in R?
text_raw<-pdf_text("link")
if(text_raw is nonsense){
text<-pdf_ocr_text(text_raw)
}else{
text<-text_raw
}

Related

Is there a way to count specific words from a corpus of PDFs in R?

How can I count the number of specific words in a corpus of PDFs?
I tried using text_count but I honestly didn't understand what it was returned.
First you would want to OCR the PDFs if necessary the convert them to raw text. pdftools can help with that and converting to text, but I am not sure that it can handle multiple columns.
https://cran.r-project.org/web/packages/pdftools/pdftools.pdf
Here is another post:
Use R to convert PDF files to text files for text mining
As above, you could use xpdf (installed via homebrew) to convert the pdfs, as I believe it has some more functionality as far as multiple columns/ text alignment goes.
After you have raw text, you can use a package like tm to obtain word counts in a corpus. Let me know if this works or if you have further questions.

R write fixed width columns to csv (in the plain text file)

I write data frames to csv files using write.csv(). When this is done, the output when viewed in a plain text editor, in particular vi or notepad++, shows no spacing between the column content and the commas, resulting in it being relatively hard to read. For example, the columns are not lined up down the page.
I have negative interest in using excel to view the csv files. I am definitely not looking for a suggestion for a csv viewer. Nor do I want instructions on how to modify the plain text file afterward. Padding needs to be spaces not tabs.
I am interested in how to get R to line up the columns in the plain text csv file so that they are easier to read using a non specialized plain text editor.
I could (and might) write my own routine that converts everything to some fixed width string format and print that. But, I would prefer to find that this is an option within write.csv() or similar common output library call.
[I just this moment found out about printf in R, and that might be the best answer to this conundrum].

Automation of images to PDF at District level

I want create a PDF with 5 Images from 5 folder with text. First I want to read the district name from CSV file and check the same file name in each folder. Second if the file name are matching, Make a PDF with the five images and CSV name as title for the PDF page and text which will be common for all the PDF. I want to give particular font and size, border for images, border for text also. I want to repeat for n number of districts. Is it possible with LaTeX or python?? Can anyone help me please, I am new to coding.
Thanks in advance.
In LaTeX including graphics is done with: \includegraphics and it's fairly straightforward. You can find a number of examples on the linked page above that will walk you through setting the pathname to each of your folders as necessary. There's also a good answer here about how to set multiple pathways in the declaration of your document. As a general note, LaTeX will definitely be more flexible with making pdfs than either r or LaTeX because that's what it was built for.

R: exporting formatted dataframe (with wordwrapping) to PDF **without** Rmarkdown

I'm trying to export a dataframe to a pdf file. My dataframe contains a column of text comments that are long, and so I would like to wrap the text in the table that would be produced within the .pdf. I already tried creating a table using grid.table and the pdf function, but I am not seeing any options for wrapping text. When I look up how to format a table, all the results seem to be talking about RMarkdown. My script has to be within an .R file, so I cannot knit it into a .pdf.
x <- data.frame("Question" = c("Comment1", "Comment2"), "Text"=c("This is one comments that's really long and I'd like the
width of the row to reflect that. I'd like it to wrap the text
so it is not cut off", "This is another comment that's really long and
gets cut off if I try to export to .pdf"))
Above is an example of a dataframe that I would like to export to a .pdf, using a wrap-text feature to control the width of the rows and not cut off the comments.
Is this possible?
Thank you!!
Best,
Kelly

Using R to transform all pages of a PDF into text, for multiple files

I'm using a looped 'pdf_render_page' function to create a bitmap of PDF documents that are then turned into raw text via the tesseract package. However this function works only given knowledge of file size. Does anyone know a way to take a pdf with an unknown page number total and discover the page count to then run this loop?
when using the pdftools package you can assign the length of pdf 'dummy.pdf' by doing:
pdf_length <- pdf_info("dummy.pdf")$pages

Resources