Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content
Announcements
Qlik Launches Open Lakehouse and advanced agentic AI experience in Qlik Answers! | LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
ajaykakkar93
Specialist III
Specialist III

Error in exporting chart to Image or PDF using Qlik Sense API

Hello Everyone,

i am facing a issue in November 2018,

i am not able to export as PDF or IMAGE, but i am able to export the data to excel i need to implement other export feature as well it will be great if you and others can help me on this issue.

exportPdf_exportImg_issue.PNG






Same error is coming for IMG also

My simple test code

 

var prefix = window.location.pathname.substr(0, window.location.pathname.toLowerCase().lastIndexOf("/extensions") + 1);
var config = {
	host: window.location.hostname,
	prefix: prefix,
	port: window.location.port,
	isSecure: window.location.protocol === "https:"
};
require.config({
	baseUrl: (config.isSecure ? "https://" : "http://") + config.host + (config.port ? ":" + config.port : "") + config.prefix + "resources"
});
require(["js/qlik"], function(qlik) {
	qlik.setOnError(function(error) {
		$('#popupText').append(error.message + "<br>");
		$('#popup').fadeIn(1000);
	});
	$("#closePopup").click(function() {
		$('#popup').hide();
	});
	//callbacks -- inserted here --
	//open apps -- inserted here --
	var app = qlik.openApp('APP-ID', config);
	//get objects -- inserted here --
	// test
	app.getObject('QV01', 'OBJID').then(function(visual) {
		//console.log(visual);
		$("#exportChart").show().click(function() {
			//Working Good
			/*
			visual.exportData().then(function(result) {
				console.log('Data download link: ', result);
			});
			*/
			//Not Working IMG
			/*
				var Imgsettings = {
					imageType: 'png',
					height: 200,
					width: 300
				};
				visual.exportImg(Imgsettings).then(function(result) {
					console.log('Image download link: ', result);
				});
			*/
			//Not Working PDF
			var Pdfsettings = {
				documentSize: "A4",
				aspectRatio: 2,
				orientation: "landscape"
			};
			visual.exportPdf(Pdfsettings).then(function(result) {
				console.log('Pdf download link: ', result);
			});
			console.log("Exporting");
		});
	});
	//create cubes and lists -- inserted here --
});

Can some one help me on this &  please if i am wrong please explain me what was wrong.

 

Regards,

Ajay Kakkar

Please mark the correct replies as Solution. Regards, ARK
Profile| GitHub|YouTube|Extension|Mashup|Qlik API|Qlik NPrinting

Labels (2)
31 Replies
Øystein_Kolsrud
Employee
Employee

When you are not authenticated, only GET requests are accepted. They will redirect you to the right authentication flow and after successful authentication you valid get a valid cookie. Once you have that, you can do other request. The QlikSenseRestClient library should do that automatically though. Maybe you can share your code and I can have a look at what goes wrong? If you are calling the printing service, then it might be that you simply need to call the qrs service with a GET request first. You could try adding something like this:

client.Get("/qrs/about");

 

DBIT
Contributor III
Contributor III

var baseAddress = new Uri("https://<senseserverURL>/qrs/about");
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() {
UseCookies = true,
AllowAutoRedirect = true,
CookieContainer = cookieContainer
})

using (var client = new HttpClient(handler) { BaseAddress = baseAddress})
{
X509Certificate2 Cert = new X509Certificate2("C:\\client.pfx");

client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("User-Agent", "Windows");
client.DefaultRequestHeaders.Add("X-Qlik-User", @"UserDirectory=internal;UserId=sa_repository");
client.DefaultRequestHeaders.Add("X-Qlik-xrfkey", xrfkey);

handler.ClientCertificates.Add(SenseCert);

var getResult = await client.GetAsync(baseAddress);

Now using the HTTPClient in c# instead.

I have replaced the actual server URL with <senseserverURL> for security.

Currently getting a 401: unauthorised response back from the server.
Thanks.

Øystein_Kolsrud
Employee
Employee

Getting all those headers and everything right is quite hard when you use the HttpClient class directly. And since you are doing a direct connection using certificates, you should specify the port for the repository which is 4242.

I'd really recommend you try out some library to do this for you though. That way you don't have to worry about all the nitty gritty details of the authentication flow which can be quite convoluted. With the QlikSenseRestClient library your code would look like this:

var client = new RestClient("https://<senseserverURL>/");
var certs = RestClient.LoadCertificateFromDirectory("C:\\client.pfx");
client.AsDirectConnection("internal", "sa_repository", certificateCollection: certs);

var getResult = await client.GetAsync("/qrs/about");

 

DBIT
Contributor III
Contributor III

Great. How can I add a custom header to a rest client object using this? Seems to be the only thing not provided for in examples?

Øystein_Kolsrud
Employee
Employee

You should be able to do like this:

client.CustomHeaders["myheader"] = "value";

But what do you need a custom header for? Just curious.

 

DBIT
Contributor III
Contributor III

That's great thanks! It's been a while since I did any C# so I'm more than a bit rusty. I don't really recall assigning values this way.

Why do I need to? The API I'm interacting with requires additional headers as above. I can't work out how to get them in any other way. I have been able to establish a connection with POSTMAN but was getting the same error as my C# program without the headers being added, so this should hopefully resolve the issue!

 

Thank you again for the help and direction.

Øystein_Kolsrud
Employee
Employee

I'm not sure what you mean here. How would you have preferred to set the header?

Or do you mean the language construct used? The CustomHeader property is a dictionary. So it's assigning a value in the dictionary.

DBIT
Contributor III
Contributor III

Yes. Only real compiled language I have worked with in the last decade is objective C, and a little Java. Still getting used to C# again.

 

Thanks again.

DBIT
Contributor III
Contributor III

After scratching my head, getting 405 errors (method not allowed) from server when requesting export of the PDF after finally getting this together, it seems this only works for Qlik Cloud? I'm interacting with Enterprise installed on an on premises server, so seems like I've been going down the wrong rabbit hole.

Do you know any way to interact with the Enterprise APIs in a similar way? I'd have thought the APIs would have been fairly close in terms of functionality across the board being extremely similar products but apparently not.

Øystein_Kolsrud
Employee
Employee

You mean that library? Or the APIs for printing? The library handles both Client Managed and QCS. You can find examples for both in the repo for that library:

https://github.com/kolsrud/qlik_rest_sdk/tree/master/Qlik.Sense.RestClient

Printing is handled very differently in the two though, so the flow for exporting a PDF will require usage of two totally different sets of APIs. The APIs for doing this in Client Managed are not public.

OSZAR »