pdf-lib creating pages with variable data - javascript

I want to generate a dynamic pdf with javascript (node or deno).
I have variable data on request coming into API.
The part I can't figure out is,
For example, an array with 20 objects came from the API. I will print these values to pdf with certain shaping. But since the data coming from the API will be variable, I need to dynamically print the incoming data.
For example, another object should come just below the first object I have printed. If the object's fields exceed one page, the next page should be passed.
In accordance with any data that may come from the API.
I searched here in detail, but could not find a clear solution. (https://pdf-lib.js.org/docs/api/)
I came across a similar question in the past (Create PDF with multiple pages)
But I think the situation here is a little different.
I would be very grateful if someone could enlighten me on this.

Related

architecture for get and store api request data

This is more of a architectural questions. An external platform had product and price information for let's say, books. There is an API available to get this information.
What I read is that it should be possible to create a function in Javascript and connect the Javascript to a page where you want to show the data on my own website. This would mean that for each page request an API-call is made. Since the requested information only changes once a day maximum this does not sound the most efficient solution.
Can someone advise a better solution? Something into the direction of a similar php or javascript function that does the request on the background, schedule an update and import the data into mysql? If so, what language would be most common.
I need the solution for a Joomla/php/mysql environment
Here's a simple idea - fetch and store results from the API (ones you think aren't gonna change in a day), either on disk, or in the database, and later use these stored results to retrieve what you otherwise would've fetched from the API.
Since storing anything in frontend JS across page reloads isn't easy, you need to make use of PHP for that. Based on what's given, you seem to have two ways of calling the API:
via the frontend JS (no-go)
via your PHP backend (good-to-go)
Now, you need to make sure your results are synced every (say) 24 hours.
Add a snippet to your PHP code that contains a variable $lastUpdated (or something similar), and assign it the "static" value of the current time (NOT using time()). Now, add a couple of statements to update the stored results if the current time is at least 24 hours greater than $lastUpdated, followed by updating $lastUpdated to current time.
This should give you what you need with one API call per day.
PS: I'm not an expert in PHP, but you can surely figure out the datetime stuff.
It sounds like you need a cache, and you're not the first person to run into that problem - so you probably don't need to reinvent the wheel and build your own.
Look into something like Redis. There's an article on it available here as well: https://www.compose.com/articles/api-caching-with-redis-and-nodejs/

Presenting dynamic information from CSV file

So, i have googled and googled and googled. Maybe I need to improve my google skills.
Maybe the answer already exists somewhere on this page but I have not found it. Anyway.
So I am dealing with a csv file which shows date, transacation category, location and the amount of the transaction. In that order.
So what I need to do is to present this information dynamically in an HTML table. The user should be able to select the location from a dropdown menu and the table should be updated with the corresponding information about transactions done from that location.
I am able to present the overall information from the csv file, but when making it truly dynamical Im struggeling.
So I have the information stored in a 2d array [date, transaction category, location, transaction category].
So my real problem is, how do I make this truly dynamical. Meaning, how can I update the tag with without hardcoding anything. I want it to be independet of what csv file I am uploading.
I'm thinking that it would be good to have a function that loop through the array and split out the user selected array on an onchange event and then send the selecte array to output. But I cant quite imagined exactly how this would be done...
Thankfull for any suggestions. :)
Honestly, I would turn it into JSON first and go from there. One of my favorite parsers is csvtojson. It's pretty simple to use.

Get fixed number of JSON objects from third-party API

