I got a Bad Request:400 error while sending base 64 PDF bytes to
DocuSign SOAP API(createAndSendEnvelope). Please help me in this issue.
Below is the code for converting PDF file into base 64 bytes:
File fFile = new File(pDFFileName);
byte[] bBytes = FileUtils.readFileToByteArray(fFile);
byte[] bBytesEnc = Base64.getEncoder().encode(bBytes);
documents.setPDFBytes(bBytesEnc);
I don't think you need to do Base64 of the byte array. I have below code without base64 and its working fine for me:
File f = new File(pDFFileName);
FileInputStream fs = new FileInputStream(f);
byte[] pdfBytes = new byte[(int) f.length()];
fs.read(pdfBytes);
fs.close();
Document doc = new Document();
doc.setPDFBytes(pdfBytes);
byte[] fileBytes = System.IO.File.ReadAllBytes(#"C:\Users\%username%\Desktop\File.PDF");
// Add a document to the envelope
Document doc = new Document();
doc.DocumentBase64 = System.Convert.ToBase64String(fileBytes);
doc.Name = "TestFile.pdf";
doc.DocumentId = "3";
I do this like that and it's workinf for me.
Related
I am using itext to read a large pdf file and save selected pages.
PdfReader reader = null;
reader = new PdfReader("customPath/largePdf.pdf");
int pages = reader.getNumberOfPages();
List<Integer> pagesList = new ArrayList<Integer>();
pagesList.add(1);
pagesList.add(2);
reader.selectPages(pagesList);
String path;
PdfStamper stamper = null;
path = String.format("customerPath/split.pdf");
stamper = new PdfStamper(reader, new FileOutputStream(path));
All good until now, i can open the split.pdf .
Now, Instead of saving to a file, i want to save it to a bytearray (so that i can save it as a blob later)
Tried this:
PdfReader reader = null;
reader = new PdfReader("customPath/largePdf.pdf");
int pages = reader.getNumberOfPages();
List<Integer> pagesList = new ArrayList<Integer>();
pagesList.add(1);
pagesList.add(2);
reader.selectPages(pagesList);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfStamper stamper2 = new PdfStamper(reader, baos);
byte[] byteARy = baos.toByteArray();
Just to make sure it works, i tried writing this bytearray to a file:
OutputStream out = new FileOutputStream("customPath/fromByteArray.pdf");
out.write(byteARy);
out.close();
fromByteArray.pdf does not open and the size is zero, any idea what might be wrong ?
You retrieve the byte array (using baos.toByteArray()) immediately after creating the PdfStamper.
PdfStamper stamper2 = new PdfStamper(reader, baos);
byte[] byteARy = baos.toByteArray();
At that time there is (next to) nothing in the output. You must instead wait until after closing your PdfStamper to retrieve the output.
PdfStamper stamper2 = new PdfStamper(reader, baos);
...
stamper2.close();
byte[] byteARy = baos.toByteArray();
Now the byte array should contain the complete, stamped PDF.
Below is my code to create PDF from input String or byte[]
This input is working with iText5. When I pass input to PdfReader in iText5 it was able to create PdfReader object.
Case 1:
byte[] bytesDecoded = Base64.decodeBase64(input.getBytes(StandardCharsets.UTF_8));
InputStream is = IOUtils.toInputStream(bytesDecoded.toString(), StandardCharsets.UTF_8);
PdfReader reader = new PdfReader(is);
Case 2:
PdfReader reader = new PdfReader(IOUtils.toInputStream(input, StandardCharsets.UTF_16));
Exception:
Exception in thread "main" com.itextpdf.io.IOException: PDF header not found.
at com.itextpdf.io.source.PdfTokenizer.getHeaderOffset(PdfTokenizer.java:223)
at com.itextpdf.kernel.pdf.PdfReader.getOffsetTokeniser(PdfReader.java:1018)
Not working in iText7
This is the way I have done. Sharing it as may be useful in future.
ByteArrayOutputStream pdfos = new ByteArrayOutputStream();
byte[] bytesDecoded = Base64.decodeBase64(src.getBytes(StandardCharsets.UTF_8));
ByteArrayInputStream inStream = new ByteArrayInputStream(bytesDecoded);
PdfReader reader = new PdfReader(inStream);
PdfSigner signer = new PdfSigner(reader, pdfos, false);
This is the easiest method to create a PdfReader from a Byte[] or a String.
byte[] template;
PdfReader templatePdf = new PdfReader(new RandomAccessSourceFactory().CreateSource(new MemoryStream(template).ToArray()),new ReaderProperties());
When I try upload the file from server, I use this code. I get the file size of 500kb, when the original file size about 300kb.
What am I doing wrong?
attachmentContent = applicationApi.getApplicationAttachmentContent(applicationame);
InputStream in = new BufferedInputStream(attachmentContent.getAttachmentContent().getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n=0;
while (-1 !=(n=in.read(buf)))
{
out.write(buf,0,n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.write(response);
outStream.close();
Simplify. The same result:
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.close();
The ByteArrayOutputStream is a complete waste of time and space. You're reading the attachment twice, and writing it twice too. Just read from the attachment and write directly to the file. Simplify, simplify. You don't need 90% of this.
What is the efficient way to encode and decode an audio file in android
I have tried the Base64 as below but after decoding the file size get increased.
Encode
ByteArrayOutputStream objByteArrayOS = new ByteArrayOutputStream();
File file = new File(path);
FileInputStream objFileIS;
try {
objFileIS = new FileInputStream(file);
byte[] byteBufferString = new byte[1024];
for (int readNum; (readNum = objFileIS.read(byteBufferString)) != -1;) {
objByteArrayOS.write(byteBufferString, 0, readNum);
System.out.println("read " + readNum + " bytes,");
byte[] bytes = FileUtils.readFileToByteArray(file);
strAttachmentCoded = Base64.encodeToString(bytes,
Base64.DEFAULT);
Decode
byte[] decoded = Base64.decode(strAttachmentCoded,
Base64.DEFAULT);
// byte[] decoded1 = Base64.decode(byteBinaryData1, 0);
File file1 = new File(pathAudio);
FileOutputStream os = new FileOutputStream(file1, true);
os.write(decoded);
os.close();
i want audio in String format to send to server and retrieve the same as per the user requirement.
wikipedia:base64 (rfc3548) is the right method to choose I would think. It is most common now I think having taken over from wikipedia:uuencoding.
To answer the question . . .
You could add some padding. The wikipedia article on base64 gives a good example of padding.
Or you could add a header to your audio string including length. The header could also include other control data so it may be something you want to include anyway.
How do I read an image into a base64 encoded string by its ImageReader?
Here's example source code using HtmlUnit. I want to get the base64 String of img:
WebClient wc = new WebClient();
wc.setThrowExceptionOnFailingStatusCode(false);
wc.setThrowExceptionOnScriptError(false);
HtmlPage p = wc.getPage("http://flickr.com");
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
System.out.println(img.getImageReader().getFormatName());
The HtmlUnit's HtmlImage#getImageReader() returns javax.imageio.ImageReader which is part of standard Java 2D API. You can get an BufferedImage out of it which you in turn can write to an OutputStream of any flavor using ImageIO#write().
The Apache Commons Codec has a Base64OutputStream which you can just decorate your OutputStream with.
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
ImageReader imageReader = img.getImageReader();
BufferedImage bufferedImage = imageReader.read(0);
String formatName = imageReader.getFormatName();
ByteArrayOutputStream byteaOutput = new ByteArrayOutputStream();
Base64OutputStream base64Output = new base64OutputStream(byteaOutput);
ImageIO.write(bufferedImage, formatName, base64output);
String base64 = new String(byteaOutput.toByteArray());
Or if you want to write it to file directly:
// ...
FileOutputStream fileOutput = new FileOutputStream("/base64.txt");
Base64OutputStream base64Output = new base64OutputStream(fileOutput);
ImageIO.write(bufferedImage, formatName, base64output);
I'm not quite sure what exactly you want.
But what about creating your own Reader (see javax.imageio.stream.ImageInputStreamImpl), containing the Base64-stuff?
Maybe this external free Base64Encoder can help you out.
Something that could be used like this in the end?
WebClient wc = new WebClient();
wc.setThrowExceptionOnFailingStatusCode(false);
wc.setThrowExceptionOnScriptError(false);
HtmlPage p = wc.getPage("http://flickr.com");
HtmlImage img = (HtmlImage) p.getByXPath("//img").get(3);
MyBase64EncodingReader reader = new MyBase64EncodingReader(img);
System.out.println(reader.toString());
You could use one of the encodeBase64 methods
from apache commons codec.
and create a string from the resulting byte array using the String(bytes[]) constructor.