I am new to groovy and to this forum.
I am using a middleware tool(SAP CPI) where I am getting a pdf in base64 string format. I need to send the pdf to another system.
This is what I did:
Store the Base64 input in a string.
Pass it through a Base64 Decoder(this is an inbuilt function)
Use below code and pass the base64 string as an input to "body":
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message){
def body = message.getBody(String.class);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
outputStream.write(body.getBytes())
message.setBody(outputStream.toString())
return message;
}
what I get as an output looks something like below:
%PDF-1.7
%????
5 0 obj
<</Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>
endobj
14 0 obj
<</Title(T....
and so on..
Now when I save this output as a pdf file, it is either blank or it says "There was an error opening this document. The file is damaged and could not be repaired."
What am I doing wrong? Thanks in advance!
Note: Due to limitation of the middleware, I cannot create a whole project, but will rather have to use processData() function and can manipulate the string inside it. Thanks!
Related
I am using Beanshell sampler to save one pdf file content into another pdf file.
In Beanshell sampler I have put this following code:
FileInputStream in = new FileInputStream("C:\\Users\\Dey\\Downloads\\sample.pdf");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int i; (i = in.read(buffer)) != -1; ) {
bos.write(buffer, 0, i);
}
in.close();
byte[] extractdata = bos.toByteArray();
bos.close();
vars.put("extractdata", new String(extarctdata));
using beanshell post processor I saved this variable ${extractdata} in another pdf file .
file is generated but when open the file it's empty means there is no content showing.
So , can someone please tell me how to resolve this issue ?? is there anything wrong in above code snippet ?? please guide me.
You made a typo
byte[] extractdata = bos.toByteArray();
and
vars.put("extractdata", new String(extarctdata));
so your test element is silently failing, check jmeter.log file it should contain some errors.
It's not possible to state what else is wrong because we don't see your Beanshell Post-Processor code, most probably there is an issue with encoding when converting the byte array to string and vice versa.
So I would suggest skipping this step and using vars.putObject() function instead like:
vars.put("extractdata", extractdata);
and then
byte [] extractdata = vars.getObject("extractdata");
If you just need to copy the file you can use the following snippet:
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
Path source = Paths.get("C:\\Users\\Dey\\Downloads\\sample.pdf");
Path target = Paths.get("/location/for/the/new/file.pdf")
Files.copy(source, target);
Since JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy for scripting so you should switch to Groovy, in this case you will be able to do something like:
new File('/location/for/the/new/file.pdf').bytes = new File('C:\\Users\\Dey\\Downloads\\sample.pdf').bytes
More information on Groovy scripting in JMeter: Apache Groovy: What Is Groovy Used For?
val sanitizedHTML = Jsoup.clean(html, whitelist)
val textRenderer = ITextRenderer()
val outputStream = ByteArrayOutputStream()
textRenderer.setDocumentFromString(sanitizedHTML)
textRenderer.layout()
textRenderer.createPDF(outputStream)
textRenderer.finishPDF()
return Base64.getDecoder().decode(outputStream.toByteArray())
I would like to generate pdf from html content and rather than saving as file, would like to upload to server which expects it to be ByteArray.
I tried to do above using jsoup to clean html and textRenderer for generating pdf but keep receiving error about invalid Base64 character 25. Could someone help what I am doing wrong here.
return Base64.getDecoder().decode(outputStream.toByteArray())
This was incorrect, if I remove Base64 decoding it is working well.
I created a pdf document using the code looks like the following:
// The text parameter equels 'שדג' it is Hebrew. unicode equivalent is '\u05E9\u05D3\u05D2'
private static void createSimplePdf(String filename, String text) throws Exception {
final String path = RunItextApp.class.getResource("/Arial.ttf").getPath();
final PdfFont font = PdfFontFactory.createFont(path, PdfEncodings.IDENTITY_H);
Style hebrewStyle = new Style()
.setBaseDirection(BaseDirection.RIGHT_TO_LEFT)
.setFontSize(14)
.setFont(font);
final PdfWriter pdfWriter = new PdfWriter(filename);
final PdfDocument pdfDocument = new PdfDocument(pdfWriter);
final Document pdf = new Document(pdfDocument);
pdf.add(
new Paragraph(text)
.setFontScript(Character.UnicodeScript.HEBREW)
.addStyle(hebrewStyle)
);
pdf.close();
System.out.println("The document '" + filename + "' has been created.");
}
and after that, I tried to open this document using pdfbox util and I got the following data:
but I got an unexpected result in the Contents:stream section especially Tj tag. I expected string like the following 05E905D305D2 but I got 02b902a302a2. I tried to convert this hex string to normal string and I got the following result: ʹʣʢ but I expected that string שדג.
What do I wrong? Hot to convert this 02b902a302a2 string and get שדג?
This answer writes in a comment #usr2564301. Thanks for the help!
The numbers you get are not Unicode characters but font indexes instead. (Check how the font is embedded!) The text in a PDF does not specifically care about Unicode – it may or may not be this. Good PDF creators add a /ToUnicode table to help decoding, but it's optional.
I am creating an XmlDoc in C# and using Newtonsoft to serialize to JSON. It works, but I am getting a bunch of what appear to be "NUL"'s at the end of the JSON. No idea why. Anyone seen this before?
CODE:
XmlDocument xmlDoc = BuildTranslationXML(allTrans, applicationName, language);
// Convert the xml doc to json
// the conversion inserts \" instead of using a single quote, so we need to replace it
string charToReplace = "\"";
string jsonText = JsonConvert.SerializeXmlNode(xmlDoc);
// json to a stream
MemoryStream memoryStream = new MemoryStream();
TextWriter tw = new StreamWriter(memoryStream);
tw.Write(jsonText);
tw.Flush();
tw.Close();
// output the stream as a file
string fileName = string.Format("{0}_{1}.json", applicationName, language);
return File(memoryStream.GetBuffer(), "text/json", fileName);
The file is served up to the calling web page and the browser prompts the user to save the file. When opening the file, it displays the correct JSON but also has all the trailing nulls. See image below (hopefully the stackoverflow link works):
file screenshot
The GetBuffer() method returns the internal representation of the MemoryStream. Use ToArray() instead to get just the part of that internal array that has data Newtonsoft has put in there.
I am currently making an app for students where they can upload a PDF file to a server. I am using the android Volley API but have been testing the function using JPEG files.
This is my code
public String getStringImage(Bitmap bmp) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
How would I change this code so that a PDF can be uploaded instead?
Do I still used Base64? and imageBytes?
Or is there an alternate method?
You should be able to reuse most of the code that you have there, instead of taking in a Bitmap, you would take in a File. You won't be able to use Bitmap.Compress on it though so that will need to be removed.
For uploading a PDF, you can Base64 encode it and send it as a string as part of the request, this might get out of control if you are handling large files.
The other option is to use a multipart form, I would suggest taking a look at this stackoverflow question and answer for how to do that.