I'm working with this returned API from a third party:
(note: returns LARGE dataset)
https://www.saferproducts.gov/RestWebServices/Recall?format=json
I know I can get this giant object like so:
$.getJSON('https://www.saferproducts.gov/RestWebServices/Recall?format=json', function(json, textStatus) {
console.log(json);
});
This returns ~7000 objects. There is no way in the API call to dictate how many objects you want returned. It's all or nothing.
Here's the question...can I use getJSON (or similar) to only get the first 5 objects and stop without having to load the entire JSON file first?
I did something similar a while a go. I used PHP to fetch a webpage of an api. Then I would cache it. Via PHP logic, I stored a variable inside a text file which contained all the information from the webpage. I had another file that stored the timestamp. Then, when the page was called, php would check the timestamp to see how old it was. If it was too old, it'd recache the page and return relevant information. If it was still valid, it would just return the cached information. If you only want the last 5, the PHP logic wouldn't be too hard to write that in. Then, jQuery would query the PHP page.
They don't have anything called out in their documentation for limiting the returns. I think their intent is for you to narrow down your search so you're not fetching every single item. You could always email them and ask as what Mike McCaughan said, if they don't have a 'limit' baked in, then no, it's not possible.
It also looks like they offer weekly downloads that you can just create your own API and add a limit property:
https://www.fda.gov/%20Safety/Recalls/EnforcementReports/default.htm
Reference:
https://github.com/presidential-innovation-fellows/recalls-api/wiki/data-sources
https://www.cpsc.gov/Recalls/CPSC-Recalls-Application-Program-Interface-API-Information/
https://www.cpsc.gov/Global/info/Recall/CPSC-Recalls-Retrieval-Web-Services-Programmers-Guide_3-25-2015.pdf
If there really is no option for limiting that call, then I'd suggest caching, showing some kind of processing wheel while the call takes place or narrowing your query. They have options to for filtering that may work for you such as the following:
RecallNumber
RecallDateStart
RecallDateEnd
LastPublishDateStart
LastPublishDateEnd
RecallURL
RecallTitle
ConsumerContact
RecallDescription
ProductName
ProductDescription
ProductModel
ProductType
RecallInconjunctionCountry
ImageURL
Injury
ManufacturerCountry
UPC – see caveat below
Hazard
Manufacturer
Remedy
Retailer

jQuery load or better ajax way?

I am creating a product overview page via PHP and want to add detail information via AJAX. I am using the jQuery .load-function. The thing is I want to add detail information at three different places, each having a different format, while the detail data being about the same.
I wanted to be smart and created one PHP page where the database connection and query for the details are established and the three different kinds of information formats are created and then referenced each information format using the load function's ability to address page fragments, i.e. something like
$("#123").load(bestprice.php?id=123 #part1")
$("#123").load(bestprice.php?id=123 #part2")
$("#123").load(bestprice.php?id=123 #part3")
It works, but of course my hope that some sort of cache would arrange for the page to be loaded only once was disappointed. It loads three times for each id.
So, my questions are:
a) Is there a better way for reaching my goal, i.e. creating an array in the loaded page containing the data that I can use in my original page with doing the formatting there? How would I hand it over?
b) Would it be possible to load the page only once and address the parts with some kind of processing in my original page?
Please, don't answer only yes. Consider me a newbie who needs as much explanation as possible. Code samples are greatly appreciated!

Filtering PDO and MySQL queries with JS/jQuery

Sorry for my ignorance on the lack of knowledge I have on this subject however I cannot find an answer to my question anywhere.
So I have this MySQL table:
Feed_ID Vehicle_ID FullRegistration Colour FuelType Year Mileage Bodytype Doors Make Model Variant EngineSize Price PreviousPrice Transmission PictureRefs ServiceHistory PreviousOwners Description FourWheelDrive Options Comments New Used Site Origin V5 Condition ExDemo FranchiseApproved TradePrice TradePriceExtra ServiceHistoryText Cap_ID
As you can see each column will contain vehicle data.
I have displayed all of the results in the database using PDO onto my front end, all data is displayed in a listing style similar to Ebay.
Now I need to filter these results however I have noticed that many result filter systems are using JS.
Here are some examples so you get a better idea of what I am talking about:
http://www.autotrader.co.uk/search/used/cars/
http://www.motors.co.uk/search/car/
As you can see all the filters are using JS however I am having a problem understanding how JS is filtering the MySQL query?
I know this question might be a little broad but can someone show me an example of how JS can filter PDO results just like the examples I have shown?
Thanks
The first one uses what I suspect to be a combined method of Javascript and a server-side language (it's hard to prove, because I can't see the server-side code involved). For simplicity, I'll assume this server-side language is PHP, though it could easily not be.
Basically, all Javascript is doing on the first website is setting cookies and telling you to refresh the page. Once you refresh, PHP fetches the cookies that Javascript set and filters the results from the MySQL query based on those cookies.
Now, the second one is actually filtering using Javascript, yet at the same time still using PHP (again, it could be any server-side language).
This is a method called AJAX. It is a function built into Javascript which allows you to fetch another page from Javascript (aka. send and receive an HTTP request).
The reason this is useful is because once you've changed an option on that page, Javascript can send an HTTP query using AJAX to something like "http://www.motors.co.uk/search/getcarinfo.php?transmission=manual", allowing PHP to fetch a new dataset from MySQL and return it to Javascript (this probably isn't the API entry point that they use, but it has to be somewhere in their Javascript).
Once Javascript receives the response from that page (usually in JSON or XML form), it can modify HTML to update what's shown on the page.
To answer your question directly, Javascript doesn't filter the data. MySQL filters the data based on a PHP query, which returns its response to the Javascript. Then, Javascript just puts it on the screen.

Categories