How do I get the byte array from a PDF generated by pdfMake? - pdf

I have a web app that is creating a PDF using pdfMake. I want to get the byte[] from the generated pdf to be used to transmit to a database. How can I get the byte[] of the pdf generated by pdfmake?

You can use the following method provided by the library to fetch the array of the generated PDF.
pdfMake.createPdf(docDefinition).getBuffer(function (databuffer) {
data = databuffer;
});

Related

Control asset loading in babylonjs for authentication

All examples I could find just pass the url to babylonjs and it does a GET internally.
I need to either add an authentication header to this request, or just do the loading myself and pass the byte array to babylonjs.
How is authentication supposed to work with babylonjs?
Hello in this case you can just do the loading by yourself and then creates a blob and use object.createObjectUrl to refer to it:
var blob = new Blob([data]);
var url = window.URL.createobjecturl(blob);

Attachment.Name when retrieving email attachments is null

I'm using openpop to retrieve and process emails, In processing the mails, I'm checking for attachments and saving them to a specific folder. This works fine for csv files but for some reason for pdfs "att.Name" is returning null and won't save.
AttachmentCollection attachments = mailItem.Attachments;
foreach (Attachment att in attachments)
{
using (var fileStream = new FileStream(conf.AttachmentSaveTo + att.Name, FileMode.Create, FileAccess.Write))
{
att.ContentStream.CopyTo(fileStream);
}
}
Any help, much appreciated.
Without having access to the raw message, it's possible that the MIME headers for your pdf attachments do not specify a file name.
If you look at the raw message source, check to see if your pdf attachments have a Content-Disposition header and make sure that they have a filename parameter.
If not, does the Content-Type header have a name parameter?
If at least 1 of those 2 parameters is present, perhaps you have discovered a bug in OpenPOP. If that's the case, I would suggest using my MailKit library instead.

If it isn't Base64, what might it be?

Original Question
I'm using an API to get a thumbnail image that I uploaded. Instead of providing me with a url to the image, the service returns me a garbles text string that I thought was Base64. However, all of my attempts to decode this string have failed. Does anyone have any ideas of what object types the service might be returning me? If it's not base64, what would it be?
screenshot on imgur of the API response
Answer to Original Question
#Andrew Tran: pointed out that the response I was getting looked like it was the raw binary data for the png file. This helped correct my lack of experience with Base64 and led me to some further research.
First Attempt at downloading the file
Dim path As String = "C:\Users\username\lpImages\img1.png"
Dim fs As FileStream = File.Create(path)
Dim info As Byte() = New UTF8Encoding(True).GetBytes(thumbnail)
fs.Write(info, 0, info.Length)
fs.Close()
This never worked... so I talked with a coworker and finally realized my mistake
I had been using an abstracted class to call the API and had glazed over the fact that under the hood the "request" method was actually reading the return stream into a string. The service returned the image png file as a stream, but the request method was converting this raw stream. Once I created a different request method I was able to get the png as a stream. From there it was relatively easy to use it as I intended: attaching the images as LinkedResources to an email that I then send out.
Original request method code
response = theRequest.GetResponse
Dim reader As StreamReader = New StreamReader(response.GetResponseStream)
lp_response.response = reader.ReadToEnd
New request code I wrote instead
lp_response.response = theRequest.GetResponse.GetResponseStream
VB.Net code to handle the stream (this is just a snippet where I'm building a List of LinkedResources to pass to my email function; just to give an idea of how I'm using it)
Dim document As Stream = LPApi.GetDocumentThumbnail(d("id").ToString)
Dim mediaType As String = Utils.GetContentType(Path.GetExtension(d("file_name")))
Dim lrDocument As New LinkedResource(document, mediaType)
lrDocument.ContentId = d("id").ToString
Thanks to everyone who commented. I'm pretty inexperienced when it comes to the deeper architecture of web requests/responses and data serialization. Any good learning resources would be helpful; otherwise I'll just keep Googling :)
It doesn't look like it's being encoded at all.
The IHDR chunk start is clearly visible and in plaintext at the start of the file.
It's probably not a good idea to use HttpWebRequest to download binary data, then convert the response to a string, just to try to convert that string back into binary data to save it to a file.
If you're getting an HttpWebResponse object, you should be able to use HttpWebResponse.GetResponseStream to get a stream for the image data. Then you should be able to copy the image data straight into your filestream using Stream.CopyTo.

loading response data into web view Titanium

I got response data from the web services, which is base64binary data.
I want to load this base64binary data into web view for titanium alloy [version 3.1.0.2].
The data base64binary is of pdf file.
Ti.API.info('Status is ::',xhrDocument.status);
var ResponseData = xhrDocument.getResponseXML().getElementsByTagName('GetDocResult').item(0).text;
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'pdfbinarray.pdf');
if(xhrDocument.status == 200){
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'filename2.pdf'); file.write(xhrDocument.getResponseXML().getElementsByTagName('GetDocResult').item(0).text);
Titanium.API.info('file write');
Titanium.API.info(file.size);
}
The above code created filename2.pdf in my Documents directory. When I open the file using Adobe Reader, it says Adobe Reader could not open filename2.pdf because it is either not a valid file or has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
Is the web service call returning ONLY the document, or is there additional data included in the response?
We have had success using a simpler method. If the service is simply returning the document, try changing line two to something more like this:
var ResponseData = xhrDocument.responseText;

Splunk java sdk return XML

I am making a REST call to Splunk and need to now how I get the full XML from ResultsReaderXml
InputStream results = jobSavedSearch.getResults();
ResultsReaderXml resultsReader = new ResultsReaderXml(results);
I can push the reader to a hash and print, but I want the full XML.
The results reader is meant to tokenize the XML into key value pairs. If you want the raw XML, just pull the data directly from the stream. For example:
InputStream results = jobSavedSearch.getResults();
InputStreamReader reader = new InputStreamReader(results, "UTF8");
Now you can read directly from the stream reader -- its all just a stream of text at this point.