Warning: Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 239" on PDF response from springboot API after dropzone upload - angularjs

I'm using dropzone to send files to a springboot api, which processes those files and returns a pdf response, which I want to display on my angular front-end. Everything works fine until time to display the pdf, when it renders a blank pdf (with the correct number of pages) along with the warnings in console:
Warning: Indexing all PDF objects pdf.worker.min.js:1
Warning: Invalid stream: "FormatError: Bad FCHECK in flate stream: 120, 239"
I know that the pdf itself is not corrupt, because when I make a get request to the API to retrieve the stored version of the same pdf, it is not corrupt and renders fine.
I have tried using TextEncoder:
let enc = new TextEncoder();
this.masterPdf = enc.encode(event[1])
using FileReader:
var reader = new FileReader();
  reader.onload = function() {
var arrayBuffer = this.result;
self.masterPdf = new Uint8Array(arrayBuffer);
console.log(self.masterPdf);
self.masterShow = true;
}
var blob = new Blob([event[1]], {type:'application/pdf'});
reader.readAsArrayBuffer(blob);
and using StringToBytes:
let strToByteArr = stringToBytes(event[1]);
this.masterPdf = new Uint8Array(strToByteArr);
event in all of these cases is the response, here it is below (of course the pdf string is actually much longer):
0: File(1237) {upload: {…}, status: "success", accepted: true, processing: true, xhr: XMLHttpRequest, …}
1:"%PDF-1.7↵%����↵1 0 obj↵<</Type/Catalog/Pages 2 0 R/Lang(en-US) /StructTreeRoot 12 0 R/MarkInfo<</Marked true>>/Metadata 32 0 R/ViewerPreferences 33 0 R>>↵endobj↵2 0 obj↵<</Type/Pages/Count 2/Kids[ 3 0 R 9 0 R] >>↵endobj↵3 0 obj↵<</Type/Page/Parent 2 0 R/Resources<</Font<</F1 5 0 R>>/ExtGState<</GS7 7 0 R/GS8 8 0 R>>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/MediaBox[ 0 0 612 792] /Contents 4 0 R/Group<</Type/Group/S/Transparency/CS/DeviceRGB>>/Tabs/S/StructParents 0>>↵endobj↵4 0 obj↵<</Filter/FlateDecode/Length 3808>>↵stream
2: ProgressEvent {isTrusted: true, lengthComputable: true, loaded: 99282, total: 99282, type: "load", …}
I did check those particular bytes (120-239) in the valid file returned versus the invalid one, and they are different, but the invalid one just looked like normal numbers.

When requesting a PDF from an API, set the request's response type to 'blob'. The default type is application/json.
Once the file has been received, create a blob using the response's data then use the blob to create a url.
let blob = new Blob([data], {type: 'application/pdf'})
let url = window.URL.createObjectURL(blob)

Related

Parsing a pipe delimited json data in python

