How to convert selected pdf page with gm - node.js

I am converting different images and pdf files with "gm" module for nodejs. Image types go successfully but when I want to convert PDF to image have problems. I need to covert only one selected page from pdf file to jpg/png. If I pass whole pdf file to "gm" it saves to image only first page, but I cannot find the way to save another page.
gm(file).toBuffer(format.toUpperCase(),
function (err, buffer) {
// so in buffer now we have converted image
}
Thank you.

You can use gm.selectFrame like this
gm(file).selectFrame(0).toBuffer() // To get first page
gm(file).selectFrame(1).toBuffer() // To get second page

// for only first pdf page use:
gm(file, 'pdf.pdf[0]').toBuffer(...)
// for only second pdf page use:
gm(file, 'pdf.pdf[1]').toBuffer(...)

There is spindrift for manipulating pdf (includes image conversion).
You can define your pdf using (You don't have you use all of the commands):
var pdf = spindrift('in.pdf')
.pages(7, 24)
.page(1)
.even()
.odd()
.rotate(90)
.compress()
.uncompress()
.crop(100, 100, 300, 200) // left, bottom, right, top
Later on convert to image:
// Use the 'index' property of an image element to extract an image:
pdf.extractImageStream(0)
If you have to use gm, you can do what #Ben Fortune suggested in his comment and split the pdf first.

Related

Add SVG image to PdfSignatureAppearance with iText7

I want to add a SVG image to PdfSignatureAppearance. The method setSignatureGraphic has an ImageData parameter now in iText7. I couldn't find a way to create an imageData from SVG because ImageDataFactory is not supporting this format.
Can you please guide me on how to do that?
Note that with iText5 I was able to add svg after converting it to PDF and import it to a PDFTemplate then create an image after instantiate the PDFTemplate. setSignatureGraphic was accepting com.itextpdf.text.Image as parameter
Your question could be split into 2 more precise and simple ones:
How to process an SVG with iText?
How to create an ImageData instance out of the result of point 1?
As for question 1: one can use SvgConverter class (part of iTextCore's svg module). Unfortunately there are only PDF-related methods there: an SVG could be converted either to Image (class of layout module), or to PdfFormXObject (again PDF-related) or to a PDF file.
// to PDF
SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), signer.getDocument()); // the mentioned `signer` is the instance of PdfSigner which you use to sign the document
// to Image
SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), new File(destinationFolder + "svgAsPdf.pdf"));
As for question 2, there are several answers:
a) Suppose that you want to use this Image as the PdfSignatureAppearance's graphics data. For now the class doesn't provide a convenient setter, however, you could use some low level methods - either getLayer0 or getLayer2 to get the signature's background or foreground. They are represented by PDfFormXObject, hence you can use Canvas to add your image to them:
Image svg = SvgConverter.convertToImage(new FileInputStream(sourceFolder + "your_svg.svg"), signer.getDocument());
Canvas canvas = new Canvas(appearance.getLayer0(), signer.getDocument());
canvas.add(svg);
canvas.close();
b) Suppose that your goal is to use the rendered bitmap as the PdfSignatureAppearance's graphics data. Then there is a specific iText product - pdfRender - which converts PDF files to images. The following code could be applied:
PdfToImageRenderer.renderPdf(new File(destinationFolder + "svgAsPdf.pdf"), new File(folderForTheResultantImage));
Now you can create an ImageData instance out of the resultant image file (by default a PDF is converted to a series of images with the format "pdfnamePAGE_NUMBER.jpg", but one could customize either the name or the output image format). In your case the PDF consist of just one page (which represents the converted SVG) and its name is "image1.jpg". The rest is obvious:
appearance.setSignatureGraphic(ImageDataFactory.create(destinationFolder + "image1.jpg"));

Write File in a specific size NODE JS

