Access the BuildVu Microservice using Dart
Table of contents
- Introduction
- Prerequisites
- Code Example
- Return result to a callback url
- Configuration Options
- Upload by URL
- Using Authentication
- Further details
Introduction
The following tutorial shows you how to convert PDF files to HTML or SVG using a hosted BuildVu cloud API, such as:
- the IDRsolutions trial and cloud subscription service
- your own self-hosted BuildVu microservice
This tutorial uses our REST API.
Prerequisites
Before you begin you will need to ensure you have an up-to-date version of the Dart SDK installed. You can find more on this on Dart’s website.
You should also install the following libraries:
http
http_parser
path
Code Example
Here is a basic code example to convert PDF files to HTML or SVG. Configuration options and advanced features can be found below.
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:path/path.dart';
import 'dart:convert' as convert;
void main() async {
final apiUrl = 'https://cloud.idrsolutions.com/cloud/buildvu';
final filePath = 'path/to/exampleFile.pdf';
final file = File(filePath);
// Prepare the request headers and form data
final request = http.MultipartRequest('POST', Uri.parse(apiUrl));
request.fields['token'] = 'your_token'; //Required only when connecting to the IDRsolutions trial and cloud subscription service
request.fields['input'] = 'upload';
// Add the file to the form data
final fileBytes = await file.readAsBytes();
final fileStream = http.ByteStream.fromBytes(fileBytes);
final fileLength = file.lengthSync();
final fileName = basename(filePath);
request.files.add(http.MultipartFile(
'file',
fileStream,
fileLength,
filename: fileName,
contentType: MediaType('application', 'pdf'),
));
late String uuid;
// Send the request to upload the file
try {
final response = await request.send();
if (response.statusCode != 200) {
print('Error uploading file: ${response.statusCode}');
exit(1);
}
final responseBody = await response.stream.bytesToString();
final Map<String, dynamic> responseData = convert.jsonDecode(responseBody);
uuid = responseData['uuid'];
print('File uploaded successfully!');
} catch (e) {
print('Error uploading file: $e');
exit(1);
}
// Poll until done
try {
while (true) {
final pollResponse = await http.Request('GET', Uri.parse('$apiUrl?uuid=$uuid')).send();
if (pollResponse.statusCode != 200) {
print('Error Polling: ${pollResponse.statusCode}');
exit(1);
}
final Map<String, dynamic> pollData = convert.jsonDecode(await pollResponse.stream.bytesToString());
if (pollData['state'] == "processed") {
print("Preview URL: ${pollData['previewUrl']}");
print("Download URL: ${pollData['downloadUrl']}");
break;
} else {
print("Polling: ${pollData['state']}");
}
// Wait for next poll
await Future.delayed(Duration(seconds: 1));
}
} catch (e) {
print('Error polling file: $e');
exit(1);
}
}
Return result to a callback url
The BuildVu Microservice accepts a callback url to send the status of a conversion on completion. Using a callback url removes the need to poll the service to determine when the conversion is complete.
The callback url can be provided to the params map as shown below.
final request = http.MultipartRequest('POST', Uri.parse(apiUrl));
request.fields['token'] = 'your_token'; //Required only when connecting to the IDRsolutions trial and cloud subscription service
request.fields['input'] = 'upload';
request.fields['callbackUrl'] = 'http://listener.url';
Configuration Options
The BuildVu API accepts a stringified JSON object containing key value pair configuration options to customise your conversion. The settings should be added to the parameters array. A full list of the configuration options to convert PDF files to HTML or SVG can be found here.
final settings = {
"key": "value",
"key": "value",
};
final settingsJson = convert.jsonEncode(settings);
final request = http.MultipartRequest('POST', Uri.parse(apiUrl));
request.fields['settings'] = settingsJson;
Upload by URL
As well as uploading a local file you can also provide a URL which the BuildVu Microservice will download and then perform the conversion. To do this you should replace the input and file values in the parameters variable with the following.
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
void main() async {
final apiUrl = 'https://cloud.idrsolutions.com/cloud/buildvu';
final fileUrl = 'hosted_file_url';
final fileName = 'hosted_file_name';
try {
// Get the file from the URL
final response = await http.get(Uri.parse(fileUrl));
if (response.statusCode == 200) {
// Convert the file content to bytes
final fileBytes = response.bodyBytes;
// Prepare the request headers and form data
final request = http.MultipartRequest('POST', Uri.parse(apiUrl));
request.fields['token'] = 'your_token'; //Required only when connecting to the IDRsolutions trial and cloud subscription service
request.fields['input'] = 'upload';
// Add the downloaded file to the form data
final fileStream = http.ByteStream.fromBytes(fileBytes);
final fileLength = fileBytes.length;
request.files.add(http.MultipartFile(
'file',
fileStream,
fileLength,
filename: fileName,
contentType: MediaType('application', 'pdf'),
));
// Send the request to upload the file (same as above code example)
// Poll until done (same as above code example)
} else {
print('Error downloading file: ${response.statusCode}');
}
} catch (e) {
print('Error: $e');
}
}
Using Authentication
If the BuildVu Microservice requires authentication, you will need to provide a username and password. These are provided by passing two variables named username and password to the convert method as shown below.
String apiUrl = 'https://your-api-url.com/endpoint';
String username = 'your-username';
String password = 'your-password';
String credentials = '$username:$password';
String base64Credentials = base64Encode(utf8.encode(credentials));
Map<String, String> headers = {'Authorization': 'Basic $base64Credentials',};
try {
final response = await http.get(Uri.parse(apiUrl), headers: headers);
if (response.statusCode == 200) {
print('Response body: ${response.body}');
} else {
print('Failed to load data. Status code: ${response.statusCode}');
}
} catch (e) {
print('Error: $e');
}
Further details
BuildVu Microservice API
BuildVu Microservice Use