-
-
Notifications
You must be signed in to change notification settings - Fork 450
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Order of attachments #822
Comments
That's because attachments are stored in a PDF |
As far as I understand, elements of |
Nametree keys are sorted in lexical order. |
After some research I now understand that Attachment.ID is not the file name, but the string that identifies the embedded file in package main
import (
"bytes"
"fmt"
"os"
"strings"
pdfcpuapi "github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
pdfcpucreate "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/create"
pdfcpumodel "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/model"
pdfcputypes "github.com/pdfcpu/pdfcpu/pkg/pdfcpu/types"
)
func main() {
ctx, err := pdfcpu.CreateContextWithXRefTable(pdfcpumodel.NewDefaultConfiguration(), pdfcputypes.PaperSize["A4"])
if err != nil {
panic(err)
}
template := `{"pages": { "1": { "content": { "text": [{ "value": "page 1", "anchor": "left", "font": { "name": "Helvetica", "size": 12 } }] } } } }`
err = pdfcpucreate.FromJSON(ctx, strings.NewReader(template))
if err != nil {
panic(err)
}
attachments := []pdfcpumodel.Attachment{
{Reader: strings.NewReader("afile"), ID: "id1", FileName: "a.txt", Desc: "a-decs"},
{Reader: strings.NewReader("1file"), ID: "id2", FileName: "1.txt", Desc: "1-decs"},
{Reader: strings.NewReader("zfile"), ID: "id3", FileName: "z.txt", Desc: "z-decs"},
{Reader: strings.NewReader("dfile"), ID: "id4", FileName: "d.txt", Desc: "d-decs"},
}
for _, a := range attachments {
err = ctx.AddAttachment(a, false)
if err != nil {
panic(err)
}
}
var b bytes.Buffer
err = pdfcpuapi.WriteContext(ctx, &b)
if err != nil {
panic(err)
}
attachements, err := pdfcpuapi.ExtractAttachmentsRaw(bytes.NewReader(b.Bytes()), "", nil, nil)
if err != nil {
panic(err)
}
for _, a := range attachements {
fmt.Println(a.ID, a.FileName, a.Desc)
}
} Expected output is:
But the actual one is:
And actually if you analyze the constructed PDF, return xRefTable.NewFileSpecDict(a.ID, a.ID, a.Desc, *sd)
So this issue is actually not about the order of attachments, but about losing attachments files names. |
Thanks I'll take a look. |
It looks like v0.7.0 sorts the attachments alphanumerically by file name (ID), but as long as technically attached files are ordered in PDF, it confuses. For example in PDF text attached files could be referenced not only by name, but also by their order.
Code to reproduce:
Expected output:
Actual output:
The text was updated successfully, but these errors were encountered: