Convert List of encoded Image to PDF - flutter

At first, I am converting a PDF file to Image file.
Then I make changes on the Image(converted PDF).
Now, I want to convert everything back to PDF and also have a copy in a base64 format of the PDF.
This is how I convert my PDF:
Future<List<List<int>>> imageFromPdfFileFullImage(
Future<PdfDocument> pdfFile) async {
int height = 0, width = 0;
final document = await pdfFile;
im.Image imImage;
for (int i = 1; i <= document.pagesCount; i++) {
final page = await document.getPage(i);
final pdfPageImage = await page.render(
width: page.width, height: page.height, format: PdfPageFormat.JPEG);
imImage = im.decodeJpg(pdfPageImage.bytes); // First issue in this line
height += imImage.height;
if (imImage.width > width) {
width = imImage.width;
}
//List<Image> buffer 'package:image/image.dart'
imageList.add(imImage);
//List<List<int>> 'package:image/image.dart' as im
//I want this encodeImaegList to be converted back to PDF later
encodeImageList.add(im.encodePng(imageList[i - 1]));
await page.close();
}
return encodeImageList;
}
This may be useful for those who's looking to convert some pdf files to images.
I hope you can help me on my problem too. Thanks guys!

Try this package, hope it helps
https://pub.dev/packages/pdf

Related

Print image from API in esc_pos package

image of the image when print
I want to print image in 80mm wifi/printer image come from API as string.
Some of the images print correctly and some do not.
My code:
void testReceipt(NetworkPrinter printer, String img) async {
Uint8List unit8List = base64Decode(img);
Image? a = decodeImage(unit8List);
print(decodeImage(unit8List));
printer.image(a!, align: PosAlign.center);
printer.cut();
}
I Solved it by change the width of the image
Uint8List unit8List = base64Decode(img);
image.Image? a = image.decodeImage(unit8List);
image.Image resized = image.copyResize(a!, width: 600, height: a.height);
printer.image(resized, align: PosAlign.center);
printer.cut();

determine the size of placeholder to match the actual size of the image

I'm trying to load images from internet but i want to show a placeholder till load finishes.
How can i determine the size of placeholder to match the actual size of the image?
The width and height of the image from the api are 2000, 3000 respectively. How can I convert these values?
There are a few parts to the question,
First, you should download an image the from the web through one of many methods.
https://docs.flutter.io/flutter/widgets/FutureBuilder-class.html
https://docs.flutter.io/flutter/widgets/Image/Image.network.html
get the bytes and convert to an image with this https://docs.flutter.io/flutter/widgets/Image/Image.memory.html
Here are the widgets you should use.
You should use this widget as a sized placeholder.
https://docs.flutter.io/flutter/widgets/SizedBox-class.html
Here is a possible child to show a loading animation
https://docs.flutter.io/flutter/material/CircularProgressIndicator-class.html
To display it, you can either complete the FutureBuilder or
Image im = null;
Map imageDimensions = null;
initState(){
fetchImageDimension.fetch().then((imageD) => setState(){imageDimensions = imageD;})
somefuntion.fetch().then((image) => setState(){im = image;})
}
Widget build(buildContext context){
var width = imageDimensions["width"];
var height = imageDimensions["height"];
var placeholderColor = imageDimensions["color"];
var dimensionsLoaded = imageDimensions != null;
var imageLoaded = im != null;
return if(imageLoaded)
Image.....
else {
if(dimensionsLoaded) {
SizedBox(
width: width,
height: height,
child: const Card(child: Container(color: Color(placeholderColor)),
)
} else {
//neigher image or dimensions are Loaded. Nothing you can do
}
}
}
}
This code is not meant to be compiled but it is meant to show the possible options to answer the question.

