Convert a JSON Response to pdf - NodeJS - html

When a get request is sent to: 'http://localhost:4000/features'
There is a response with JSON Data which has HTML inside it.
I need the contents of the field name and description to be saved as PDF
Sample:
[{"_id":"5ad4951d0ba1c37c65818bc7","name":"Find your work faster","description":"<p>With an improved <strong>quick search</strong>, searching through all your issues and projects will be nothing else but a breeze. Whether you know the full issue key, part of the issue name, or just have a distant memory of a project from a year ago, start typing the words, and we’ll do the rest for you. The quick search instantly shows the most relevant results, and refreshes them whenever you change your search term.</p>\n\n<p><img alt=\"\" src=\"https://confluence.atlassian.com/jirasoftware/files/945521251/945528523/1/1518181922686/quicksearch.png\" style=\"height:400px; width:800px\" /></p>\n\n<p>If you’ve already found what you were looking for, just treat quick search as a handy work diary. Click anywhere in the box to see the issues and projects you’ve been working on recently, and have the most important work always at your fingertips.</p>\n\n<p>Learn more</p>\n","__v":0},{"_id":"5ad5ddddcd054b2b5b20143c","name":"Project sidebar","description":"<p>The project sidebar that we previewed in JIRA 6.4 is here to stay. We built this new navigation experience to make it easier for you to find what you need in your projects. It's even better, if you are using JIRA Agile: your backlog, sprints, and reports are now just a click away. If you've used the sidebar with JIRA Agile before, you'll notice that cross-project boards, which include multiple projects, now have a project sidebar as well — albeit a simpler version.</p>\n","__v":0}]
Can this be done in nodeJS?

Conversion isn't the right word but generation is. According to the generalized response in json response you can write logic for generation of pdf from it in node-js server.
PDFKit and PDFmake are two good libraries for this purpose.
I've used pdfmake and is very good.
See doc here: https://pdfmake.github.io/docs/

Use html-pdf to generated PDF from html, Where it works on top of phantom
var pdf = require('html-pdf');
pdf.create(file[0].description).toFile('./' + file[0].description + '.pdf', function (err, res) {
console.log(res.filename);
});
Note : Sample code snippet above to handle first object in your array

Related

Opening a downloaded mht file from Selenium (Help needed)

Long story short, I'm not a coder.
My team used to have this coder who created this Python/Selenium code to extract some information from chrome browser (Echocardiography reports) and/or downloaded mht file (also Echocardiography reports).
This code was working fine until recently, it stopped working.
The program still successfully downloads the mht file via chrome.
However, it fails to open the file and hence, code continues without extracting any information - resulting in empty extractions.
This is the part I need help figuring out
driver.get('chrome://downloads')
# driver.get('file:///C:/Users/name/Downloads/')
root1 = driver.find_element_by_tag_name('downloads-manager')
shadow_root1 = expand_shadow_element(root1)
time.sleep(2)
root2 = shadow_root1.find_element_by_css_selector('downloads-item')
shadow_root2 = expand_shadow_element(root2)
time.sleep(1.5)
openEchoFileButton = shadow_root2.find_element_by_id('file-link')
mhtFileName = openEchoFileButton.text
driver.get('file:///C:/Users/name/Downloads/' + mhtFileName) # go to web page
try:
echoDateElement = WebDriverWait(driver, delay).until(
EC.presence_of_element_located((By.XPATH, '/html/body/div[3]/p[1]/span[3]')))
except TimeoutException:
print("Loading page took too much time!")
I'm trying to figure out why it suddenly fails to open the downloaded mht files.
Last time our team tried using this code is back in 2020 and was successful.
Were there any updates to Chrome perhaps?
Help would be immensely appreciated.
Thank you so much in advance.
There are three obvious weaknesses in this code. The first two are the use of time.sleep() to wait for the element to appear and be manipulable. What if the machine is busy doing something else, and 1.5 seconds isn't enough? The right way to do that is to repeatedly check for the element to be ready. You've got a great example of how to do that using WebDriverWait() in this code already. The third weakness is the locator used in that presence_of_element_located() call. XPath locators rooted at "/html" are notoriously fragile, subject to breakage by small changes to the web page. Try to find something in the page that you can check via a more stable locator - ideally, an element with an ID= attribute.

Which to use for Prismic project, prismic-javascript, #prismicio/client, prismic-dom, prismic-reactjs

