Forum: support
Monitor Forum | | RE: png2pdf [ Reply ] By: Achim Zeileis on 2017-12-20 23:54 | [forum:45527] |
|
Michael, nice, thanks for tracking this down. This is much appreciated. I'll add this to the installation instructions on R-exams.org in the next days... Happy holidays, Z |
|
| RE: png2pdf [ Reply ] By: michael schulte on 2017-12-20 14:45 | [forum:45526] |
|
Achim, thanks for the steps - the problem was in the pdftk version I used - they link to an old version - here is the one for OSX > 10.11 https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.11-setup.pkg works up to High Sierra (10.13) ... after reinstalling pdftk also nops_scan() worked as expected. cool! thanks. m |
|
| RE: png2pdf [ Reply ] By: Achim Zeileis on 2017-12-20 11:39 | [forum:45525] |
|
Michael, thanks for the follow-up, very useful! For debugging the remaining problem, could you please try the following: Download the attached _NOPS_.pdf file and place it in some empty/temporary directory. First, try to do the following on the shell: pdftk _NOPS_.pdf rotate 1-enddown output _NOPS_2_.pdf mv -f _NOPS_2_.pdf _NOPS_.pdf pdftk _NOPS_.pdf burst output S%07d.pdf rm -f _NOPS_.pdf doc_data.txt convert -density 300 S0000001.pdf S0000001.PNG convert -density 300 S0000002.pdf S0000002.PNG convert -density 300 S0000003.pdf S0000003.PNG This uses 'pdftk' to rotate and split (aka burst) the PDF - and then 'convert's the PDF pages to PNG files. At the end there should be three PNG files which are ready for scanning. If this works, then I would hope also the R code works. It essentially uses system() calls to execute exactly the commands above. For safe calling of system() on Windows I have included a small wrapper function shsystem(). And then the commands above are put together. ## auxiliary function for "safe" system() under Windows shsystem <- function(cmd, ...) { sh <- Sys.getenv("COMSPEC") if(sh != "") sh <- paste(shQuote(sh), "/c") system(paste(sh, cmd), ...) } ## check for availability of 'pdftk' and 'convert' pdftk <- try(shsystem("pdftk --version", intern = TRUE, ignore.stdout = TRUE, ignore.stderr = TRUE), silent = TRUE) magic <- try(shsystem("convert --version", intern = TRUE, ignore.stdout = TRUE, ignore.stderr = TRUE), silent = TRUE) if(inherits(pdftk, "try-error")) stop("system requirement 'pdftk' is not available for merging/rotating/splitting PDFs") if(inherits(magic, "try-error")) stop("system requirement 'convert' is not available for converting PDF to PNG") ## rotate PDF shsystem("pdftk _NOPS_.pdf rotate 1-enddown output _NOPS_2_.pdf") file.remove("_NOPS_.pdf") file.rename("_NOPS_2_.pdf", "_NOPS_.pdf") ## burst PDF into individual pages shsystem("pdftk _NOPS_.pdf burst output S%07d.pdf") file.remove(c("_NOPS_.pdf", "doc_data.txt")) ## convert PDF to PNG for(i in dir(pattern = "\\.pdf$")) { cmd <- paste("convert -density 300", i, gsub(".pdf", ".PNG", i, fixed = TRUE)) shsystem(cmd) } If you can tell me where R/RStudio hangs, we can probably fix the code to also work (more robustly) on OS X. Thanks in advance! |
|
| RE: png2pdf [ Reply ] By: michael schulte on 2017-12-20 09:22 | [forum:45524] |
|
Achim, I was able to install PDFTk and ImageMagick through these links: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/pdftk_server-2.02-mac_osx-10.6-setup.pkg http://www.imagemagick.org/script/download.php (this needs: https://www.macports.org) Note that imagemagick requires macports which in turn automatically installs ghostscript as a dependency. - checked these commands on the terminal (command line) - works fine. - run nops_scan() and it hangs (for a small pdf) ... needs a complete restart of RStudio ... best Michael |
|
| RE: png2pdf [ Reply ] By: Achim Zeileis on 2017-12-19 21:50 | [forum:45523] |
|
Ralf, thanks for the pointer. I have never used sips so far, will try to have a look. Michael, pdf2pngs() is a (non-exported) function within the "exams" package. It relies on two commands being available on the command line: "pdftk" from the PDFTk toolkit and "convert" from ImageMagick. Some installation instructions are available at: http://www.R-exams.org/tutorials/installation/#optional-further-scanning-tools I have never tried this on OS X. So if you could provide some brief feedback what exactly needs to be done, I can include that information on R-exams.org. I have already considered some other paths for going from several multi-page PDF files to a set of (single-page) PNG files. In most situations the exact path is not very important as long as the resulting files are in sufficiently high resolution. However, if the check boxes on the exam sheet are scanned in a way that is "too pixelated" I have also seen false positives. Hence, I would recommend to sticking with PDFTk+ImageMagick for now. As usual: Don't hesitate to ask further questions if something is unclear! |
|
| RE: png2pdf [ Reply ] By: Ralf Schäfer on 2017-12-19 13:57 | [forum:45522] |
|
An easier solution would be to use sips. I use this shell script on the files before I import them into R - the script is: #!/bin/sh echo "Enter directory for file modification" read Input_directory cd $Input_directory for file in *.pdf; do sips -s format png $file --out "${file%\.*}".png && rm -f "$file"; done you could easily do all of this within R using the system command, if you wanted to play around. Regards Ralf |
|
| png2pdf [ Reply ] By: michael schulte on 2017-12-19 13:46 | [forum:45521] |
|
hi, I am using nops_scan() which works great with several *.png files. from the scanner here I get a pdf - so now I try to use nops_scan() with this file but apparently it needs the pdf2png program. Error in pdfs2pngs(images[pdfs], density = density, cores = cores, dir = tdir, : system requirement 'convert' is not available for converting PDF to PNG I am on a Mac OSX Sierra - after playing around with this: https://github.com/kaorukobo/pdf2png-mac and https://github.com/alfwatt/pdf2png I am a little stuck how to get this running ... any advice? (this is purely convenience - there is an pdf - AdobeAcrobat Pro export - route that works just fine ... thanks michael |
|

