Giter Club home page Giter Club logo

Comments (13)

jerbob92 avatar jerbob92 commented on July 21, 2024 2

Ah I see, yes that example has the same issue.

Basically it just makes the page the same size in points (inches) as the image is in pixels. Normally you would have something like 72 dots (pixels) per inch. I think they just make them the same to make the example very easy to use/read, but it might look a little odd if you mix it with normally sized pages and/or add other things to the same page.

from go-pdfium.

jerbob92 avatar jerbob92 commented on July 21, 2024 1

Hi! Can you give me your Go code so I can see what's wrong with it?

from go-pdfium.

jerbob92 avatar jerbob92 commented on July 21, 2024 1

Everything is supported in both implementations, WASM and CGO, that it is not working is a bug.

from go-pdfium.

jerbob92 avatar jerbob92 commented on July 21, 2024 1

I haven't figured it out yet, but:

  • It can load the image fine in WebAssembly, I have validated this by checking the pixel size with FPDFImageObj_GetImagePixelSize and actually rendering the bitmap in Go with FPDFImageObj_GetBitmap/FPDFBitmap_GetBuffer, and the image looks like what it should be
  • The image object is added to the PDF content
  • The image object content (XObject) is not added to the PDF content

from go-pdfium.

jerbob92 avatar jerbob92 commented on July 21, 2024 1

What I do want to add:
You seem to be mixing pixel sizes (from FPDFImageObj_GetImagePixelSize) with point sizes (in the matrix and FPDFPage_New). This should work, but it will make a very big PDF (in terms of inches), and if you want to add anything else to the page it might give you some issues.

from go-pdfium.

nonchan7720 avatar nonchan7720 commented on July 21, 2024

@jerbob92
Thanks for the reply.
This is the kind of code we are working on.

type matrix struct {
	a float32
	b float32
	c float32
	d float32
	e float32
	f float32
}

func newMatrix(a, b, c, d, e, f float32) *matrix {
	m := matrix{
		a: a,
		b: b,
		c: c,
		d: d,
		e: e,
		f: f,
	}
	if m.a == 0 {
		m.a = 1
	}
	if m.d == 0 {
		m.d = 1
	}

	return &m
}

func newDefaultMatrix() *matrix {
	return newMatrix(0, 0, 0, 0, 0, 0)
}

func (m *matrix) multiply(other *matrix) *matrix {
	return newMatrix(
		m.a*other.a+m.b*other.c,
		m.a*other.b+m.b*other.d,
		m.c*other.a+m.d*other.c,
		m.c*other.b+m.d*other.d,
		m.e*other.a+m.f*other.c+other.e,
		m.e*other.b+m.f*other.d+other.f,
	)
}

func (m *matrix) scale(x, y float32) *matrix {
	return m.multiply(newMatrix(x, 0, 0, y, 0, 0))
}

func newImageToPage(ctx context.Context, doc references.FPDF_DOCUMENT, imageBuffer []byte) error {
	instance := GetInstance(ctx)
	img, err := instance.FPDFPageObj_NewImageObj(&requests.FPDFPageObj_NewImageObj{
		Document: doc,
	})
	if err != nil {
		return err
	}
	imgObject := img.PageObject
	defer closePageObject(ctx, imgObject)
	_, err = instance.FPDFImageObj_LoadJpegFile(&requests.FPDFImageObj_LoadJpegFile{
		ImageObject:    imgObject,
		FileReader:     bytes.NewReader(imageBuffer),
		FileReaderSize: int64(len(imageBuffer)),
	})
	if err != nil {
		return err
	}
	size, err := instance.FPDFImageObj_GetImagePixelSize(&requests.FPDFImageObj_GetImagePixelSize{
		ImageObject: imgObject,
	})
	if err != nil {
		return err
	}
	m := newDefaultMatrix().scale(float32(size.Width), float32(size.Height))
	_, err = instance.FPDFImageObj_SetMatrix(&requests.FPDFImageObj_SetMatrix{
		ImageObject: imgObject,
		Transform: structs.FPDF_FS_MATRIX{
			A: m.a,
			B: m.b,
			C: m.c,
			D: m.d,
			E: m.e,
			F: m.f,
		},
	})
	if err != nil {
		return err
	}
	page, err := instance.FPDFPage_New(&requests.FPDFPage_New{
		Document: doc,
		Width:    float64(size.Width),
		Height:   float64(size.Height),
	})
	if err != nil {
		return err
	}
	defer ClosePage(ctx, page.Page)
	_, err = instance.FPDFPage_InsertObject(&requests.FPDFPage_InsertObject{
		Page: requests.Page{
			ByReference: &page.Page,
		},
		PageObject: imgObject,
	})
	if err != nil {
		return err
	}
	_, err = instance.FPDFPage_GenerateContent(&requests.FPDFPage_GenerateContent{
		Page: requests.Page{
			ByReference: &page.Page,
		},
	})
	if err != nil {
		return err
	}
	if err != nil {
		return err
	}
	return nil
}

from go-pdfium.

nonchan7720 avatar nonchan7720 commented on July 21, 2024

@jerbob92
I'd be happy to check 🙏.

from go-pdfium.

jerbob92 avatar jerbob92 commented on July 21, 2024

I have looked into it and this seems to be an issue with the WebAssembly version, when I try it in the CGO version it does work, I will investigate a bit more and add some tests for it.

from go-pdfium.

nonchan7720 avatar nonchan7720 commented on July 21, 2024

Thank you for investigating this.
Is this not supported on the Wasm side and there is nothing I can do with this library?

from go-pdfium.

nonchan7720 avatar nonchan7720 commented on July 21, 2024

I wish I could help, but my lack of skills makes it difficult to modify this library yet.

from go-pdfium.

jerbob92 avatar jerbob92 commented on July 21, 2024

This should be fixed in v1.6.1!

from go-pdfium.

nonchan7720 avatar nonchan7720 commented on July 21, 2024

Thank you!
If I have any problems I will let you know👍

from go-pdfium.

nonchan7720 avatar nonchan7720 commented on July 21, 2024

I see, thanks.
I was referring to this.
https://github.com/pypdfium2-team/pypdfium2

from go-pdfium.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.