html2canvas toDataURL(image/png") return poor image quality

I tried to use html2canvas to get image screenshot byte from website. However, the screenshot result ended with poor resolution. Looking for advice to improve screenshot quality. Thanks.
How about something like this:
var $wrapper = $("#yourDiv");
setSize($wrapper, "2000px", "20pt");
html2canvas($wrapper, {
onrendered: function (canvas) {
var a = document.createElement('a');
a.href = canvas.toDataURL("image/jpg");
a.download = 'filename.jpg';
a.click();
setSize($wrapper, "1000px", "10pt");
}
});
function setSize(dv, width, fontsize) {
dv[0].style.width = width;
dv[0].style.fontSize = fontsize;
}
This resizes the div and font to bigger size and then shrinks back afterwards.

html2canvas loading issue when saving the image

I am using the following code to save the content of div (image and text) as an image using html2canvas.
$(function() {
$("#save").click(function() {
var flag = true;
var imgpath = document.getElementById('file').value;
if(imgpath.length == 0)
{
alert('Please select image file to upload.');
flag = false;
}
else
{
html2canvas($('.body740'), {
onrendered: function(canvas) {
theCanvas = canvas;
var url = canvas.toDataURL("image/png");
var br = document.createElement("br");
var center = document.createElement("center");
var newImg = document.createElement("img"); // create img tag
newImg.src = url;
$(".body740").hide();
$("#canvas").show(); //div where the final image is shown
document.getElementById("rsimg").src=url;
document.getElementById("rsa").href=url;
}
});
}
});
});
This is my link : http://www.aamras.com/greetings2/
But, when I click the save image button, it takes a lot of time to generate the image. Why is it taking so much time to load? What is the issue?
Solved : Change the version of html2cannvas. I was previously using html2canvas 0.5.0-alpha 2014 version
I Switched to html2canvas 0.5.0-alpha1. It generates the image properly and takes no time to do so. You can download the files from https://github.com/niklasvh/html2canvas/releases

Trying to make use of Jcrop and serverside image resizing with scala Scrimage lib

I'm trying to combine jcrop and scrimage but I'm having trouble in understanding
the documentation of scrimage.
The user uploads an image. When the upload is done the user is able choose a fixed
area to crop with Jcrop:
upload.js
$(function () {
$('#fileupload').fileupload({
dataType: 'json',
progress: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
$("#progress").find(".progress-bar").css(
"width",
progress + "%"
);
},
done: function (e, data) {
$("#cropArea").empty();
var cropWidth = 210;
var cropHeight = 144;
if(data.result.status == 200) {
var myImage = $("<img></img>", {
src: data.result.link
}).appendTo('#cropArea');
var c = myImage.Jcrop({
allowResize: false,
allowSelect: false,
setSelect:[0,0,cropWidth,cropHeight],
onSelect: showCoords
});
}
}
});
});
Example:
When the user is satisfied the coordinates will be posted to the server and there is where the magic should happen.
Controller:
def uploadFile = Action(multipartFormDataAsBytes) { request =>
val result = request.body.files.map {
case FilePart(key, filename, contentType, bytes) => {
val coords = request.body.dataParts.get("coords")
val bais = new ByteArrayInputStream(bytes)
Image(bais).resize(magic stuff with coords)
Ok("works")
}
}
result(0)
}
If i read the docs for scrimage and resize:
Resizes the canvas to the given dimensions. This does not scale the
image but simply changes the dimensions of the canvas on which the
image is sitting. Specifying a larger size will pad the image with a
background color and specifying a smaller size will crop the image.
This is the operation most people want when they think of crop.
But when trying to implement resize with an inputstream Image(is).resize() I'm not sure I how should do this. Resize takes a scaleFactor, position and color... I guess I should
populate the position with the coords I get from jcrop??, and what do I do with the scaleFactor? Anybody got a good example of how to do this this?
Thank you for two great libs!
Subimage is what you want. That lets you specify coordinates rather than offsets.
So simply,
val image = // original
val resized = image.subimage(x,y,w,h) // you'll get these from jcrop somehow