How can I merge two pdf using pdfBox - java

Hi there I am trying to generate pdf by combining two one from my local machine and another one from s3 I am not sure how to do this
here is what I tried doing -
S3Object object = amazonS3.getObject(new GetObjectRequest(bucket, "name.pdf"));
InputStream ins = object.getObjectContent();
try {
PDDocument doc = PDDocument.load(ins);
File file1 = new File(
"C:\\Users\\admin\\example.pdf");
PDFMergerUtility obj = new PDFMergerUtility();
obj.setDestinationFileName(
"C:\\Users\\admin\\newMerged.pdf");
obj.addSource(file1);
obj.addSource(String.valueOf(doc));
obj.mergeDocuments();
System.out.println("PDF Documents merged to a single file");
}
catch (Exception e){
e.printStackTrace();
}
Error - org.apache.pdfbox.pdmodel.PDDocument#4d33940d (The system cannot find the file specified)
I know this could not be the way, I wanna know how to do this thing.

Consider to use absolute pathname to where the file exists.
PDFMergerUtility result = new PDFMergerUtility();
result.addSource("C:\\...\\FileRead\\first.txt");
result.addSource("C:\\...\\FileRead\\second.txt");
result.setDestinationFileName("the destination path");
result.mergeDocuments();

Related

How to save pdf to Aws s3

I am using PDFbox to merge two pdf files in my code, then I want to store the resultant (merged file) into AWS s3 bucket.
I was trying storing the pdf file to s3 directly without saving locally in my system, but I am not able to figure out any way to do it.
My code to merge two pdf-
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
String FinalFileName= "Merged.pdf";
pdfMergerUtility.setDestinationFileName(FinalFileName);
pdfMergerUtility.addSource(FileOne);
pdfMergerUtility.addSource(FileTwo);
pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
//To upload over s3
String fileNameIWantInS3 = "myfile.pdf";
s3.putObject(BucketName, fileNameIWantInS3, ??); //Stuck here
I don't want to make a file over my server instead I want to put it on s3 directly, how can I modify this code to upload Merged.pdf to s3 bucket.
Above code is just a part where I am stuck. FileOne and FileTwo I have created using File.createTempFile.
Entire idea is to merge two files and put the final file over s3 without making a physical copy of that over the server! Please help.
to upload the file you need to pass the byte array or input stream, the following use byte array:
public void saveFileToS3(String yourBucketName,String pathAws, String folderName) {
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
String FinalFileName= "Merged.pdf";
pdfMergerUtility.setDestinationFileName(FinalFileName);
pdfMergerUtility.addSource(FileOne);
pdfMergerUtility.addSource(FileTwo);
pdfMergerUtility.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
pdfMergerUtility.setDestinationStream(byteArrayOutputStream);
AmazonS3 s3Client = AwsUtil.s3Authentication();
PutObjectRequest objectRequest = PutObjectRequest.builder()
.bucket(yourBucketName)
.key(folderName+"/"+pathAws)//pathAws: path on S3 where the file will be saved
.build();
//convert it into a byte array
byte[] pdfBytes = byteArrayOutputStream.toByteArray()
CompletableFuture<PutObjectResponse> future =
s3Client.putObject(objectRequest,AsyncRequestBody.fromBytes(pdfBytes));
future.whenComplete((resp, err) -> {
try {
if (resp != null) {
System.out.println("Object uploaded. Details: " + resp);
} else {
err.printStackTrace();
}
} finally {
s3Client.close();
}
});
future.join();
}

Apache PDFBox to open temporary created PDF file

I'm using apache pdfbox 2.x version and I am trying to read a temp created file.
Below is my code to create a temp file and read it:
Path mergedTempFile = null;
try {
mergedTempFile = Files.createTempFile("merge_", ".pdf");
PDDocument pdDocument = PDDocument.load(mergedTempFile.toFile());
But it gives error:
java.io.IOException: Error: End-of-File, expected line
at org.apache.pdfbox.pdfparser.BaseParser.readLine(BaseParser.java:1098)
at org.apache.pdfbox.pdfparser.COSParser.parseHeader(COSParser.java:2577)
at org.apache.pdfbox.pdfparser.COSParser.parsePDFHeader(COSParser.java:2560)
at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:219)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1099)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1082)
at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1006)
at com.howtodoinjava.demo.PdfboxApi.test(PdfboxApi.java:326)
at com.howtodoinjava.demo.PdfboxApi.main(PdfboxApi.java:317)
From this link I have got a reference but it did not help anyway:
Similar Issue Link
Please help me with this. Still, I can not get rid of this.
PDDocument.load(...) is used to parse an existing PDF.
The passed temporary file (mergedTempFile) is empty, thus the exception. Just create a PDDocument with the constructor (resides in-memory) and later save it with PDDocument.save(...).
Path mergedTempFile = null;
try {
mergedTempFile = Files.createTempFile("merge_", ".pdf");
try (PDDocument pdDocument = new PDDocument()) {
// add content
pdDocument.addPage(new PDPage()); // empty page as an example
pdDocument.save(mergedTempFile.toFile());
}
} catch (IOException e) {
// exception handling
}
// use mergedTempFile for further logic

