I need a QR code generator that generates 21500 unique serial number with a QR stamp, and export every 1000 code on a one PDF file, so we'll have 22 PDF file.
How can I do that?
Some time ago I've done a similar thing using Python, qrencode and LaTeX. I've modified my old code to fit your needs. I assumed you want A4 pages. The contents of the QR Codes are the PMY00001 to PMY22000 ASCII strings.
#!/usr/bin/env python
import random, base64, string, os, sys
width=7.7
height=7
print "\\documentclass[a4paper,10pt]{report}"
print "\\usepackage[absolute]{textpos}"
print "\\usepackage{nopageno}"
print "\\usepackage{graphicx}"
print "\\setlength{\\TPHorizModule}{1mm}"
print "\\setlength{\\TPVertModule}{1mm}"
print "\\textblockorigin{10mm}{10mm}"
print "\\setlength{\\parskip}{0pt}"
print "\\setlength{\\parindent}{0pt}"
print "\\setlength{\\fboxsep}{0pt}"
print "\\setlength{\\tabcolsep}{0pt}"
print "\\renewcommand{\\baselinestretch}{0.8}"
print ""
print "\\begin{document}"
idx=int(sys.argv[1])
for i in range(0,25):
for j in range(0,40):
b = 'PMY%05d' % idx
f = os.path.join("codes", b + ".png")
ff = os.popen("qrencode -lH -o " + f, "w")
ff.write(b)
ff.close()
print "\\begin{textblock}{" + str(width) + "}(" + str(width * i) + "," + str(height * j) + ")"
print "\\includegraphics[height="+str(height)+"mm]{" + f + "}"
print "\\end{textblock}"
idx=idx+1
print "\\end{document}"
To use it, write it as e.g. qrgen.py, add execution permissions chmod +x qrgen.py, create codes directory: mkdir codes and run ./qrgen.py 0 >codes.tex to generate the codes.tex document and then pdflatex codes.tex to generate codes.pdf file. The 0 argument is the starting serial number.
To get 22 such sheets it's best to use a loop:
for ((i=0;i<22;i++)); do ../qrgen.py $((i*1000+1)) >$i.tex; pdflatex $i.tex; done
Of course this is not the optimal solution - you can probably get a much faster one using Python qrencode library bindings instead of launching external qrencode program and some library for generating PDFs from Python directly instead of using pdflatex.
You can write a script in your language of choice that uses Google's QR code generator in a loop to generate all the codes you'll need and save them to a pdf. You'll need to provide more details if you need a more specific answer.
Related
So I want to convert these blogposts to PDF using wkhtmltopdf
On MacOS in automator I set up a WorkFlow: GET SPECIFIED TEXT > EXTRACT URLS FROM TEXT > RUN SHELL SCRIPT (shell: /bin/zsh, pass input: as arguments)
#!/bin/zsh
# url example: "https://www.somewordpressblog.com/2022/12/16/some-post-title/"
i=1
for url in "$#"
do
title1=${url[(ws:/:)6]} # split with delimiter / sixth part
title2=${(U)title1//-/ } # replace all hypens with spaces and capitalize
title3="$i. - " # prefix add numbers
title4=".pdf" # suffix add .PDF extension
title5="${title3}${title2}${title4}" # join everything
/usr/local/bin/wkhtmltopdf --disable-javascript --print-media-type $url $title5
((i+=1))
done
The files got downloaded, but for a test with only 2 URL's there was like 2 minutes waiting and the RESULTS from the Schell Script showed me 84 items!!
I am counting 14 DONES from the wkhtmltopdf output.
What is wrong with this loop? Do I need to implement something to wait for the loop to continue or something? And How?
Any code suggestions welcome as well, first day with ZSH..
I am trying to use
var <- as.numeric(readline(prompt="Enter a number: "))
and later use this in a calculation.
It works fine when running in RStudio but I need to be able to pass this input from the command line in Windows 10
I am using a batch file with a single line
Rscript.exe "C:\My Files\R_scripts\my_script.R"
When it gets to the user input part it freezes and it doesn't provide expected output.
From the documentation of readline():
This can only be used in an interactive session. [...] In non-interactive use the result is as if the response was RETURN and the value is "".
For non-interactive use - when calling R from the command line - I think you've got two options:
Use readLines(con = "stdin", n = 1) to read user input from the terminal.
Use commandArgs(trailingOnly = TRUE) to supply the input as an argument from the command line when calling the script instead.
Under is more information.
1. Using readLines()
readLines() looks very similar to readline() which you're using, but is meant to read files line by line. If we instead of a file points it to the standard input (con = "stdin") it will read user input from the terminal. We set n = 1 so that it stops reading from the command line when you press Enter (that is, it only read one line).
Example
Use readLines() in a R-script:
# some-r-file.R
# This is our prompt, since readLines doesn't provide one
cat("Please write something: ")
args <- readLines(con = "stdin", n = 1)
writeLines(args[[1]], "output.txt")
Call the script:
Rscript.exe "some-r-file.R"
It will now ask you for your input. Here is a screen capture from PowerShell, where I supplied "Any text!".
Then the output.txt will contain:
Any text!
2. UsingcommandArgs()
When calling an Rscript.exe from the terminal, you can add extra arguments. With commandArgs() you can capture these arguments and use them in your code.
Example:
Use commandArgs() in a R-script:
# some-r-file.R
args <- commandArgs(trailingOnly = TRUE)
writeLines(args[[1]], "output.txt")
Call the script:
Rscript.exe "some-r-file.R" "Any text!"
Then the output.txt will contain:
Any text!
I am trying to use
var <- as.numeric(readline(prompt="Enter a number: "))
and later use this in a calculation.
It works fine when running in RStudio but I need to be able to pass this input from the command line in Windows 10
I am using a batch file with a single line
Rscript.exe "C:\My Files\R_scripts\my_script.R"
When it gets to the user input part it freezes and it doesn't provide expected output.
From the documentation of readline():
This can only be used in an interactive session. [...] In non-interactive use the result is as if the response was RETURN and the value is "".
For non-interactive use - when calling R from the command line - I think you've got two options:
Use readLines(con = "stdin", n = 1) to read user input from the terminal.
Use commandArgs(trailingOnly = TRUE) to supply the input as an argument from the command line when calling the script instead.
Under is more information.
1. Using readLines()
readLines() looks very similar to readline() which you're using, but is meant to read files line by line. If we instead of a file points it to the standard input (con = "stdin") it will read user input from the terminal. We set n = 1 so that it stops reading from the command line when you press Enter (that is, it only read one line).
Example
Use readLines() in a R-script:
# some-r-file.R
# This is our prompt, since readLines doesn't provide one
cat("Please write something: ")
args <- readLines(con = "stdin", n = 1)
writeLines(args[[1]], "output.txt")
Call the script:
Rscript.exe "some-r-file.R"
It will now ask you for your input. Here is a screen capture from PowerShell, where I supplied "Any text!".
Then the output.txt will contain:
Any text!
2. UsingcommandArgs()
When calling an Rscript.exe from the terminal, you can add extra arguments. With commandArgs() you can capture these arguments and use them in your code.
Example:
Use commandArgs() in a R-script:
# some-r-file.R
args <- commandArgs(trailingOnly = TRUE)
writeLines(args[[1]], "output.txt")
Call the script:
Rscript.exe "some-r-file.R" "Any text!"
Then the output.txt will contain:
Any text!
Currently I'm executing:
frama-c -wp -wp-rte -report-rules test_rules.json -wp-split -wp-fct max -wp-status-maybe -wp-status-invalid -wp-timeout 10 -wp-prover alt-ergo -wp-par 12 -warn-signed-overflow -warn-unsigned-overflow -warn-special-float non-finite test.c -then -report-csv test.csv
I read documentation, but didn't find a good explanation how this JSON file works. I found some code at GitHub. But still it's not trivial for Frama-C novice.
I would like to have a CSV that has only rows with status different than Valid and only in test.c file (without dependencies). Is it possible this to be done from JSON config or I have to write custom parser?
I think there is some misunderstanding there: the -report-rules option is meant to be used in conjunction with -report-json. It has no effect on -report-csv, which will always output the whole list of properties. In fact, the very point of -report-csv is to import the resulting file into another tool in order to perform whatever operation you're interested in. For instance, you can simply open the file in your favorite spreadsheet editor and its built-in filters. But there are a lot of programming options as well. Building upon the script written here, here is an example using the python interpreter with the pandas library
>>> import pandas
>>> df = pandas.read_csv("test.csv",sep="\t")
>>> print('There are ' + str(len(df)) + ' properties.')
There are 77 properties.
>>> df = df[df['function']=='merge']
>>> print('There are ' + str(len(df)) + ' properties.')
There are 39 properties.
>>> df = df[df['status']=='Unknown']
>> print('There are ' + str(len(df)) + ' properties.')
There are 3 properties.
>>> print('There are ' + str(len(df)) + ' properties.')
>>> df.to_csv(path_or_buf='res.txt',sep='\t')
This gives me the 3 Unknown properties related to function merge in file res.csv (I hadn't a multi-file example available right away, but of course you would just have to use the file field in your first query). Just keep in mind that the "csv" file is in fact tabular-separated and not comma-separated (since commas tend to appear in ACSL formulas the latter would not be very practical).
I didn't realize that statistical charts and graphs made by R were using fonts not installed on my Windows machine, these are:
Helvetica
Helvetica-Bold
Helvetica-Oblique
ZapfDingbats
I discovered that by running pdffonts <file.name.pdf> from command line. So these fonts were not embedded in the PDF files.
I have my dissertation sent for printing but the printing house says these fonts need to be embedded in the PDF file. I have written it using LaTeX and included graphs as PDFs.
How to replace or substitute these fonts (some are licensed) with very similar ones without distorting the graphs in the individual PDFs? I don't intend to use commercial utilities.
Notes
- Windows 7 32 bit
- This post was similar:
https://superuser.com/a/223687/212779
However, it was done with a commercial program and is relatively old by now. There might be better new ideas to overcome this problem. I hope so.
My trial so far
I have succeeded to do the replacement using the procedure in the following post by some expert in Ghostscript: https://superuser.com/q/39167/212779
This requires Ghostscript to be installed (I have version 9.15) plus Adobe PostScript for Windows, a universal one and this script from command line:
gswin32c ^
-dNOPAUSE ^
-dBATCH ^
-sDEVICE=pdfwrite ^
-dPDFSETTINGS=/prepress ^
-dCompatibilityLevel=1.4 ^
-dHaveTrueTypes=true ^
-dSubsetFonts=true ^
-sOutputFile="c:\path\to\somename.pdf" ^
-c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" ^
-f "c:\path\to\somename.ps"
Question
How to get the generated .ps postscript file and subsequently the .pdf file (which is now gracefully embedded with a similar font) having the same size of the original PDF, i.e., cropped exactly to the same dimensions of the original PDF file?
If I leave all default settings in Adobe Reader (version XI) -- not to be confused with the commercial Adobe Professional, I get the same size in .ps file, so what I really need is some code in Ghostscript to preserved dimensions of the .ps file when generate the final PDF, any help? BTW, I open the .ps file with SumatraPDF viewer.
You were close: a previous SO question (12857849) mentioned adding -dEmbedAllFonts=true to the command line of gswin32c.
Your second question (in a comment) for batch-processing multiple PDFs can be done in multiple ways, the "best" being what is easiest for you to understand, maintain, and using a tool to which you have ready access.
In R:
fnames <- list.files(pattern = 'doctorate-.*.pdf')
for (fn in fnames) {
ofn <- paste0(gsub('\\.pdf$', '', fn, ignore.case = TRUE), '-withfonts.pdf')
message('Processing: ', fn, ' --> ', ofn)
system2('gswin32c',
paste0('-dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress ',
'-dCompatibilityLevel=1.4 -dHaveTrueTypes=true -dSubsetFonts=true ',
'-dEmbedAllFonts=true -sOutputFile="', ofn, '" ',
'-c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" ',
'-f "', fn, '"'))
}
This is a bit verbose. If you add stdout=FALSE in system2, all you should see is something like:
Processing: doctorate-1.pdf --> doctorate-1-withfonts.pdf
Processing: doctorate-2.pdf --> doctorate-2-withfonts.pdf
Processing: doctorate-3.pdf --> doctorate-3-withfonts.pdf
In bash:
for fn in doctorate-*.pdf ; do
ofn="$(basename ${fn} .pdf)-withfonts.pdf"
echo "Processing: ${fn} --> ${ofn}"
gswin32c -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress ^
-dCompatibilityLevel=1.4 -dHaveTrueTypes=true -dSubsetFonts=true ^
-dEmbedAllFonts=true -sOutputFile="${ofn}" ^
-c ".setpdfwrite <</NeverEmbed [ ]>> setdistillerparams" ^
-f "${fn}"
done
(Note that if you are doing this in a bash terminal under msys2 in windows, this will likely fail since msys2 sees "/prepress" and assumes it is a path, converting it to c:/msys64/prepress, which obviously means nothing to gs ...)
All what is needed in Windows is the following steps:
Step 1
Install Ghostscript and add to the PATH variables (Win + break is useful shortcut)
Step 2
Run this script from command line and go the target folder where you have the bad PDF (I do this to avoid writing the whole path).
Step 3
Check your bad PDF by running pdffont from command line:
pdffonts input-pdf-where-some-fonts-are-not-embedded.pdf
Step 4
Run this code to get the desired PDF with embedded fonts:
gswin32c ^
-sFONTPATH=C\Windows\Fonts ^
-o output-pdf-with-embedded-fonts.pdf ^
-sDEVICE=pdfwrite ^
-dPDFSETTINGS=/prepress ^
"input-pdf-where-some-fonts-are-not-embedded.pdf"