I receive an image in base64 string and I wanted to save the image in 3 different sizes. My code for saving the image in my app is as following and it works, how can I set a sepcific size for the image ?
fs.writeFile(pathImage, new Buffer(base64String, "base64"), function (err) {}
You can't just save an image in different sizes by writing part of the file to disk. In order to resize your image you need to first know what image format you are working with and then use an appropriate library to resize the image, usually by reducing image quality or cropping the image.
For example if you are working with a JPEG, PNG, WebP, or TIFF images, you could use https://github.com/lovell/sharp
From its example page
const sharp = require('sharp');
sharp(inputBuffer)
.resize(320, 240)
.toFile('output.webp', (err, info) => ... );

How to render / display multiframe dicom images in vtk?

I am using gdcm ImageReader to read multiframe dicom file. It reads multiframe correctly but I am unable to display the multiframe dicom file.
I am using vtkImageViewer to display single frame image,
vtkImageViewer viewer = new vtkImageViewer();
vtkDICOMImageReader reader = new vtkDICOMImageReader();
reader.SetInputfile(..\\inputFile);
viewer.SetInput(reader.GetOutput());
It displays single frame images correctly but does not display multiframe images.
Anybody knows how to display multiframe dicom files???
I would advise you to use vtkImageViewer2 instead of vtkImageViewer in this context. The former has a method, SetSlice, where, according to the documentation:
'Each call to SetSlice() changes the image data (slice) displayed AND changes the depth of the displayed slice in the 3D scene'
Example**:
vtkSmartPointer<vtkImageViewer2> imageViewer = vtkSmartPointer<vtkImageViewer2>::New();
imageViewer->SetSlice(5); //Specify the index/slice in image data
** Assumes you have set the input connection/data, example in c++ language.

Want to find the way to generate the thumbnail image for a given pdf file

I'm currently looking for a way to generate the thumbnail image for a given pdf file, which shows several pages in the same image. The output should like what shows in the arxiv sanity website. I want to know if there is any npm package which supports this functionality. Thanks.
In ImageMagick command line, you can do that as follows. Suppose you want 8 pages from the PDF.
Input PDF from http://www.arxiv-sanity.com:
convert image.pdf[0-7] -thumbnail 140x140 -background white +smush 20 -bordercolor white -border 10 result.jpg
This takes the first 8 pages, makes thumbnails of size 140x140 and appends them side-by-side with a 20 pixels white spacing between them and adds a 10 pixel white border around it all.
Sorry, I do not know Node.js. But apparently there is a module that integrates ImageMagick. See https://github.com/yourdeveloper/node-imagemagick
var PDFImage = require("pdf-image").PDFImage; //pdf to image convert
var pdfImage = new PDFImage("1120.pdf");
pdfImage.convertPage(0).then(function (imagePath) {
},(err)=>{
console.log("err",err)
})
//##jimp Npm use thumbnail image generate
//if auth error Follow this step :
-> In /etc/ImageMagick-6/policy.xml (or /etc/ImageMagick/policy.xml) find the following line
->
and change it to allow reading and writing by the PDF coder in ImageMagick:

Wand Image from PDF doesn't apply resizing

I'm using wand in a Django project, to generate a thumbnail from different kind of files, e.g pdf, all the thumbnail generation process is done in memory, the source file is get from a request and the thumbnail is saved to a temporary file, then Django FileFiled saves the image in the correct path, but the thumbnail generated keeps the initial size, this is my code:
with image.Image(file=self.content.file, format="png") as im: # self.content is a django model FileField didn't saved yet, so the file inside is still in memory (from the request)
im.resize(200, 200)
name = self.content.file.name
self.temp = tempfile.NamedTemporaryFile()
im.save(file=self.temp)
self.thumbnail = InMemoryUploadedFile(self.temp, None, name + ".png", 'image/png', 0, 0, None) # then self.thumnail as FileField saves the image
Do you have any idea what happen? could be a bug? I've already reported it as issue on wand github page.
The problem comes from the fact that your PDF has more than one page. If you only resize the first page (which is the one you want to display), it works. Try adding the following line after your with statement:
im = image.Image(image=im.sequence[0])
But I agree with you that your version should work as well.

Resources