copy Chinese file source to destination in java [duplicate]

This question already has answers here:
Write chinese characters from one file to another
(2 answers)
Closed 7 years ago.
copying Chinese file in java using this code . but the destination file contains question mark (?) instead of Chinese character . is there any way in java to achieve this functionality..
File source = new File("H:\\work-temp\\file");
File dest = new File("H:\\work-temp\\file2");
try {
FileUtils.copyDirectory(source, dest);
} catch (IOException e) {
e.printStackTrace();
}
First of all, while copying a file, the destination should be a folder not file... so please change File dest = new File("H:\\work-temp\\file2"); to File dest = new File("H:\\work-temp");
Next you should use FileUtils.copyFile(source, dest); instead FileUtils.copyDirectory(source, dest);
Use one of the JDK 7 Files.copy methods. They create a binary copy of your file.
You are miss the extension of files for ex: file.txt and file2.txt:
File source = new File("H:\\work-temp\\file.txt");
File dest = new File("H:\\work-temp\\file2.txt");
For coping use this:
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try{
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
For more info go to this link

How to store a downloaded file object from S3 to a local directory

I'm trying to download a file from S3 using AWS SDK for Java and store the particular file in a local directory in my PC.
The code I wrote for downloading the object is:
public void download(String key) {
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
}
But what I actually want to do is to pass the local path as a parameter instead of key and store the downloaded file obj in that particular directory say /tmp/AWSStorage/ in my linux box.
Can you please suggest a way of doing it?
I used:
s3Client.getObject(new GetObjectRequest(bucket,key),file);
It worked fine.
There is an API to directly download file to local path
ObjectMetadata getObject(GetObjectRequest getObjectRequest,
File destinationFile)
With Java >=1.6, you can directly copy the files downloaded to local directory without any problem of file corruption. Check the code:
S3Object fetchFile = s3.getObject(new GetObjectRequest(bucketName, fileName));
final BufferedInputStream i = new BufferedInputStream(fetchFile.getObjectContent());
InputStream objectData = fetchFile.getObjectContent();
Files.copy(objectData, new File("D:\\" + fileName).toPath()); //location to local path
objectData.close();
With Java 1.6 and above, you can directly specify path in Files.copy function.
You can use obj.getDataInputStream() to get the file. And then org.apache.commons.io.IOUtils copy method to copy.
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
File file=new File("/tmp/AWSStorage/"+key);
// if the directory does not exist, create it
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
And then you can use either of following.
try {
IOUtils.copy(obj.getDataInputStream(), new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
OR
BufferedReader reader=null;
BufferedWriter out=null;
String data = null;
try {
reader = new BufferedReader(new InputStreamReader(fileObj.getDataInputStream()));
out = new BufferedWriter (new FileWriter(file));
while ((data = reader.readLine()) != null) {
out.write(data);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
reader.close();
out.close();
}
In the line S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,key));
bucketname is the S3BucketName and Key is the object name, it is not a local file path.
Key - is the combination of common-prefix / objectname
i.e. if you file is saved at root of bucket then only name of the object will be the key i.e. myfile.txt
but if your file is save like myfolder1/myfolder2/myfile.txt then
myfolder1/myfolder is your common-prefix and myfile.txt is objectname.
S3Object obj=s3Client.getObject(new GetObjectRequest(bucketname,"myfolder1/myfolder2/myfile.txt"));

Java: create temp file and replace with original

i need some help with creating file
Im trying in the last hours to work with RandomAccessFile and try to achieve the next logic:
getting a file object
creating a temporary file with similar name (how do i make sure the temp file will be created in same place as the given original one?)
write to this file
replace the original file on the disk with the temporary one (should be in original filename).
I look for a simple code who does that preferring with RandomAccessFile
I just don't how to solve these few steps right..
edited:
Okay so ive attachted this part of code
my problem is that i can't understand what should be the right steps..
the file isn't being created and i don't know how to do that "switch"
File tempFile = null;
String[] fileArray = null;
RandomAccessFile rafTemp = null;
try {
fileArray = FileTools.splitFileNameAndExtension(this.file);
tempFile = File.createTempFile(fileArray[0], "." + fileArray[1],
this.file); // also tried in the 3rd parameter this.file.getParentFile() still not working.
rafTemp = new RandomAccessFile(tempFile, "rw");
rafTemp.writeBytes("temp file content");
tempFile.renameTo(this.file);
} catch (IOException ex) {
ex.printStackTrace();
} finally {
rafTemp.close();
}
try {
// Create temp file.
File temp = File.createTempFile("TempFileName", ".tmp", new File("/"));
// Delete temp file when program exits.
temp.deleteOnExit();
// Write to temp file
BufferedWriter out = new BufferedWriter(new FileWriter(temp));
out.write("Some temp file content");
out.close();
// Original file
File orig = new File("/orig.txt");
// Copy the contents from temp to original file
FileChannel src = new FileInputStream(temp).getChannel();
FileChannel dest = new FileOutputStream(orig).getChannel();
dest.transferFrom(src, 0, src.size());
} catch (IOException e) { // Handle exceptions here}
you can direct overwrite file. or do following
create file in same directory with diff name
delete old file
rename new file

Categories

Resources