I am trying to parse an API response which is JSON. the JSON looks like this:
{
'id': 112,
'name': 'stalin-PC',
'type': 'IP4Address',
'properties': 'address=10.0.1.110|ipLong=277893412|state=DHCP Allocated|macAddress=41-1z-y4-23-dd-98|'
}
It's length is 1200, If i convert it I should get 1200 rows. My goal is to parse this json like below:
id name type address iplong state macAddress
112 stalin-PC IP4Address 10.0.1.110 277893412 DHCP Allocated 41-1z-y4-23-dd-98
I am getting the first 3 elements but having an issue in "properties" key which value is pipe delimited. I have tried the below code:
for network in networks: # here networks = response.json()
network_id = network['id']
network_name = network['name']
network_type = network['type']
print(network_id, network_name, network_type)
It works file and gives me result :
112 stalin-PC IP4Address
But when I tried to parse the properties key with below code , its not working.
for network in networks:
network_id = network['id']
network_name = network['name']
network_type = network['type']
for line in network['properties']:
properties_value = line.split('|')
network_address = properties_value[0]
print(network_id, network_name, network_type, network_address )`
How can I parse the pipe delimited properties key? Would anyone help me please.
Thank you
Using str methods
Ex:
network = {
'id': 112,
'name': 'stalin-PC',
'type': 'IP4Address',
'properties': 'address=10.0.1.110|ipLong=277893412|state=DHCP Allocated|macAddress=41-1z-y4-23-dd-98'
}
for n in network['properties'].split("|"):
key, value = n.split("=")
print(key, "-->", value)
Output:
address --> 10.0.1.110
ipLong --> 277893412
state --> DHCP Allocated
macAddress --> 41-1z-y4-23-dd-98

In VS code I tried solving a problem using python,selenium, behave but I'm not getting the correct output. Can you tell me where the problem is?

my folders are
features/features_files_folder
features/steps
In features_files_folder
omniwyse.feature
code:
Feature: Omniwyse
#tagcurrent
Scenario Outline: COMPANY
Given I load the website "https://www.omniwyse.com"
When I click "About Us" page
Then verify about header element "About Us"
init.py
In steps folder
step_def_web_element_handling.py
from behave import given, when, then
from step_impl_web_element_handling import webapp
#given(u'I load the website "{url}"')
def step_impl(context,url):
webapp.load_website(url)
#when(u'I click "About Us" page')
def step_impl_goto_page(context):
webapp.goto_page()
#then(u'verify about header element "{component}"')
def step_impl_verify_component(context, component):
webapp.verify_component_exists(component)
step_impl_web_element_handling.py
from selenium import webdriver
driverpath = "/Users/giris/Downloads/chromedriver"
class WebApp:
def __init__(self):
pass
# self.driver.get("https://omniwyse.com/")
def load_website(self, url):
driver = webdriver.Chrome(executable_path = driverpath)
driver.get(url)
def goto_page(self):
driver = webdriver.Chrome(executable_path = driverpath)
driver.find_element_by_xpath("//a[normalize-space()='About Us']").click()
def verify_component_exists(self, component):
# Simple implementation
driver = webdriver.Chrome(executable_path = driverpath)
assert component in driver.find_element_by_xpath("//h1[contains(text(),'About Us')]"), \
"Component {} not found on page".format(component)
webapp = WebApp()
my errors for step_def_web_element_handling
{
"resource": "/c:/Users/giris/OneDrive/Documents/pythonassignment/omni/features/steps/step_def_web_element_handling.py",
"owner": "python",
"code": "no-name-in-module",
"severity": 8,
"message": "No name 'then' in module 'behave'",
"source": "pylint",
"startLineNumber": 4,
"startColumn": 1,
"endLineNumber": 4,
"endColumn": 1
}
same for given, when
output when run using behave command
Using default path "./features" Trying base directory:
C:\Users\giris\OneDrive\Documents\pythonassignment\omni\features
Feature: Omniwyse # features/feature_files_folder/omniwyse.feature:1
1 feature passed, 0 failed, 0 skipped 0 scenarios passed, 0 failed, 0
skipped 0 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.000s
Desired output:
Using default path "./features" Trying base directory:
C:\Users\giris\OneDrive\Documents\pythonassignment\omni\features
Feature: Omniwyse # features/feature_files_folder/omniwyse.feature:1
1 feature passed, 0 failed, 0 skipped 1 scenario passed, 0 failed, 0
skipped 3 steps passed, 0 failed, 0 skipped, 0 undefined Took 0m0.000s
In omniwyse.feature
Replace scenario outline with scenario

unable to extra/list all event log on watson assistant wrokspace

Please help I was trying to call watson assistant endpoint
https://gateway.watsonplatform.net/assistant/api/v1/workspaces/myworkspace/logs?version=2018-09-20 to get all the list of events
and filter by date range using this params
var param =
{ workspace_id: '{myworkspace}',
page_limit: 100000,
filter: 'response_timestamp%3C2018-17-12,response_timestamp%3E2019-01-01'}
apparently I got any empty response below.
{
"logs": [],
"pagination": {}
}
Couple of things to check.
1. You have 2018-17-12 which is a metric date. This translates to "12th day of the 17th month of 2018".
2. Assuming the date should be a valid one, your search says "Documents that are Before 17th Dec 2018 and after 1st Jan 2019". Which would return no documents.
3. Logs are only generated when you call the message() method through the API. So check your logging page in the tooling to see if you even have logs.
4. If you have a lite account logs are only stored for 7 days and then deleted. To keep logs longer you need to upgrade to a standard account.
Although not directly related to your issue, be aware that page_limit has an upper hard coded limit (IIRC 200-300?). So you may ask for 100,000 records, but it won't give it to you.
This is sample python code (unsupported) that is using pagination to read the logs:
from watson_developer_cloud import AssistantV1
username = '...'
password = '...'
workspace_id = '....'
url = '...'
version = '2018-09-20'
c = AssistantV1(url=url, version=version, username=username, password=password)
totalpages = 999
pagelimit = 200
logs = []
page_count = 1
cursor = None
count = 0
x = { 'pagination': 'DUMMY' }
while x['pagination']:
if page_count > totalpages:
break
print('Reading page {}. '.format(page_count), end='')
x = c.list_logs(workspace_id=workspace_id,cursor=cursor,page_limit=pagelimit)
if x is None: break
print('Status: {}'.format(x.get_status_code()))
x = x.get_result()
logs.append(x['logs'])
count = count + len(x['logs'])
page_count = page_count + 1
if 'pagination' in x and 'next_url' in x['pagination']:
p = x['pagination']['next_url']
u = urlparse(p)
query = parse_qs(u.query)
cursor = query['cursor'][0]
Your logs object should contain the logs.
I believe the limit is 500, and then we return a pagination URL so you can get the next 500. I dont think this is the issue but once you start getting logs back its good to know

Python3 - IndexError when trying to save a text file

i'm trying to follow this tutorial with my own local data files:
CNTK tutorial
i have the following function to save my data array into a txt file feedable to CNTK:
# Save the data files into a format compatible with CNTK text reader
def savetxt(filename, ndarray):
dir = os.path.dirname(filename)
if not os.path.exists(dir):
os.makedirs(dir)
if not os.path.isfile(filename):
print("Saving", filename )
with open(filename, 'w') as f:
labels = list(map(' '.join, np.eye(11, dtype=np.uint).astype(str)))
for row in ndarray:
row_str = row.astype(str)
label_str = labels[row[-1]]
feature_str = ' '.join(row_str[:-1])
f.write('|labels {} |features {}\n'.format(label_str, feature_str))
else:
print("File already exists", filename)
i have 2 ndarrays of the following shape that i want to feed the model:
train.shape
(1976L, 15104L)
test.shape
(1976L, 15104L)
Then i try to implement the fucntion like this:
# Save the train and test files (prefer our default path for the data)
data_dir = os.path.join("C:/Users", 'myself', "OneDrive", "IA Project", 'data', 'train')
if not os.path.exists(data_dir):
data_dir = os.path.join("data", "IA Project")
print ('Writing train text file...')
savetxt(os.path.join(data_dir, "Train-128x118_cntk_text.txt"), train)
print ('Writing test text file...')
savetxt(os.path.join(data_dir, "Test-128x118_cntk_text.txt"), test)
print('Done')
and then i get the following error:
Writing train text file...
Saving C:/Users\A702628\OneDrive - Atos\Microsoft Capstone IA\Capstone data\train\Train-128x118_cntk_text.txt
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-24-b53d3c69b8d2> in <module>()
6
7 print ('Writing train text file...')
----> 8 savetxt(os.path.join(data_dir, "Train-128x118_cntk_text.txt"), train)
9
10 print ('Writing test text file...')
<ipython-input-23-610c077db694> in savetxt(filename, ndarray)
12 for row in ndarray:
13 row_str = row.astype(str)
---> 14 label_str = labels[row[-1]]
15 feature_str = ' '.join(row_str[:-1])
16 f.write('|labels {} |features {}\n'.format(label_str, feature_str))
IndexError: list index out of range
Can somebody please tell me what's going wrong with this part of the code? And how could i fix it? Thank you very much in advance.
Since you're using your own input data -- are they labelled in the range 0 to 9? The labels array only has 10 entries in it, so that could cause an out-of-range problem.

displaying PDF using RestAPI and angular JS

I have one Rest API which is returning ResponseEntity<byte[]>(IOUtils.toByteArray(bpStream), headers, HttpStatus.OK); and it is exposed using http get. I am facing issue in chrome, safari and mozilla to display the PDF however it is working fine in IE.
Below is my API
#ResponseBody
#RequestMapping(value = "/blueprint/csd/{projectId}/report/{requestId}", method = RequestMethod.GET)
public ResponseEntity<byte[]> getBlueprint(#PathVariable("requestId") long requestId, #PathVariable("projectId") long projectId, final HttpServletRequest request) throws Exception {
String fileName = "report.pdf";
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "\"application/pdf\",\"pdf\"");
LOG.debug("Inline BP Content");
headers.add("Content-Disposition", new StringBuilder("inline; filename=\"").append(fileName).append("\"").toString());
String outputFileName = "ps_" + requestId + ".pdf";
String fileLocation = System.getProperty("java.io.tmpdir") + File.separator + outputFileName;
InputStream bpStream = new FileInputStream(fileLocation);
return new ResponseEntity<byte[]>(IOUtils.toByteArray(bpStream), headers, HttpStatus.OK);
}
I am calling the above URL using $window.open(),
bit the browser displays string of PDF in place of actual PDF like
%PDF-1.5 %âãÏÓ 3 0 obj <>stream xœ+äî| endstream endobj 4 0 obj <>stream xœmŽ±‚0E÷~Åu¶ ›&CI7C´# Z|ÿÉ¿´q É}ËÎy%{¸à.¹ƒº1ÿd$‡ªÙŠo}w’‹qÄ(òµêX¦Xù‹!—EÕh°5‚eN†KØŸŸYž"µ4Xª&cïö}2M; =jzê«7{¿¬wÖèoÙN_¦G2ãypmfßIAˆ endstream endobj 5 0 obj <>stream xœ+äî| endstream endobj 6 0 obj <>stream xœmŽ±‚0E÷~Åu¾"(lJ˜ $ÜÑ%h±ÿÉ¿´q É}ËÎy%{¸$#p†Cì oÌ?iqÈš­øÖw'8…ÅI£Èײc™då/N$–EÕ(˜Á2'Â%ì…ÏÏ,O‘;[MÚÜ=ìûV7í4ªQÙ§ºz³÷ËqgþÆXÓ©Ë”àhõx\›Ù7F2Aƒ endstream endobj 7 0 obj <>stream xœ+äî| endstream endobj 8 0 obj <>stream xœmŽ±‚0E÷~Åu¶ ›&CI7C´# Z,Äò/}q É}ËÍË9ï¡dÊA1NØpÈÔù'CCÕlÅ·>ä"Dœ1Š|­:–)VþâBÈ%AQ5š¶¶F°ÌÉp {ás3ËS¤Ö ÖU“±wû¾‡3M;pzÔÞìý²Fœ¬Ñß3Îvú2%8:3žj3ûCNA~ endstream endobj 9 0 obj <>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/Font<>/XObject<>>>/Length 2752/BBox[0 0 1224 792]>>stream xœÕ\mœºþ¾¿Â7R–`° \©òrSݪU›ìJ­ÔTË0;t˜³{÷ß÷ØÆÆÃðâ¦Ò4‘âÃpìçøøØ<ìü¼ùôpã3P†V7¿>Ü|»ñПø¯¹ð—ÿDzØÞ|øŠvÑÃúæöÝøn§â¢dkVò –GꊘWtÅíêéæöoUù’­ÒÅh•¦»´BqçouV£r’¼Ü¯P½Ïšø1Ë³æ ­Ë 5›Å»]ž%q“•Ê ø9Ù×ú­€›PµÞ¤ió^(Öûí6®Þxkqž´×uZ×Û´hjUwÒ ŠQ¿)wïÑë&ËSô˜æåëA£q•Šë*­÷y#L—ûzTí?ÅYQ7ÜO.z:pv€r·tÎàu“MY§ÅAÿxŸÓ8Ù#G“|Ïmû,zñ5kÐç¸IŸÊêÍA¿r ŽÌ{'I¹ÝÅEÊoÚ¨ÖPè#öºFc•ÖiUƒ'¤ZÓTÙã¾îkh>+tózà‹y÷[õ*{Ú4Žp‹˜áCgä™°¼1)-|ÝÄÇ}x‰ó=¿c¤£F7ä(+ŽÃ÷Ÿÿ‚r%Ââþ‚5íoÓN¨v3$<t߉h;n÷Fô}I›8Ëë_Ðs•Õ›"þ÷ÇÝc+ã~†lߣ0!=‚˜ÁÉ}ȶO})Ñ7ÓB­ÊŠ0Ñï´491ö=Ç !h=‡õ®‹?nïù¼ýrméÞqÀÏ !ÿÇ»³Ñu¨€Ëú%h¸”¯¢–úþGÝ’"âùœç†óN<’ö&ÁZ`t,$²e¡¦¤~™hô\Ký2¹YÿØxßͤWpñ€›]D¨ïPÞÏ%íEÞUœŽŸ‡ø´ëI9:nå,|xÛ¥£”ËÀ>¢‘¼ÃíÖi¾J}†%¹¬³&E/«ç¼Œ—‚3¦É…JߦQˆ`~QtS¿JoÖ}e˜íàÈjÝà¨Ú[!QŸñ'›©*W‚µìó Æ/¦kbÄ%ÈjX˜9Ü>â:-}\¯³jë|€îŸ³/?æiÕØ-¾|ùÕxÐ'ꉥ÷ëvåýÆÀË\…ÑXž½£åY)ðÕYT«³’&WgFÇóú®¿Ë¢k8šŸ,ˆ&U{+$«h |¿‹¦DvÑŠ-—N&þX?%šÂ…ÑJMÞ`4™:ƒÑ¤Îˆ¦¶¿ÓÑôi_gÐfXÓŠèr%(×Eƒ+ÜÁ¥jo…d\!‹ºàŠBj\ ØqéÔà C<\n\ÞÂà ƒÀ.0¸LÁàR gWäzŽÌ.UÛò©Šw›,¹ìz…]—-ˆ)]}+E«¨Mڅƶ‹+®©À¸xjda'…ÖÒu}#¶È`l( —Ö8#º Åñy 6^i²) IÌÑ×4®[öµ R‚?šáŠõ-ÄbÄã¹ä<Ö:/_DZÚ\Gð‘ŒÈ$dD°ü­”Ë·}~˜øÁŒ¹_â&F‰‹ø)åob.duQ#­Ò)Vsr1; “ž26¬5VH§Qâ„sYÏ__à)çè>)«ý¸ýÏggС4:±ùP2øÌdв#âòì”ÁçR®¥~™È;žÖñÚ{F™È…Ž’úe¢Qs-õKž9_¯m÷Ëœ®Œn'®SR¿LŒÉ­¤~i×y'®¯Ø¶…N«è˜˜ÊLIý21¦»’úeb¬_Jê—‰F͵Ô/¹Ó¯×¶…NW‘²µ³1Ú&âl"w}ŶÝpæ‰1ÐoõB‚î¼–D›oÅ|¾v…ü‰4úÑ'{‰›‰·o想7Ì<äBõš–IGOùûéj¢×ÞÊÈD ÙjÍ5ÑÜY´v‰—hr-×hãOô Ÿ«Ù†ÜùÜÞi¦Óøk]K¯µ°²rU°ñš‚sàæݦ^LKÀ‘Á›*Ã/Z…3àCþJ\ÇCð¦Êðë…Va~4 ‹|'"bíÏþX¸4ýŠÈ< õ¢µè‹Ý‡Â‰,‡wC èB”(€q‰æúB 'Á?Þ¡;ô)­í^?èHœÉ:üP1{.åZê—väІµ+Ô\KýRe×i›Aæ]¬L”<^6­¤~iÇJl“°9—­¯Ø¶“\̉Ÿì ˦•Ô/+foKD%–’ú¥Ê(®Ó¶“\¬¢`kÕ´‰¶Yí\T®¯Ø¶±ü¡—øád>°Ì³dQ:p"A—€‘øá 逊Mzh>ptÀ#‚žNïOåÛíŒëè½...
If I save the above string with .pdf extention and open it using Adobe pdf reader it comes properly. So how I make the browser to display actual pdf in place of string.
I tried content-Type as application/pdf and content-disposition as attachment as well. Any idea what I am doing wrong the above code works fine in IE.

Resources