Create a PDF file using LaTex templates and Java - java

I need to create a report system using LaTex templates and Java like programming language. I use JLR library but it's freeeware, this is my code:
File workingDirectory = new File("./config/output");
File desktop = new File("./config/desktop");
File invoice1 = new File("./config/templates/template1.tex");
File invoice2 = new File("./config/templates/template2.tex");
JLRGenerator pdfGen = new JLRGenerator();
pdfGen.deleteTempTexFile(false);
if (!pdfGen.generate(invoice1, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
if (!pdfGen.generate(invoice2, desktop, workingDirectory)) {
System.out.println(pdfGen.getErrorMessage());
}
JLROpener.open(pdfGen.getPDF());
Searching into the Web I find JLatexMath but, as far as I know, only generates equations in LaTex and not the entire PDF. Do you know a library in Java which generates an entire PDF file using a LaTex template?
Update: I execute de .tex file using Runtime.getRuntime().exec("pdflatex.exe...") command. But I don't achive save the PDF file.
Thanks in advance

I think I have the solution, here is the code:
public void generateReport()
{
Process p;
try {
p = Runtime.getRuntime().exec("C:\\pdflatex.exe -synctex=1 -interaction=nonstopmode ./config/log/document.tex");
p.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It generates a .PDF and .dvi file in the same place of your .tex file. Thank you much for your help :)

Related

How to decrypt .dat file with (possibly) java code?

I have a .dat file downloaded from a game, which appears to have some Java code or values in it, along with other illegible text. Here's some of the content:
My question is, is there any way I can decode this file? Or, how can I work out which encryption algorithm it uses so I can search more accurately on how to decrypt it?
The solution from #user207421 worked perfectly, i ended up using this code to get the file info:
public static void main(String[] args) {
try {
FileInputStream fileInputStream = new FileInputStream("./file.dat");
ObjectInputStream inputStream = new ObjectInputStream(fileInputStream);
try {
System.out.println(inputStream.readObject());
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}

Properties file sleep

I have class which will load the property keys. During execution it sleeps for 20 seconds, and while it is sleeping, I add a new key to the property file and save it. When the program wakes up, it again reads the property file, but its output does not show the property added during its sleep. How can I make the program recognize the property file change?
Here is my current code:
public class Test {
public static void main(String[] args) {
Properties props = new Properties();
InputStream is = Test.class.getClassLoader().getResourceAsStream(
"com/vijayspring/common/CityIns.properties");
try {
props.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(props.stringPropertyNames());
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Properties props1 = new Properties();
InputStream is1 = Test.class.getClassLoader().getResourceAsStream(
"com/vijayspring/common/CityIns.properties");
try {
props1.load(is1);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(props1.stringPropertyNames());
}
}
As you are referring to a src folder, I assume you are using an IDE. If I knew which IDE it is exactly, I could be more accurate with my answer.
Nevertheless, the ClassLoader.getResourceAsStream() method is reading the property file from the bin (the actual name might vary per IDE) folder, which is usually built by the IDE once when it launches the java program. Any files that are no java source files, hence do not end with .java are simply copied from src to bin.
So after 20 seconds your program will read the unedited properties file in the bin folder.

OutputStreamWriter contains a path separator

Today i tested "files" instead of a normal path.
Here is my code:
File path=new File(getFilesDir(),"uf");
File test = new File(path.getAbsolutePath(),"test.txt");
if(!path.exists()){
path.mkdir();
}
if(!test.isFile()){
try {
test.createNewFile();
} catch (IOException e) {
//TODO in errorlog -> filecreation
e.printStackTrace();
}
}else{
try {
OutputStreamWriter mywriter = new OutputStreamWriter(openFileOutput(test.getAbsolutePath().toString(),Context.M ODE_PRIVATE));
mywriter.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
In the error code comes everytime: illegal Arguments: contains a path seperator!
Thank you for your help in advance
Maybe add more info about your error. But...
This error is about that you are trying to full path (include you subdirectories) to access to private data area.
Solution is use FileOutputStream, more here. And use
new File(YOUR_FILE)
to create your file.
Keep on mind that you should call method mkDirs() to create all necessary directories and subdirectories. More about mkDirs() here
Note: There is also method mkDir(), here is doc. This one will create a single directory.

Why isn't a new created file local?

I have the problem, that I create a new file in a Java program, but I always get an exception, that the new created file is not local, when I try to open it on the eclipse project explorer view.
The code where I create it is as follows:
IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
IProject project = workspaceRoot.getProject(projectName);
FileUtil myFile = new FileUtil();
if (!project.getFile(FILE_NAME).exists()) {
IFile newFile = project.getFile("conf.txt");
FileInputStream fileStream = null;
try {
String temp = project + "/conf.txt";
temp = temp.substring(2);
fileStream = new FileInputStream(temp);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
newFile.create(fileStream, false, null);
} catch (CoreException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// create closes the file stream, so no worries.
try {
myFile.writeTextFile(FILE_NAME, "Seconds", output);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
FileUtil is a class which only implements the methods write and read for the file.
The Exception I get when I try to open it begins with:
org.eclipse.core.internal.resources.ResourceException: Resource '/ProjectE1/conf.txt' is not local.
at org.eclipse.core.internal.resources.Resource.checkLocal(Resource.java:353)
at org.eclipse.core.internal.resources.File.getContentDescription(File.java:264)
at org.eclipse.core.internal.propertytester.FilePropertyTester.testContentType(FilePropertyTester.java:108)
I somehow have to get a relative path during the runtime. Because I am opening a new instance of eclipse in the program, where I can see the Project in the Project Explorer but can't open the conf.txt file because it is not local.
It looks like your resource is an absolute path to /ProjectE1/conf.txt, I'm confused why you are not using java.io.
This will help you understand relative paths, I think this may be where you are wanting to put your conf file.
File file = new File("conf.txt");
if(!file.createNewFile()){
//err
}
System.out.println(file.getAbsolutePath());
I had similar issue. This is how I fixed.
First created file in local file system (using java.io)
Did project refresh
Reload the file
File file = new File(project.getWorkspace().getRoot().getLocation() + project.getFullPath().toString() + "/relative_path_of_my_file");
file.createNewFile();
project.refreshLocal(IProject.DEPTH_INFINITE, null);
keywordFile = project.getFile("/relative_path_of_my_file");

Why was the file created in that path?

As a example:
public class Hello {
public static void main(String[] args) {
try {
OutputStream os = new FileOutputStream(new File("c.txt"));
try {
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Why was c.txt generated in the root path of current project other then the same path of the java file?
thanks.
Because the root of your project is your current working directory when starting the JVM.
You can check the user.dir system property to see what is your current working directory. If you access a file without a leading slash (Unix) or drive specifier/backslash (Windows), the files will be created relative to your current working directory.
You haven't provided a full path - this means that File constructor will use your process's current directory.
The path you provided will point to the project directory only if you want to change you can
also you can mention the full path where file is to be generated.
OutputStream os = new FileOutputStream(new File("c.txt"));
OutputStream os = new FileOutputStream(new File("D:\\c.txt"));

Categories