Cant find a distinctive difference on their docs so asking here.
prismic-javascript or #prismicio/client or prismic-reactjs or prismic-dom
They all are npm packages.
Can someone explain the 4 separate packages and when to use one over the other.
My assumptions.
prismic-javascript is for connecting directly to the v2 api.
#prismicio/client is the same package as prismic-javascript. Still confused about this.
prismic-reactjs is just react components that you can pass data to
from prismic-javascript api results.
prismic-dom is used for plain vanilla JavaScript projects to render dom elements that you pass data to
from prismic-javascript api results.
Am I correct with the above assumptions ?
me, thanks for the question.
prismic-javascript is the old version, where #prismicio/client is version 4 of the library. I definitely see how that's confusing, I'm working on getting the references cleaned up so that's more straightforward.
prismic-dom contains helpers for things like rendering "Rich Text" fields to the DOM. They come from the API as JSON, so you need something like this to turn it into HTML.
prismic-reactjs is similar to prismic-dom, but turns Rich Text fields into JSX to work with React projects.
So if you're working on a vanilla JavaScript site, you'll use #prismicio/client + prismic-dom.
If you're working on a React app, you'll use #prismicio/client + prismic-reactjs.

Firefox Addon : parse/save loaded jsons as HAR or text file

I'm trying out to build my first firefox addon, to solve a problem that I have at work :
I'm trying to automate a part of my daily work on a website I'm using (not developping). I have to crawl several lists on the website, dig an item in the list, to check some values, and do it again and again ...
Im used to developping in Powershell and python, not with a web browser. I have limited rights on the machine I'm working on. The only solution I can easily deploy is a firefox addon on a firefox portable. I'd like to use this to catch the json answers of the website (all of them), parse the answers for some values, and automate some kind of popup "Hey, This item in the list is between 90 and 100 !"
The network console allows me to see the jsons, and I know how to parse it well (I'm actually using a powershell script for this, but I can't connect it to the network console AFAIK)
I found a way to trigger the work at the right time :
function logJson(requestDetails) {
console.log("Chargement : " + requestDetails.url);
}
browser.webRequest.onCompleted.addListener(
logRequests,
{urls: ["*://*.crappywebsi.te/*"],
types: ["xmlhttprequest"]
}
);
This code logs the url at the right moment (every time a json is loaded)
Is it possible to catch the json on the fly, to parse it, look for specific values in a path, calculate something, and popup the result ?
Or just save the json somewhere ?
How ?
I dont want to add a button on the webpage. I want a signal that some values are right. a popup or console log is an good solution for me.
Thanks for your help !

How can I get JSON data out of a web page and parse it in Excel VBA

My company has a web service that we use to keep track of some information. They have built an API that allows us to get information out of it. My group is trying to do the same. However, the other groups have all used C# to accomplish this, and the development level of this team isn't higher than that of Excel VBA. What I need to do:
Go to a known URL http://service.company.com/Group=1
Get the JSON result that appears on the screen into VBA
Translate the JSON to readable human - This part seems to have been solved here
The rest of the code around what I need to do, I can handle. I'm hoping there is a JSON reader built into VBA that I can leverage for part 3. Any help would be much appreciated.
EDIT:
I figured out how to get JSON information out of the web page, there was some user/password authentication required in order to get that. So that is part 1 and 2 done. I'm working on Part 3. The JSON information seems to be coming out in this pattern:
[{"Column1":value1,"Column2":value2},
{"Column1":value1,"Column2":value2}]

Scan an area of a web page's source code for changes while reporting it?

this is one heck of a confusing question to ask so here it goes. Firstly, I'm not asking you to write me any code I just need help going in the right direction for what I'm trying to achieve here. Basically the task is this, I want to scan a select area of a web page's source code for changes and if something does change, I want to report it somewhere (like a console or something). However, I do not want just a notification of change, I also want what the change is/was. I've been looking into things like jsoup but I am still struggling to even find out what this is called.
Any pointers would be insanely appreciated. Thanks, Optimistic.
Here are some steps assuming this is from a node.js project:
Get the URL for the specific script file you're looking for a change in.
Using the request() module, fetch that URL.
Break the data up into lines (probably using .split()).
Find the specific line you are looking for either by counting line numbers of by searching for some representative text in that line.
Using some sort of search in that line (perhaps a regex), find the current value of the exact item in that line you are looking for.
Save the current value.
Then, at some future time, repeat this whole process and compare what you find to the previous value.
If this is being done from a browser instead of node.js, then use an Ajax call to retrieve the file. If the file is on another domain from your web page and that domain does not permit cross-origin requests, then you cannot solve this problem in an automated fashion from a browser in your own web page.
Here is how I would do it with Jsoup:
Document doc = Jsoup.connect(url).get();
String scriptCssQuery = "script"; // Tune this CSS query to find THE script you need.
Element script = doc.select(scriptCssQuery).first();
if (script != null) {
String scriptLines = script.html();
// Store the changing line somewhere and compare it to its previous value...
}